How to get a score for cv2.CascadeClassifier.detectMultiScale()?
Asked Answered
H

3

10

When using Python,

the openCV function

cv.HaarDetectObjects()

returns an object found along with a detection score.

If I use the opencv2 function instead,

cv2.CascadeClassifier.detectMultiScale()

I get the detected object, but no score. This makes it difficult to get a good "confidence" measure of the detection.

Is there a way to get that somehow, using CV2?

Holcman answered 29/11, 2012 at 7:58 Comment(1)
did you figure out how to do this? Thanks!Repentant
O
1

According the documentation

cv2.CascadeClassifier.detectMultiScale(image, rejectLevels, levelWeights[, scaleFactor[, minNeighbors[, flags[, minSize[, maxSize[, outputRejectLevels]]]]]]) → objects

The list rejectLevels is kind of scores indicating the confidence of detection result.

The corresponding (however undocumented) C++ API is:

CV_WRAP virtual void detectMultiScale( const Mat& image,
                               CV_OUT vector<Rect>& objects,
                               vector<int>& rejectLevels,
                               vector<double>& levelWeights,
                               double scaleFactor=1.1,
                               int minNeighbors=3, int flags=0,
                               Size minSize=Size(),
                               Size maxSize=Size(),
                               bool outputRejectLevels=false );
Ottilie answered 27/12, 2012 at 6:19 Comment(2)
Do you know what the level weights are for?Coastward
do you have an example of using detectMultiScale with rejectLevels and levelWeights?Voltmer
I
1

I know it's a very old question, but as there is an unanswered comment: one can use detectMultiScale3 method which accepts outputRejectLevels boolean argument and returns the confidence scores.

weights='data/haarcascades/haarcascade_frontalface_alt.xml'
face_cascade = cv2.CascadeClassifier()
face_cascade.load(cv2.samples.findFile(weights))
face_cascade.detectMultiScale3(image, outputRejectLevels=True)
Intercolumniation answered 10/8, 2021 at 14:19 Comment(0)
P
0

you can find score as a percent of weights in the range between %100 to %99 by this code:

cascade_01 = cv2.CascadeClassifier(<type here path of .xml file>)
found_object = cascade_01.detectMultiScale(image_gray, scaleFactor=1.05, minNeighbors=15, minSize=(20, 20))
    score_rejlevels= cascade_01.detectMultiScale3(image_gray, outputRejectLevels=True)
    if len(found_object) != 0:
        if len(score_rejlevels[2]) <2:
            if len(score_rejlevels[2])!=0:
                score=100-1/float(score_rejlevels[2])
                print(score)

Puccini answered 26/1, 2022 at 12:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.