Ask Your Question
2

Face Detection through a webcam in java

asked 2013-05-06 21:40:40 -0600

orochi gravatar image

Hi, i'm trying to create an application, it will detect our face through a webcam, i'm referring to this tutorial and write it in java code. here is my code:

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.Size;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.highgui.VideoCapture;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;


/**
 * @author Lycosa
 *
 */
public class detedctAndDisplay {



    /** Global variables */
     private static String face_cascade_name = "haarcascade_frontalface_alt.xml";
     private static String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
     private static CascadeClassifier face_cascade = new CascadeClassifier();
     private static CascadeClassifier eyes_cascade = new CascadeClassifier();
     private static String window_name = "Capture - Face detection";


     public static void detectAndDisplay(Mat frame)
     {
       Mat frame_gray = new Mat();
       MatOfRect faces = new MatOfRect();

       Rect[] facesArray = faces.toArray();

       Imgproc.cvtColor(frame, frame_gray, Imgproc.COLOR_BGRA2GRAY);
       Imgproc.equalizeHist(frame_gray, frame_gray);

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

       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();

         Rect[] eyesArray = eyes.toArray();

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

         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);
            Core.circle(frame, center1, radius, new Scalar(255, 0, 0), 4, 8, 0);
         }
       }
       //-- Show what you got
       Highgui.imwrite(window_name, frame);
     }

    /**
     * @param args
     */
     public static void Main(String args[])
     {
       VideoCapture capture;
       Mat frame = new Mat();

       //-- 1. Load the cascades
       if (!face_cascade.load(face_cascade_name))
       {
           System.out.print("--(!)Error loading\n");
           return;
       }
       if (!eyes_cascade.load(eyes_cascade_name))
       {
           System.out.print("--(!)Error loading\n");
           return;
       }

       //-- 2. Read the video stream
       capture = new VideoCapture(0);
       if(!capture.isOpened())
       {
           System.out.println("Did not connect to camera.");
       }
       else
       {
           capture.retrieve(frame);
           detectAndDisplay(frame);
           capture.release();
       }
       }
}

There are no error in the code, but i'm not sure whether i'm doing it correctly because when i'm trying to run the app it says "Selection does not contain main type". What is that mean?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2013-05-07 02:34:27 -0600

sc3sc3 gravatar image

updated 2013-05-07 02:35:02 -0600

there's a problem with your main method i think you should start by changing

public static void Main(String args[])

into this:

public static void main(String args[])

i didn't run the code so their might be other problems too :-)

edit flag offensive delete link more

Comments

woaaaa, thanks man, i didnt realise that one. now i can run the code, but unfortunately, got an exception error lol, i'll try to solve it. Thank you very much sc3sc3

orochi gravatar imageorochi ( 2013-05-07 06:33:26 -0600 )edit

another thing i don't see in your code is: System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

you have to load the opencv libraries see https://github.com/Itseez/opencv/blob/master/samples/java/eclipse/HelloCV/src/Main.java

sc3sc3 gravatar imagesc3sc3 ( 2013-05-07 08:12:00 -0600 )edit

hi, i already at the library, but still got an exception error.. this is the error

Exception in thread "main" java.lang.UnsatisfiedLinkError: org.opencv.objdetect.CascadeClassifier.CascadeClassifier_0()J at org.opencv.objdetect.CascadeClassifier.CascadeClassifier_0(Native Method) at org.opencv.objdetect.CascadeClassifier.<init>(CascadeClassifier.java:38) at detedctAndDisplay.<clinit>(detedctAndDisplay.java:29)

what is that mean?

orochi gravatar imageorochi ( 2013-05-07 21:45:40 -0600 )edit

check out this post for the answer: http://answers.opencv.org/question/13450/please-help-me-in-configure-the-opencv-for-eclipse/ (dont forget to upvote the post)

keltik gravatar imagekeltik ( 2013-05-25 18:26:12 -0600 )edit

thanks keltik, i already found the answer :)

orochi gravatar imageorochi ( 2013-05-26 20:54:08 -0600 )edit
1

What did you do to correct it?

RadicalAirTime gravatar imageRadicalAirTime ( 2013-05-31 03:25:47 -0600 )edit
orochi gravatar imageorochi ( 2013-06-01 02:24:16 -0600 )edit

did you need to add the private static String face_cascade_name?

RadicalAirTime gravatar imageRadicalAirTime ( 2013-06-01 22:29:30 -0600 )edit

if you want to put it as global variable, you need to add static into it, what i did to correct it is i load the library, because in the above code, i forgot to load the library. you can refer on this http://answers.opencv.org/question/12395/real-time-face-tracking-java/

orochi gravatar imageorochi ( 2013-06-02 06:54:49 -0600 )edit

Hi, when i run the code i get this error OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file ........\opencv\modules\core\src\array.cpp, line 2482 Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ........\opencv\modules\core\src\array.cpp:2482: error: (-206) Unrecognized or unsupported array type in function cvGetMat ]

can anyone explain why?

moods gravatar imagemoods ( 2015-01-07 00:35:18 -0600 )edit

Question Tools

Stats

Asked: 2013-05-06 21:40:40 -0600

Seen: 15,101 times

Last updated: May 07 '13