OpenCV Face recognition Java code terminates JVM

1

I have the following code for detecting faces in an image using OpenCV taken and adapted from here:

import org.junit.Test;
import org.opencv.core.*;
import org.opencv.objdetect.CascadeClassifier;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;

import static org.opencv.imgcodecs.Imgcodecs.imread;
import static org.opencv.imgcodecs.Imgcodecs.imwrite;
import static org.opencv.imgproc.Imgproc.rectangle;

public class FaceRecognizer
{
    @Test
    public void recognizeFace() throws IOException
    {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        // Create a face detector from the cascade file in the resources directory.
        ClassPathResource classifierResource = new ClassPathResource("lbpcascade_frontalface.xml");
        CascadeClassifier faceDetector = new CascadeClassifier(classifierResource.getFile().getAbsolutePath());
        ClassPathResource classPathResource = new ClassPathResource("lena.png");
        Mat image = imread(classPathResource.getFile().getAbsolutePath());

        // 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())
        {
            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));
        imwrite(filename, image);
        //FaceRecognizer fr;//= new LBPHFaceRecognizer();
    }
}

When I run it, it terminates the JVM early:

Process finished with exit code -1073740791 (0xC0000409)

This happens at the

faceDetector.detectMultiScale(image, faceDetections);

line.

The image I used is the following (it's a well known example image):

Also the lbpcascade_frontalface.xml file is part of the OpenCV release pack.

Why does it crash? Is there something wrong with the code? I use OpenCV 3.4.1 and other OpenCV code samples work.

java
opencv
image-recognition
face-recognition
asked on Stack Overflow Aug 14, 2018 by BullyWiiPlaza • edited Aug 14, 2018 by BullyWiiPlaza

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0