简体   繁体   中英

How do i speed up face detection in OpenCV in Java

Im trying to make a realtime face detection programming for tracking faces in a room with a webcam. However when i am doing this it gives me not more then 4 frames per second (every 267ms). I know that other people that use OpenCV can get <20ms with using TBB/OpenMP.

I would like to get atleast 10 frames per second. Here are the actions I already have done to speed up the process:

  • I am detecting faces on a grayscale of my image.
  • I am using a scaling factor of 1.1
  • I have both minimum and maximum values set.

My question in short is: How come my code is so slow in comparison to others? Does my code not use OpenMP or TBB? How do i get my code to use it? Is it even possible in java to use OpenMP or TBB?

I am running the program on my Late 2012 MacBook (Core i7, 4Gb RAM) with windows installed.

Here is my current code:

public class FaceDetector {
    CascadeClassifier faceDetector;

    public FaceDetector(){
        faceDetector = new CascadeClassifier(new File("haarcascade_frontalface_alt.xml").getPath());
    }

    /** Image will be overwritten by an image with detected faces on it **/
    public Rect[] getFaces(Mat image){

        MatOfRect faceDetections = new MatOfRect();

        Mat mGray = image;
        Imgproc.cvtColor(image, mGray, Imgproc.COLOR_RGBA2GRAY); // Convert to grayscale
        faceDetector.detectMultiScale(image, faceDetections, 1.1, 2, 2,new Size(40, 40), new Size(400, 400));

        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));
        }

        return faceDetections.toArray();
    }   
}

Thanks in advance!

Milan van Dijck (Student at Avans University of Applied Sciences)

I don't think your code isn't using OpenMP or TBB as it stands. To use OpenMP, you would typically add something like '#pragma omp...' before your loops.

Something like:

//#pragma omp parallel for private(j)
for(j = 0; j < _eyesVec.size(); j++ )
{
    cv::rectangle(faceROIColor, Rect(_eyesVec[j].x,_eyesVec[j].y,_eyesVec[j].width,_eyesVec[j].height), 

    cv::Scalar(0,255,0),2,8,0);
}

You must also make sure you are using the opencv binaries that have OpenMP enabled (compiled with the WITH_OPENMP option)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM