eran
eran

Reputation: 15136

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

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?

Upvotes: 10

Views: 3772

Answers (3)

Mehran Rezaei
Mehran Rezaei

Reputation: 1

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)

Upvotes: 0

Kirill Fedyanin
Kirill Fedyanin

Reputation: 712

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)

Upvotes: 1

Zifei Tong
Zifei Tong

Reputation: 1727

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 );

Upvotes: 1

Related Questions