Linux ·

让OpenCV输出人脸检测的得分代码(置信率)

最近项目略多,其中一个需要找出一些和脸比较像但是不是脸的负样本,想用OpenCV的人脸检测器检测到的错误脸作为这样的负样本。

但是国内(包括国外)居然几乎没有相关的资料如何输出detectMultiScale()的置信率或者说是人脸得分

所以写一篇小小的总结供有相关需求的人参考。

看了下人脸识别函数的OpenCV的源码

\sources\modules\objdetect\src\cascadedetect.cpp

 

中detectMultiScale有两个重载,第二个重载在opencv的开发文档里居然只字未提:

void CascadeClassifier::detectMultiScale( const Mat& image, vector<Rect>& objects,
                                          vector<int>& rejectLevels,
                                          vector<double>& levelWeights,
                                          double scaleFactor, int minNeighbors,
                                          int flags, Size minObjectSize, Size maxObjectSize,
                                          bool outputRejectLevels )

发现他有个rejectLevels和levelWeight这两个引用参数,看名字感觉是一种得分输出。

google了一下发现国外问的人不少但是基本没啥解释(或者是我没认真找?)

然后看了下它调用的cvHaarDetectObjectsForROC()的源码实现,大概懂了这俩vectors是在干什么的。

先上结论:确实和人脸得分有关。

首先应该明白一点detectMultiScale()这个方法是一个级联分类器,使用了boosting的方法。所以输入图像要经过层层(级级)选拔,留到最后的才是真汉子(正样本)

rejectLevels就是代表在第几层被out的。如果是最后一层(在lbpcascade_frontalface.xml中是20,具体要看xml中的叙述)被out,则说明很可能是正样本。

为啥说很可能呢?

因为还有个参数:levelWeight。即使是在最后一层被out的,levelWeight很小甚至是负数,也可以看成是负样本。

实际上很多负样本正是在最后一层被out的。

见下图:

让OpenCV输出人脸检测的得分代码(置信率) Linux 第1张

我这里只截取了level在20才out的框。输出了他们的levelWeight。是脸的地方最大是4.23多,其他的就很小。不用过多解释了吧~

所以这个函数的原理是这样的(个人理解,有错误请指教):

首先一个level一个level地测试样本,然后每一个level给一个对应的得分,也就是levelWeight,如果这个weight低于或者高于对应level的threshold,则被抛弃。

坚持到最后一个level并且在最后一个level仍然满足threshold的框就是正确的脸(正样本)。

所以,人脸的分应该是这样:level越大,分数越高,在相同的level,levelWeight越大分数越高。

但是实际上真正的人脸都是能坚持到level20(最后一个level)的,所以只比对最后一个level的所有大于1的框的levelWeight进行比对就可以知道脸的得分啦~

这里给出所有level被gg的框的图:

让OpenCV输出人脸检测的得分代码(置信率) Linux 第2张

最后给出灰常短小精悍的demo的源代码:

#include <opencv2\opencv.hpp>
#include <iostream>
#include <vector>
#include <fstream>
#include <math.h>
using namespace std;
using namespace cv;
const string xmlpath = "lbpcascade_frontalface.xml";
CascadeClassifier face_cc;

int tic = 0;

void detect(Mat img){
    vector<Rect> faces;
    vector<int> rejLevel;
    vector<double> levelW;
    Mat grayimg;
    cvtColor(img, grayimg, CV_RGB2GRAY);
    equalizeHist(grayimg, grayimg);
    int minl = min(img.rows, img.cols);
    face_cc.detectMultiScale(grayimg, faces, rejLevel, levelW, 1.1, 3, 0, Size(), Size(), true);
    //face_cc.detectMultiScale(grayimg, faces, 1.1);
    for ( int i = 0; i < faces.size(); i++ )
    {
        if ( rejLevel[i] < 00 )
        {
            continue;
        }
        stringstream text1, text2;
        text1 << "rejLevel:" << rejLevel[ i ];
        text2 << "levelW:" << levelW[ i ];
        string ttt = text1.str();
        rectangle(img, faces[ i ], Scalar(255, 255, 0), 2, 8, 0);
        putText(img, ttt, cvPoint(faces[ i ].x, faces[ i ].y - 3), 1, 1, Scalar(0,255,255));
        ttt = text2.str();
        putText(img, ttt, cvPoint(faces[ i ].x, faces[ i ].y + 12), 1, 1, Scalar(255, 0, 255));
    }
    imshow("IMG", img);
    waitKey(0);
}

int main(){
    if ( !face_cc.load(xmlpath) )
    {
        cout << "load error!\n";
        return -1;
    }
    ifstream pathin;
    pathin.open("imgpath.txt");
    string t;
    while ( pathin >> t && tic < 10000)
    {
        Mat img = imread(t);
        detect(img);
    }
    pathin.close();
    return 0;
}

--------------------------------------分割线 --------------------------------------

Ubuntu Linux下安装OpenCV2.4.1所需包 http://www.linuxidc.com/Linux/2012-08/68184.htm

Ubuntu 12.04 安装 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htm

CentOS下OpenCV无法读取视频文件 http://www.linuxidc.com/Linux/2011-07/39295.htm

Ubuntu 12.04下安装OpenCV 2.4.5总结 http://www.linuxidc.com/Linux/2013-06/86704.htm

Ubuntu 10.04中安装OpenCv2.1九步曲 http://www.linuxidc.com/Linux/2010-09/28678.htm

基于QT和OpenCV的人脸识别系统 http://www.linuxidc.com/Linux/2011-11/47806.htm

参与评论