Example usage for org.opencv.objdetect CascadeClassifier detectMultiScale2

List of usage examples for org.opencv.objdetect CascadeClassifier detectMultiScale2

Introduction

In this page you can find the example usage for org.opencv.objdetect CascadeClassifier detectMultiScale2.

Prototype

public void detectMultiScale2(Mat image, MatOfRect objects, MatOfInt numDetections) 

Source Link

Usage

From source file:io.github.leonardosnt.eyedetector.EyeDetector.java

License:Open Source License

public static String detect(byte[] imgData) throws IOException {
    File tempImage = File.createTempFile("tmpImg", "");

    JsonObject jsonResponse = Json.object();
    JsonArray eyesJsonArray = (JsonArray) Json.array();
    jsonResponse.add("eyes", eyesJsonArray).toString();

    try {//from   w  w  w  .j a  v a2  s.c  o  m
        Files.write(tempImage.toPath(), imgData);

        CascadeClassifier eyeClassifier = new CascadeClassifier(EYE_CASCADE_PATH);
        Mat image = Imgcodecs.imread(tempImage.getAbsolutePath());

        MatOfRect eyeDetections = new MatOfRect();
        MatOfInt numDetections = new MatOfInt(1);

        eyeClassifier.detectMultiScale2(image, eyeDetections, numDetections);

        Rect[] eyes = eyeDetections.toArray();

        if (eyes.length == 0) {
            return jsonResponse.toString();
        }

        eyesJsonArray.add(Json.object().add("x", eyes[0].x).add("y", eyes[0].y));
        eyesJsonArray.add(Json.object().add("x", eyes[1].x).add("y", eyes[1].y));
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        tempImage.delete();
    }
    return jsonResponse.toString();
}