Ask Your Question
-1

Can someone please rewrite the Cascade Classifier sample script?

asked 2018-04-13 11:44:32 -0600

Marko5280 gravatar image

updated 2018-04-19 04:07:36 -0600

The Cascade Classifier sample script is written in C++ only. I am studying java. Can someone please rewrite the sample in Java.

edit retag flag offensive close merge delete

Comments

1

THANK YOU!

But, this big Java script is GitHub research and pushes into Android. As a new user I need a sample of OpenCV Face Detection in Java without adding complexity.

Marko5280 gravatar imageMarko5280 ( 2018-04-14 10:47:09 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2018-04-18 06:58:23 -0600

Marko5280 gravatar image

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/12...

> real time face tracking java

http://answers.opencv.org/question/12...

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 ...
(more)
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-04-13 11:44:32 -0600

Seen: 422 times

Last updated: Apr 19 '18