package com.yida.data.user.util;

/**
 * TODO
 *
 * @author ZYJ
 * @date 2022/8/19
 */

import nu.pattern.OpenCV;
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

public class FaceTest {
    public static void main(String[] args) {

        //Loading the OpenCV core library
//        System.load("D:\\opencv_java342.dll");
//        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        OpenCV.loadLocally();

        //Reading the Image from the file and storing it in to a Matrix object
        String file = "D:/1.jpg";
        Mat src = Imgcodecs.imread(file);

        //Instantiating the CascadeClassifier
        String xmlFile = "D:/face.xml";
        CascadeClassifier classifier = new CascadeClassifier(xmlFile);

//        Mat rgb = new Mat();
//        // 灰度化
//        Imgproc.cvtColor(src, rgb, Imgproc.COLOR_BGR2RGB);
        //Detecting the face in the snap
        MatOfRect faceDetections = new MatOfRect();
        classifier.detectMultiScale(src, faceDetections);
        System.out.println(String.format("Detected %s faces",
                faceDetections.toArray().length));

        //Drawing boxes
        for (Rect rect : faceDetections.toArray()) {
            Imgproc.rectangle(src,       //where to draw the box
                    new Point(rect.x, rect.y),   //bottom left
                    new Point(rect.x + rect.width, rect.y + rect.height),  //top right
                    new Scalar(0, 0, 255),
                    3);    //RGB color
        }
        //Writing the image
        Imgcodecs.imwrite("D:/output1.jpg", src);

        System.out.println("Image Processed");
    }
}
