DetectFaceDemo only working on one picture [closed]
I am using the following tutorial DetectFaceDemo
but it only works on the lena.jpg picture that comes with the demo . I tried other pics even converting them to the same resolution and size as lena.jpg but it doesn't detect the face . how can I make it work on other pictures?
[root@hadoop1 java]# more HelloOpenCV.java
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.highgui.Highgui;
import org.opencv.objdetect.CascadeClassifier;
// Detects faces in an image, draws boxes around them, and writes the results
// to "faceDetection.png".
//
class DetectFaceDemo {
public void run() {
System.out.println("\nRunning DetectFaceDemo");
System.out.println(System.getProperty("java.class.path"));
//
//
// Create a face detector from the cascade file in the resources
// directory.
CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());
Mat image = Highgui.imread(getClass().getResource("queen.png").getPath());
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
// Draw a bounding box around each face.
for (Rect rect : faceDetections.toArray()) {
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
}
// Save the visualized detection.
String filename = "faceDetection.png";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, image);
}
}
public class HelloOpenCV {
public static void main(String[] args) {
System.out.println("Hello, OpenCV");
// Load the native library.
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
new DetectFaceDemo().run();
}
}
[root@hadoop1 java]#
see below , I am just changing the picture file name , recompiling and running the same code, with lena1.png I get "Detected 1 face" ,but with queen.png I get "Detected 0 faces"
[root@hadoop1 java]# grep png HelloOpenCV.java
// to "faceDetection.png".
Mat image = Highgui.imread(getClass().getResource("lena1.png").getPath());
String filename = "faceDetection.png";
[root@hadoop1 java]# sbt run
[info] Set current project to DetectFaceDemo (in build file:/root/openCV/opencv/samples/java/sbt/src/main/java/)
[info] Running HelloOpenCV
[info] Hello, OpenCV
[info]
[info] Running DetectFaceDemo
[info] /root/openCV/opencv/samples/java/sbt/src/main/java/target/scala-2.11/classes:/root/openCV/opencv/samples/java/sbt/src/main/java/lib/libopencv_java249.so:/root/openCV/opencv/samples/java/sbt/src/main/java/lib/opencv-249.jar:/root/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.11.6.jar
[info] Detected 1 faces
[info] Writing faceDetection.png
[success] Total time: 1 s, completed Nov 10, 2016 10:08:29 AM
[root@hadoop1 java]# grep png HelloOpenCV.java
// to "faceDetection.png".
Mat image = Highgui.imread(getClass().getResource("queen.png").getPath());
String filename = "faceDetection.png";
[root@hadoop1 java]# sbt compile
[info] Set current project to DetectFaceDemo (in build file:/root/openCV/opencv/samples/java/sbt/src/main/java/)
[info] Compiling 1 Java source to /root/openCV ...
Sorry but if it only works on lena, then you must do something wrong. The model gives output on about every single image I provide to it... especially with the default settings.
how can I debug ? I have pasted my code , as you see I am using a file called "queen.png" , it generates the new file but it doesn't show the rectangle around the face . If I replace the "queen.png" with "lena.png" it does show the rectangle.
just to make sure: please always check, if resources loaded, like
! image.empty()
and! faceDetector.empty()
(the java code will just fail silently, unfortunately)here I proved that it has to be the file because I didn't modify the code and just replace the file queen.png with lena1.png and it detects the face . i have uploaded the image file so you can test if you can read it ?
hi Berak the code is not failing though , like I said even without modifying the code if I just replace the files it either detects the face or not .
again the code won't fail anywhere, if it did not find some resource. it might not be the problem, still make it a habit of checking.
sure will do! in meantime I downloaded a picture of Donald Trump and it works. so something wrong with the English queen :) can someone try to use this picture (I uploaded it ) and see why this program does not detect face on it ?
also is it supposed to only detect faces in upright position ? I rotated the Donald Trump image 90 deg and then it only draws a small rectangle around the eye area and not the complete face , please see attached picture.
added another picture , the program also failed to detect the complete face but detected and marked just the eye area. ?
i did not test the queen yet, but you're right about the angle (it can handle like +- 20°, but that's it)