Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

SOLVED

I have found code examples close to what I am trying to do as follows . . .

> Face Detection through a webcam in java

http://answers.opencv.org/question/12978/face-detection-through-a-webcam-in-java/

> real time face tracking java

http://answers.opencv.org/question/12395/real-time-face-tracking-java/

These examples have given me enough code to write my program to find and display faces in a live webcam stream.

My program in its entirety follows . . .

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.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.videoio.VideoCapture;

public class My_Casscade {



   public static void main(String[] args) {

       //load OpenCV 3.4.1 Native Library
       System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

       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 = "Video";

       System.out.println("4/18/2018");
       System.out.println("OpenCV 3.4.1");
       System.out.println("My Casscade");
       System.out.println("capture through camera "+Core.VERSION);


       // Load the face xml cascade
       if(face_cascade.load(face_cascade_name))
       {
           System.out.println("Success loading face cascade");
       }
       else
       {
           System.out.println("Error loading face cascade");
       }

       // Load the eyes xml cascade
       if(eyes_cascade.load(eyes_cascade_name))
       {
           System.out.println("Success loading eyes cascade");
       }
       else
       {
           System.out.println("Error loading eyes cascade");
       }

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

       if(capture.isOpened())
       {
           System.out.println("Conected to camera: " + capture.toString());
       }
       else
       {
           System.out.println("Did not connected to camera.");
       }

       // Create new Mat image
       Mat frame = new Mat();
       Mat frame_gray = new Mat();
       int framecount = 0;
       int facecount = 0;

       while ( capture.read(frame) )
       {
           System.out.println("Frame Count   " + framecount);
           framecount += 1;
           if( frame.empty() )
           {
               System.out.println(" --(!) No captured frame -- Break!");
               break;
           }

       // Apply the classifier to the frame
       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++)
       {
           facecount += 1;
           System.out.println("                           Face Count   " + facecount);
           Point center = new Point(facesArray[i].x + facesArray[i].width * 0.5, facesArray[i].y + facesArray[i].height * 0.5);
            Imgproc.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++)
            {
                System.out.println("i = " + i);
                System.out.println("j = " + j);
                System.out.println("facesArray[i].x = " + facesArray[i].x);
                System.out.println("facesArray[i].y = " + facesArray[i].y);
                System.out.println("eyesArray[i].x = " + eyesArray[i].x);
                System.out.println("eyesArray[i].y = " + eyesArray[i].y);
                System.out.println("eyesArray[i].width = " + eyesArray[i].width);
                System.out.println("eyesArray[i].height = " + eyesArray[i].height);
                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);
               Imgproc.circle(frame, center1, radius, new Scalar(255, 0, 0), 4, 8, 0);
            }
       }

       HighGui.imshow(window_name, frame);
       HighGui.waitKey(1);
       }
       capture.release();
       }

}