DetectFaceDemo only working on one picture
C:\fakepath\queen.png DONALD-ROTATED
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/opencv/samples/java/sbt/src/main/java/target/scala-2.11/classes...
[success] Total time: 1 s, completed Nov 10, 2016 10:09:22 AM
[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 0 faces
[info] Writing faceDetection.png
[success] Total time: 1 s, completed Nov 10, 2016 10:09:29 AM
[root@hadoop1 java]#