Getting the confidence level of detectMultiscale in OpenCV with Python?

RonenKi picture RonenKi · Mar 11, 2015 · Viewed 7.4k times · Source

I'm using a trained opencv cascade classifier to detect hands in video frames, and would like to lower my false positive rate. Reading up on the net, I saw you can do so by accessing the rejectLevels and levelWeights information returned by the detectMultiScale method. I saw here that this is possible in C++, my question is- has anyone managed to do it in Python? A similar question was asked here but it was for an earlier version of of the detection method.

If it is possible, what is the proper syntax to call the method? If it worked for you, please mention the OpenCV version you're using. I'm on 2.4.9.

The 2.4.11 API gives the following syntax

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

So accordingly, I've tried

import cv2
import cv2.cv as cv
import time
hand_cascade = cv2.CascadeClassifier('cascade.xml')
img = cv2.imread('test.jpg')

rejectLevels = []
levelWeights = []
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = hand_cascade.detectMultiScale(gray,rejectLevels,levelWeights, 1.1, 5,cv.CV_HAAR_FIND_BIGGEST_OBJECT,(30, 30),(100,100),True)

But the output I get is

[[259 101  43  43]
 [354 217  43  43]
 [240 189  43  43]
 [316 182  47  47]
 [277 139  92  92]]
[]
[]

Thanks for the help,

Ronen

Answer

ferrouswheel picture ferrouswheel · Dec 7, 2015

For anyone coming to this question and using OpenCV 3.0 I worked out after poking around the python API.

On the cascade classifier there are three methods detectMultiScale, detectMultiScale2, and detectMultiScale3. Using the third one, I was able to get what looked like a confidence/weight.

faces = faceCascade.detectMultiScale3(
    gray,
    scaleFactor=1.1,
    minNeighbors=5,
    minSize=(30, 30),
    flags = cv2.CASCADE_SCALE_IMAGE,
    outputRejectLevels = True
)
rects = faces[0]
neighbours = faces[1]
weights = faces[2]

weights[i] looks to match the confidence of the face defined by rects[i]. neighbours[i] is the number of matches in neighbourhood of the current rectangle.