Java和Haarcascade的面部和口腔检测-以鼻子为嘴 [英] Java and haarcascade face and mouth detection - mouth as the nose

查看:137
本文介绍了Java和Haarcascade的面部和口腔检测-以鼻子为嘴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我开始测试该项目,该项目可以在Java和OpenCv中检测到微笑.为了识别面部和嘴巴项目,使用了haarcascade_frontalface_alt和haarcascade_mcs_mouth但是我不明白为什么在某些原因下项目会将鼻子当作嘴巴. 我有两种方法:

Today I begin to test the project which detects a smile in Java and OpenCv. To recognition face and mouth project used haarcascade_frontalface_alt and haarcascade_mcs_mouth But i don't understand why in some reasons project detect nose as a mouth. I have two methods:

private ArrayList<Mat> detectMouth(String filename) {
    int i = 0;
    ArrayList<Mat> mouths = new ArrayList<Mat>();
    // reading image in grayscale from the given path
    image = Highgui.imread(filename, Highgui.CV_LOAD_IMAGE_GRAYSCALE);
    MatOfRect faceDetections = new MatOfRect();
    // detecting face(s) on given image and saving them to MatofRect object
    faceDetector.detectMultiScale(image, faceDetections);
    System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
    MatOfRect mouthDetections = new MatOfRect();
    // detecting mouth(s) on given image and saving them to MatOfRect object
    mouthDetector.detectMultiScale(image, mouthDetections);
    System.out.println(String.format("Detected %s mouths", mouthDetections.toArray().length));
    for (Rect face : faceDetections.toArray()) {
        Mat outFace = image.submat(face);
        // saving cropped face to picture
        Highgui.imwrite("face" + i + ".png", outFace);
        for (Rect mouth : mouthDetections.toArray()) {
            // trying to find right mouth
            // if the mouth is in the lower 2/5 of the face
            // and the lower edge of mouth is above of the face
            // and the horizontal center of the mouth is the enter of the face
            if (mouth.y > face.y + face.height * 3 / 5 && mouth.y + mouth.height < face.y + face.height
                    && Math.abs((mouth.x + mouth.width / 2)) - (face.x + face.width / 2) < face.width / 10) {
                Mat outMouth = image.submat(mouth);
                // resizing mouth to the unified size of trainSize
                Imgproc.resize(outMouth, outMouth, trainSize);
                mouths.add(outMouth);
                // saving mouth to picture 
                Highgui.imwrite("mouth" + i + ".png", outMouth);
                i++;
            }
        }
    }
    return mouths;
}

发现微笑

private void detectSmile(ArrayList<Mat> mouths) {
        trainSVM();
        CvSVMParams params = new CvSVMParams();
        // set linear kernel (no mapping, regression is done in the original feature space)
        params.set_kernel_type(CvSVM.LINEAR);
    // train SVM with images in trainingImages, labels in trainingLabels, given params with empty samples
        clasificador = new CvSVM(trainingImages, trainingLabels, new Mat(), new Mat(), params);
        // save generated SVM to file, so we can see what it generated
        clasificador.save("svm.xml");
        // loading previously saved file
        clasificador.load("svm.xml");
        // returnin, if there aren't any samples
        if (mouths.isEmpty()) {
            System.out.println("No mouth detected");
            return;
        }
        for (Mat mouth : mouths) {
            Mat out = new Mat();
            // converting to 32 bit floating point in gray scale
            mouth.convertTo(out, CvType.CV_32FC1);
            if (clasificador.predict(out.reshape(1, 1)) == 1.0) {
                System.out.println("Detected happy face");
            } else {
                System.out.println("Detected not a happy face");
            }
        }
    }

示例:

针对该图片

正确检测到该安装:

但在其他图片中

检测到鼻子

您认为出了什么问题?

推荐答案

由于面部的比例(与眼睛之间的距离相比,从眼睛到嘴的距离太长),它很可能在您的图片上检测到错误.使用haar检测器检测口鼻不是很稳定,因此算法通常使用面部几何模型来为每个面部特征选择候选特征的最佳组合.如果没有找到候选的嘴巴,一些实现甚至可以尝试根据眼睛预测嘴巴的位置.

Most likely it detects it wrong on your picture, because of proportion of face (too long distance from eyes to mouth compared to distance between eyes). Detection of mouth and nose using haar detector isn't very stable, so algorithms usually use geometry model of face, to choose best combination of feature candidates for each facial feature. Some implementations can even try to predict mouth position based on eyes, if no mouth candidates was found.

Haar检测器目前还不是最新的,最著名的特征检测器.尝试使用可变形零件模型实现.尝试一下,他们拥有具有高效c ++优化功能的matlab代码: https://www.ics.uci.edu/~xzhu/face/

Haar detector isn't the newest and best known at this time for feature detection. Try to use deformable parts model implementations. Try this, they have matlab code with efficient c++ optimized functions: https://www.ics.uci.edu/~xzhu/face/

这篇关于Java和Haarcascade的面部和口腔检测-以鼻子为嘴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆