Ask Your Question

real time face tracking java

asked 2013-04-25 07:37:52 -0500

orochi gravatar image

updated 2013-05-12 18:55:08 -0500

Hi guys, I'm very vey new to opencv. i'm trying to make a Java program that can detect my face through a webcam. i don't know which library i should import and i don't know how to make it able to detect my webcam. Based on this tutorial i try to write my java code for detecting face through a webcam, but i got so many errors, can someone tell me why the error occur? I'm new in opencv and java.

  import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.highgui.VideoCapture;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

public class CamCapture {

     * @param args

    public static void main(String[] args) {

        //load opencv native library

        String face_cascade_name = "haarcascade_frontalface_alt.xml";
        String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
        CascadeClassifier face_cascade = new CascadeClassifier();
        CascadeClassifier eyes_cascade = new CascadeClassifier();
        String window_name = "Capture - Face detection.jpg";

        System.out.println("capture through camera "+Core.VERSION);

        //load the face xml cascade
            System.out.println("Error loading face cascade");
            System.out.println("Success loading face cascade");

        //load the eyes xml cascade
            System.out.println("Error loading eyes cascade");
            System.out.println("Success loading eyes cascade");

        //detect default camera
        VideoCapture capture = new VideoCapture(0);

            System.out.println("Did not connected to camera.");
            System.out.println("Conected to camera: "+capture.toString());

        //create new Mat image
        Mat frame = new Mat();

        Mat frame_gray = new Mat();
        Imgproc.cvtColor(frame, frame_gray, Imgproc.COLOR_BGRA2GRAY);
        Imgproc.equalizeHist(frame_gray, frame_gray);

        MatOfRect faces = new MatOfRect();

        face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0, new Size(30,30), new Size() );

        Rect[] facesArray = faces.toArray();

        for(int i=0; i<facesArray.length; i++)
            Point center = new Point(facesArray[i].x + facesArray[i].width * 0.5, facesArray[i].y + facesArray[i].height * 0.5);
             Core.ellipse(frame, center, new Size(facesArray[i].width * 0.5, facesArray[i].height * 0.5), 0, 0, 360, new Scalar(255, 0, 255), 4, 8, 0);

             Mat faceROI = frame_gray.submat(facesArray[i]);
             MatOfRect eyes = new MatOfRect();

             //-- In each face, detect eyes
             eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0,new Size(30,30), new Size());            

             Rect[] eyesArray = eyes.toArray();

             for (int j = 0; j < eyesArray.length; j++)
                Point center1 = new Point(facesArray[i].x + eyesArray[i].x + eyesArray[i].width * 0.5, facesArray[i].y + eyesArray[i].y + eyesArray[i].height * 0.5);
                int radius = (int) Math.round((eyesArray[i].width + eyesArray[i].height) * 0.25);
      , center1, radius, new Scalar(255, 0, 0), 4, 8, 0);

        Highgui.imwrite(window_name, frame);


edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-04-25 08:21:17 -0500

Basically this tutorial tells you how to implement real time face detection.

For Java just look up the corresponding functionality in the Java docs.

A guide that might help you can be found here.

edit flag offensive delete link more


thank you Steven, i really appreciate it

orochi gravatar imageorochi ( 2013-04-26 08:22:32 -0500 )edit

accept the answer if it suited your problem. It will make the topic show as solved for people looking for the same answers :)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-26 08:24:20 -0500 )edit

Hi Steven, i have tried to write it in java, but i got many errors, can you pls take a look into my code and tell me why the error occur, i'm still new in java

orochi gravatar imageorochi ( 2013-05-01 09:03:14 -0500 )edit

I am not a Java specialist so I guess this is just a step to far for me. However, can you please add the error output. I do not have time to go compile code myself and look for error in the output.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-05-02 01:56:19 -0500 )edit

oh nevermind steven, i try to fix it by myself, it seems i'm not implement the method in the right way.

orochi gravatar imageorochi ( 2013-05-02 05:59:00 -0500 )edit

hi orochi, could you please update your post with the changes you made ? thanks

sc3sc3 gravatar imagesc3sc3 ( 2013-05-06 05:57:54 -0500 )edit

hi Steven and sc3sc3, finally i manage to detect my face but not my eyes because i dont know function of this code Mat faceROI = frame_gray( faces[i] ); what that code means?

orochi gravatar imageorochi ( 2013-05-11 07:49:17 -0500 )edit

It selects the actual face region (ROI = region of interest) in the original input to perform eye detection. It would be stupid to run it over the entire image. It reduces the time needed to detect eyes.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-05-11 12:33:09 -0500 )edit

so far i manage to detect my face but not my eyes, there is no error in my coding and when i run the code, it also not generate any error, but in capture image, it's only detect my face

orochi gravatar imageorochi ( 2013-05-11 17:18:32 -0500 )edit

Actually you should keep in mind that eyes do need a lot of pixels of information in order to get detected using a cascade classifier. This means that if your face is like 30x30 pixels large in the image and you try to find eyes in that region, it will fail all the way through. Try the following steps to see if your code is wrong, or it is your input images:

  • Take a large resolution photo of your face, like 450x450 pixels
  • Pass that image to the eye detector
  • Be sure to look straight in front of you towards the camera
  • See if it detects the eyes

If this works, your code is correct, and your eyes just contain not enough information to be detectable.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-05-12 04:09:18 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2013-04-25 07:37:52 -0500

Seen: 7,942 times

Last updated: May 12 '13