Ask Your Question

DetectFaceDemo only working on one picture [closed]

asked 2016-11-10 01:00:20 -0500

aliyesami gravatar image

updated 2016-11-10 10:39:42 -0500



only eye detected

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
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");
    // 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.
    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
// 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] 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/
[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
// 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 ...
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by aliyesami
close date 2016-11-19 19:02:14.140138


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.

StevenPuttemans gravatar imageStevenPuttemans ( 2016-11-10 03:05:58 -0500 )edit

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.

aliyesami gravatar imagealiyesami ( 2016-11-10 08:27:32 -0500 )edit

just to make sure: please always check, if resources loaded, like ! image.empty() and ! faceDetector.empty() (the java code will just fail silently, unfortunately)

berak gravatar imageberak ( 2016-11-10 09:40:20 -0500 )edit

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 ?

aliyesami gravatar imagealiyesami ( 2016-11-10 09:45:47 -0500 )edit

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 .

aliyesami gravatar imagealiyesami ( 2016-11-10 09:47:09 -0500 )edit

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.

berak gravatar imageberak ( 2016-11-10 09:56:20 -0500 )edit

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 ?

aliyesami gravatar imagealiyesami ( 2016-11-10 10:04:05 -0500 )edit

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.

aliyesami gravatar imagealiyesami ( 2016-11-10 10:12:03 -0500 )edit

added another picture , the program also failed to detect the complete face but detected and marked just the eye area. ?

aliyesami gravatar imagealiyesami ( 2016-11-10 10:36:27 -0500 )edit

i did not test the queen yet, but you're right about the angle (it can handle like +- 20°, but that's it)

berak gravatar imageberak ( 2016-11-10 10:38:17 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2016-11-12 01:04:33 -0500

aliyesami gravatar image

as Berak pointed out the program was not detecting the face because of the tilt in the head of more than 20 deg . so its not broken , its working as designed.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-11-10 01:00:20 -0500

Seen: 216 times

Last updated: Nov 12 '16