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, I am trying to make a robot with face detection on the Lego EV3. The Lego EV3 uses leJOS and OpenCV 2.7.11! The script does not complete with OpenCV 2.7.11! It halts on line 34.
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?
My script 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.highgui.VideoCapture;
import org.opencv.highgui.*;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
//import org.opencv.videoio.VideoCapture;
public class face_detection {
public static void main(String[] args) {
System.out.println("4/15/2018");
System.out.println("OpenCV 2.7.11");
System.out.println("My Casscade");
System.out.println("capture through camera: "+Core.VERSION);
//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";
// 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 count = 0;
while ( capture.read(frame) )
{
System.out.println(count);
count += 1;
if( frame.empty() )
{
System.out.println("No captured frame");
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++)
{
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);
//Imgproc.circle(frame, center1, radius, new Scalar(255, 0, 0), 4, 8, 0);
}
}
//HighGui.imshow(window_name, frame);
//HighGui.waitKey(1);
}
capture.release();
}
}