使用OpenCV Java更快速的人脸检测代码

时间:2017-11-28 21:48:21

标签: java opencv

对于我的大学项目工作,我编写了一个代码来检测图像中的面部,并使用OpenCV Java将面部裁剪到不同的文件夹中。在代码中我使用了“CascadeClassifier”。使用此代码我已检测到面部。但是,是否可以改进代码,以便可以更快地检测代码。例如,从5.jpg(分辨率1280 * 720)检测5张脸需要1.1520934秒。我的电脑有英特尔核心i-5 @ 2.20 GHz。是否有可能更快地检测到脸部? Opencv代码

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.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

public class DetectFaceDemo 
{

    public void run() 
    {
        // TODO Auto-generated method stub
        System.out.println("\nRunning DetectFaceDemo");

        // Create a face detector from the cascade file in the resources
        // directory.
        long startTime = System.nanoTime();
    //    CascadeClassifier faceDetector = new CascadeClassifier("D:\\opencv\\sources\\data\\lbpcascades\\lbpcascade_frontalface.xml");
        CascadeClassifier faceDetector = new CascadeClassifier("D:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");
//       CascadeClassifier faceDetector = new CascadeClassifier("D:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_default.xml");


//      Mat image = Highgui.imread("d:\\face image\\aaa05.jpg");
        Mat image = Highgui.imread("D:\\photo test gallery_HD_2\\5.jpg");
       float size = Math.round(image.height() * 0.1f);
       //System.out.println("####= "+ size); 
        Rect rectCrop = null;
        // Detect faces in the image.
        // MatOfRect is a special container class for Rect.
        MatOfRect faceDetections = new MatOfRect();
        faceDetector.detectMultiScale(image, faceDetections);
//      faceDetector.detectMultiScale(image, faceDetections, 1.1, 3, 0, new Size(90, 90),new Size(400, 400));


        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, 0, 255));

            rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height);  

            Core.putText(image, "Edited by me", new Point(rect.x,rect.y),
                    Core.FONT_HERSHEY_PLAIN, 1.0 ,new  Scalar(0,255,255));
        }


        // crop individual image
        int counter=0;
        for (Rect rect : faceDetections.toArray())
        {
            Mat faceImage = image.submat(rect);
            Mat resizeimage = new Mat();
            Size sz = new Size(64, 128);
            Imgproc.resize(faceImage, resizeimage, sz);
            System.out.println(counter);
            Highgui.imwrite("D:\\image\\appface\\"+ counter+ ".jpg", resizeimage);
            counter++;
            System.out.println(";)");

        }

        //long stopTime = System.nanoTime();
        //float time_diff = stopTime - startTime;
        //System.out.println("timefacedetect-s:"+ time_diff/1000000000);

        // Save the visualized detection.
        String filename = "d:\\faceDetection.jpg";
        System.out.println(String.format("Writing %s", filename));
        Highgui.imwrite(filename, image);

    }

}

示例图片文件: https://drive.google.com/open?id=1kQHyF3E-pYMy1IESn8yzRs_6J4fZq_zi

0 个答案:

没有答案