简体   繁体   中英

Mouth detection using openCv returns a lot of circles in only one face

I'm using OpenCv with android studio to detect faces in an image along with the eyes and the mouth in each face. But, the problem is whenever I try to detect the mouth it returns multiple circles in a face which is wrong.

Here is the code I added for mouth detection:

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
...
InputStream iserM = getResources().openRawResource(
                                R.raw.haarcascade_mcs_mouth);
                        File cascadeDirERM = getDir("cascadeERM",
                                Context.MODE_PRIVATE);
                        File cascadeFileERM = new File(cascadeDirERM,
                                "haarcascade_mcs_mouth.xml");
                        FileOutputStream oserM = new FileOutputStream(cascadeFileERM);

                        byte[] bufferERM = new byte[4096];
                        int bytesReadERM;
                        while ((bytesReadERM = iserM.read(bufferERM)) != -1) {
                            oserM.write(bufferERM, 0, bytesReadERM);
                        }
                        iserM.close();
                        oserM.close();
...
//here begins
                        mJavaDetectorMouth = new CascadeClassifier(
                                cascadeFileERM.getAbsolutePath());
                        if (mJavaDetectorMouth.empty()) {
                            Log.e(TAG, "Failed to load cascade classifier");
                            mJavaDetectorMouth = null;
                        } else
                            Log.i(TAG, "Loaded cascade classifier from "
                                    + mCascadeFile.getAbsolutePath());
                        //here ends
...
}

public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
...
Rect r = facesArray[i];
            MatOfRect mouths = new MatOfRect();
            Mat faceROI = mRgba.submat(facesArray[i]);
            mJavaDetectorMouth.detectMultiScale(faceROI, mouths,1.1,1,1, new org.opencv.core.Size(30, 30), new org.opencv.core.Size());
            Rect[] mouthArray = mouths.toArray();

            for (int j = 0; j < mouthArray.length; j++){
                Point center1 = new Point(facesArray[i].x + mouthArray[j].x + mouthArray[j].width * 0.5,
                        facesArray[i].y + mouthArray[j].y + mouthArray[j].height * 0.5);
                int radius = (int) Math.round(mouthArray[j].width / 2);
                Imgproc.circle(mRgba, center1, radius, new Scalar(255, 0, 0), 4, 8, 0);
            }
...
}

I've been looking around since I posted the question and tried a lot of things. But finally I found the solution: I changed:

 mJavaDetectorMouth.detectMultiScale(faceROI, mouths,1.1,1,1, new org.opencv.core.Size(30, 30), new org.opencv.core.Size());

To:

 mJavaDetectorMouth.detectMultiScale(faceROI, mouths,1.1, 2,
                    Objdetect.CASCADE_FIND_BIGGEST_OBJECT
                            | Objdetect.CASCADE_SCALE_IMAGE, new org.opencv.core.Size(30, 30), new org.opencv.core.Size());

and I solved the issue.

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