Ask Your Question

baxterstockman's profile - activity

2015-04-15 13:24:13 -0600 received badge  Editor (source)
2015-04-15 13:11:13 -0600 asked a question How to check if Eye Detection runs (in Java)

I got face detection to work with openCV, right now it detects my eyes. What I want it that the program displays when it detects eyes and when not.

My code looks like this

 public class FaceDetection extends javax.swing.JFrame {



private DaemonThread myThread = null;
    int count = 0;
    VideoCapture webSource = null;
    Mat frame = new Mat();
    MatOfByte mem = new MatOfByte();
    CascadeClassifier faceDetector = new CascadeClassifier(FaceDetection.class.getResource("haarcascade_eye.xml").getPath().substring(1));

MatOfRect faceDetections = new MatOfRect();


class DaemonThread implements Runnable {
 protected volatile boolean runnable = false;

public void run() {
    synchronized (this) {
        while (runnable) {
            if (webSource.grab()) {
                try {
                    webSource.retrieve(frame);
                    Graphics g = jPanel1.getGraphics();
                    faceDetector.detectMultiScale(frame, faceDetections);
                    for (Rect rect : faceDetections.toArray()) {
                        System.out.println("Eyes detected");

                        Core.rectangle(frame, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
                                new Scalar(0, 255, 0));

                    }
                    Highgui.imencode(".bmp", frame, mem);
                    Image im = ImageIO.read(new ByteArrayInputStream(mem.toArray()));
                    BufferedImage buff = (BufferedImage) im;
                    if (g.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");
                }
            }
        }
    }
}

}

The problem is

System.out.println("Eyes detected");

shows up as soon as the camera is moving, even if I am not in front of the camera. I only want it to be displayed when the eye detector are being activated, how do I do that?

Thanks