2

I'm new to OpenCV and I want to run a Java program for face detection using OpenCV.

Only including one haarcascade xml file doesn't give me expected results. So I need to run two,three haarcascade files in the same program. (specially "haarcascade_frontalface_alt.xml" and "haarcascade_profileface.xml" together).

I tried to do it with the following code but it didn't work. Please mention how to proceed.

Thank you.

public class LiveFeed extends WatchDogBaseFrame {

private DaemonThread myThread = null;
int count = 0;
VideoCapture webSource = null;
Mat frame = new Mat();
MatOfByte mem = new MatOfByte();
CascadeClassifier faceDetector1 = new CascadeClassifier("/home/erandi/NetBeansProjects/WatchDog/src/ueg/watchdog/view/haarcascade_frontalface_alt.xml");
CascadeClassifier faceDetector2 = new CascadeClassifier("/home/erandi/NetBeansProjects/WatchDog/src/ueg/watchdog/view/haarcascade_eye.xml");
MatOfRect faceDetections = new MatOfRect();

public LiveFeed(WatchDogBaseFrame parentFrame) {
    super(parentFrame);
    initComponents();
    super.setCloseOperation();
    jButtonExit.setVisible(false);
}

//class of demon thread
public class DaemonThread implements Runnable {

    protected volatile boolean runnable = false;

    @Override
    public void run() {
        synchronized (this) {
            while (runnable) {
                if (webSource.grab()) {
                    try {
                        webSource.retrieve(frame);
                        Graphics graphics = jPanelVideo.getGraphics();
                        faceDetector1.detectMultiScale(frame, faceDetections);
                        faceDetector2.detectMultiScale(frame, faceDetections);
                        for (Rect rect : faceDetections.toArray()) {
                            // System.out.println("ttt");
                            Imgproc.rectangle(frame, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
                                    new Scalar(0, 255, 0));
                        }
                        Imgcodecs.imencode(".bmp", frame, mem);
                        Image im = ImageIO.read(new ByteArrayInputStream(mem.toArray()));
                        BufferedImage buff = (BufferedImage) im;
                        if (graphics.drawImage(buff, 0, 0, getWidth(), getHeight() - 150, 0, 0, buff.getWidth(), buff.getHeight(), null)) {
                            if (runnable == false) {
                                System.out.println("Paused ..... ");
                                this.wait();
                            }
                        }
                    } catch (Exception ex) {
                        System.out.println("Error");
                    }
                }
            }
        }
    }
}
Erandi Ganepola
  • 303
  • 3
  • 12
  • The second call to `faceDetector2.detectMultiScale(frame, faceDetections);` would override the results of previous class stored in `faceDetections`. Create 2 variables `faceDetections1` and `faceDetections2` then iterate them separately, they wont be implicitly concat the results – ZdaR Oct 27 '16 at 04:54

1 Answers1

1

Object Detection using Haar feature-based cascade classifiers is an effective object detection method proposed by Paul Viola and Michael Jones in their paper, "Rapid Object Detection using a Boosted Cascade of Simple Features" in 2001. It is a machine learning based approach where a cascade function is trained from a lot of positive and negative images. It is then used to detect objects in other images.

OpenCV already contains many pre-trained classifiers for face, eyes, smile etc. Those XML files are stored in opencv/data/haarcascades/ folder.

You can't run many cascade files simultaneously and increase performance. But you can use them one by one as a loop and pass input images through that loop.

Example code is given in this link: OpenCv sample code

Erandi Ganepola
  • 303
  • 3
  • 12