Let OpenCV output face detection score (confidence rate)

Source: Internet
Author: User

A little more recent projects, one of which needs to find some negative samples that look like a face but not a face, and want to use the OPENCV face detector to detect false faces as such negative samples.

But at home (including abroad) there is virtually no relevant information on how to output the confidence rate of detectmultiscale () or the human face score

So write a small summary for people with relevant needs for reference.

Reprint must indicate: http://www.cnblogs.com/sciencefans/

See the source of the OpenCV of the human face recognition function

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

There are two overloads in Detectmultiscale, and the second overload is not mentioned in the development documentation for OPENCV:

 void  cascadeclassifier::d Etectmultiscale ( const  mat& image, Vector<rect>& objects, ve                                          ctor  <int  >& Rejectlevels, Vector  <double  >& Levelweights,  double  Scalefactor, Span style= "color: #0000ff;" >int   Minneighbors,  int   flags, size minobjectsize, size maxobjectsize,  bool  outputrejectlevels) 

Found that he had a rejectlevels and levelweight these two reference parameters, to see the name feeling is a kind of score output.

Google a bit to find abroad to ask a lot of people but basically no explanation (or I did not seriously find?) )

And then looked at the Cvhaardetectobjectsforroc () of the source code implementation, probably understand what these two vectors is doing.

The first conclusion: it really has to do with the human face score.

First you should understand that a little detectmultiscale () This method is a cascade classifier, using the boosting method. So the input image to go through layers (level) selection, left to the last is the Real Man (positive sample)

Rejectlevels is the representation of being out on the first floor. If it is the last layer (20 in lbpcascade_frontalface.xml, which depends on the narrative in the XML), it is probably a positive sample.

Why is that possible?

Because there is another parameter: Levelweight. Even if the last layer is out, the levelweight is small or even negative, and can be considered a negative sample.

In fact, a lot of negative samples are out at the last layer.

See:

I've only intercepted a box with level 20. The output of their levelweight. Is the face of the largest place is more than 4.23, the other is very small. Don't explain it too much.

So the principle of this function is this (personal understanding, there are errors please advise):

First, a level to test the sample, and then each level to a corresponding score, that is, levelweight, if the weight is lower or higher than the corresponding level of threshold, is discarded.

The box that sticks to the last level and still satisfies threshold at the last level is the correct face (positive sample).

Therefore, the human face should be the same: the higher the level, the higher the score, the higher the score in the same level,levelweight.

But actually the real face can stick to the LEVEL20 (the last level), so only compared to the last level of all the boxes greater than 1 levelweight to be able to know the score of the face ~

Here is a picture of all level GG frames:

Finally, we give the source code of the Gray and concise demo:

1#include <opencv2\opencv.hpp>2#include <iostream>3#include <vector>4#include <fstream>5#include <math.h>6 using namespacestd;7 using namespaceCV;8 Const stringXmlpath ="Lbpcascade_frontalface.xml";9 cascadeclassifier face_cc;Ten  One intTIC =0; A  - voidDetect (Mat img) { -Vector<rect>faces; thevector<int>Rejlevel; -vector<Double>Levelw; - Mat grayimg; - Cvtcolor (IMG, grayimg, cv_rgb2gray); + equalizehist (grayimg, grayimg); -     intMinl =min (img.rows, img.cols); +Face_cc.detectmultiscale (Grayimg, Faces, Rejlevel, LEVELW,1.1,3,0, size (), size (),true); A     //Face_cc.detectmultiscale (Grayimg, faces, 1.1); at      for(inti =0; I < faces.size (); i++ ) -     { -         if(Rejlevel[i] <xx ) -         { -             Continue; -         } in StringStream Text1, Text2; -Text1 <<"Rejlevel:"<<rejlevel[i]; toText2 <<"LEVELW:"<<levelw[i]; +         stringTTT =text1.str (); -Rectangle (img, faces[i], Scalar (255,255,0),2,8,0); thePuttext (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 + A),1,1, Scalar (255,0,255));Panax Notoginseng     } -Imshow ("IMG", IMG); theWaitkey (0); + } A  the intMain () { +     if( !face_cc.load (xmlpath)) -     { $cout <<"Load error!\n"; $         return-1; -     } - ifstream Pathin; thePathin.open ("Imgpath.txt"); -     stringT;Wuyi      while(Pathin >> t && tic <10000) the     { -Mat img =Imread (t); Wu Detect (IMG); -     } About pathin.close (); $     return 0; -}

Let OpenCV output face detection score (confidence rate)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.