Does Cascade Classifier work in OpenCV 2.4.11?

This is where you talk about the EV3 software itself, installation issues, and programming talk.

Moderators: roger, gloomyandy, skoehler

clplaneguy
Novice
Posts: 29
Joined: Sat Feb 18, 2017 3:44 pm

Does Cascade Classifier work in OpenCV 2.4.11?

Postby clplaneguy » Sun Apr 15, 2018 5:45 pm

I am trying to make a robot with face detection. I have created a script to detect faces in a live camera feed from a Webcam. It works fine in OpenCV 3.4.1 Java! However, the Lego EV3 uses leJOS and OpenCV 2.4.11! The script does not complete with OpenCV 2.4.11! It halts on line 34.

Code: Select all

CascadeClassifier face_cascade = new CascadeClassifier();


I get 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 samples.face_detection.main(face_detection.java:34)


Does anyone know the cause/solution?

User avatar
gloomyandy
leJOS Team Member
Posts: 6047
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Does Cascade Classifier work in OpenCV 2.4.11?

Postby gloomyandy » Sun Apr 15, 2018 8:20 pm

Are you sure you have loaded the openCV native library before trying to use any openCV objects/methods? That is the usual cause of problems like this.

https://stackoverflow.com/questions/164 ... fier-error
leJOS news https://lejosnews.wordpress.com/

clplaneguy
Novice
Posts: 29
Joined: Sat Feb 18, 2017 3:44 pm

Re: Does Cascade Classifier work in OpenCV 2.4.11?

Postby clplaneguy » Mon Apr 16, 2018 2:59 pm

As I understand it, the correct procedure is to

1) Load OpenCV 2.4.11
2) Load the Native Library for OpenCV 2.4.11
3) Implement the Native Library in the code

My plan is to: . . .
1) Get my script to run with OpenCV 3.4.1 on the PC in Java! Done!
2) Get my script to run with OpenCV 2.4.11 on the PC in Java! Inprocess
3) Get my script to run with OpenCV 2.4.11 on the EV3 in leJOS Java! Pending


But, this script was written for OpenCV 3.4.1 and runs correctly in OpenCV 3.4.1 on the PC! I am trying to make it run in OpenCV 2.4.11 on the PC!
OpenCV 2.4.11 and the native library seem to be installed correctly.

DOES ANYONE KNOW WHAT I AM DOING WRONG (NOT DOING CORRECTLY)?

My OpenCV 2.4.11 Java script in its entirety follows . . .


Code: Select all

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

public class face_detection {



   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 = "Capture - Face detection.jpg";

       System.out.println("4/14/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("Error loading face cascade");
       }
       else
       {
           System.out.println("Success loading face cascade");
       }

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

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

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

       // 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++)
       {
          System.out.println("                      Face Count    " + facecount);
           facecount += 1;
           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++)
            {
               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();
       }

}

clplaneguy
Novice
Posts: 29
Joined: Sat Feb 18, 2017 3:44 pm

Re: Does Cascade Classifier work in OpenCV 2.4.11?

Postby clplaneguy » Mon Apr 16, 2018 4:12 pm

I believe that OpenCV 2.4.11 and its Native Library have been installed correctly according to the instructions. But, it is installed into the jar and not the folder.

User avatar
gloomyandy
leJOS Team Member
Posts: 6047
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Does Cascade Classifier work in OpenCV 2.4.11?

Postby gloomyandy » Mon Apr 16, 2018 5:41 pm

Unless you really know what you are doing, having two versions of something like openCV installed is likely to cause issues or at the very least confusion. Do you really need 3.4.1? I suspect things will be much simpler if you just use the same version on the PC and your EV3. If you must use two different versions then you will need to take a great deal of care to make sure that your eclipse system (and possibly your containing environment), correctly references the correct version both when compiling your program (by the way what you have is a program, not a script) and when executing it. There is also the added complication of your code needing to use slightly different methods and classes due to the changes in openCV. If you have any intention of sharing data between a program running on the PC and EV3 then I would definitely ensure that you use the same openCV version on both systems. Personally if I really, really had to mix versions (and I would try very hard not to), I would create a virtual machine using something like VirtualBox and set up a separate build environment for the EV3 in that, this would ensure that the two versions do not get mixed up.

Oh and as so far your problems have nothing to do with leJOS, you may get better answers from an openCV group, they are more likely to have seen the issues you are having before.
leJOS news https://lejosnews.wordpress.com/

clplaneguy
Novice
Posts: 29
Joined: Sat Feb 18, 2017 3:44 pm

SOLVED :Does Cascade Classifier work in OpenCV 2.4.11?

Postby clplaneguy » Mon Apr 16, 2018 9:42 pm

SOLVED

THANK YOU

Yes, of course my mentor is perfectly correct.

I have finished phase 1, the latest and greatest of everything (Java 10 and OpenCV 3.4.1 on a PC) and I am now in phase two (Java 1.7 (7) and OpenCV 2.4.11 on a PC). I mistakenly thought that this transition would be quick. But, it has ended up being a sticking point. There are undoubtedly differences between 2.4.11 and 3.4.1!

OpenCV 2.4.11 Core.ellipse Becomes OpenCV 3.4.1 Imgproc.ellipse
OpenCV 2.4.11 Core.circle Becomes OpenCV 3.4.1 Imgproc.circle
And HighGui is not implemented in OpenCV 2.4.11

It is my intent to NOT use different versions of software in the same ECLIPSE PROJECT. It is my understanding (and experience) that in Eclipse each PROJECT has its own settings. So, my phase 1 ECLIPSE PROJECT uses Java 10 and OpenCV 3.4.1 exclusively. My phase 2 ECLIPSE PROJECT uses Java 1.7 (7) and OpenCV 2.4.11 exclusively. They are different PROJECTs in Eclipse.

Now that I am firmly imbedded in phase 2, I will use the OpenCV forum.


Return to “EV3 Software”

Who is online

Users browsing this forum: No registered users and 1 guest