Ask Your Question

OpenCV Python detectMultiscale

asked 2014-05-17 12:29:04 -0500

Fox_21 gravatar image

updated 2014-05-17 15:27:16 -0500

berak gravatar image

Hi guys can you give me advice about OpenCV? When I want to print out r to see rejectLevels it only print's out empty array.

# -*- coding: utf-8 -*-

import os, sys, 
import logging, time 
import numpy as np 
import cv2 
import as cv

face_cacade = '../home/haarcascade_frontalface_default.xml'

xml = cv2.CascadeClassifier(face_cascade)

array_of_images = [] --> Some images

for image in array_of_images:         

        img   = cv2.imread(image)

        gray  = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        gray  = cv2.equalizeHist(gray)

        **r**  =  []
        x  =  []

        #cv2.CascadeClassifier.detectMultiScale(image, rejectLevels, levelWeights[, scaleFactor[, minNeighbors[, flags[, minSize[, maxSize[, outputRejectLevels]]]]]]) → objects
        faces = xml.detectMultiScale(image = img, rejectLevels = **r**, levelWeights = x ,scaleFactor=1.05, minNeighbors=1, minSize=(30, 30), flags = cv.CV_HAAR_SCALE_IMAGE)

        file_out  = open('/tmp/faces_score.txt','w+',0)

        for (x,y,w,h) in faces: 


               roi_gray  = gray[y:y+h, x:x+w]

               roi_color = img[y:y+h, x:x+w]

               print **r**, x

Can you give me some solution or experience on Opencv detectMultiScale, I looked into c++ code source and it's looking fine there, but in Python it wont work as it should be. OpenCV version: 2.4.8 Python version: 2.7.6

edit retag flag offensive close merge delete



I think you are adressing it wrong. The rejectLevels is a parameter that can be passed as an input parameter looking at the documents. The only output parameter for the detectMultiScale function is objects parameter containing all detections. C++ don't use return variables, filling it up there. However I think the wrapper for python just ignores it for now... I think a bug report is maybe a good idea!

StevenPuttemans gravatar imageStevenPuttemans ( 2014-05-19 03:51:03 -0500 )edit

I knew I read about them somewhere!

StevenPuttemans gravatar imageStevenPuttemans ( 2014-05-19 11:47:02 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2014-05-19 13:48:32 -0500

Abid Rahman K gravatar image

Right now, it is a bug in OpenCV.

But if you are using only OpenCV-Python (not Java), there is a temporary solution. I have tested it only with master branch.

Go to opencv/modules/objdetect/include/opencv2/objdetect.hpp.

Its line 200 starts as follows :

void detectMultiScale( InputArray image,

Change it to following:

 CV_WRAP_AS(detectMultiScale2) void detectMultiScale( InputArray image,

So the final function definition will look like below (especially note 3 CV_OUT macros):

 CV_WRAP_AS(detectMultiScale2) void detectMultiScale( InputArray image,
                               CV_OUT std::vector<Rect>& objects,
                               CV_OUT std::vector<int>& rejectLevels,
                               CV_OUT std::vector<double>& levelWeights,
                               double scaleFactor = 1.1,
                               int minNeighbors = 3, int flags = 0,
                               Size minSize = Size(),
                               Size maxSize = Size(),
                               bool outputRejectLevels = false );

Now compile it. You will be able to call detectMultiScale2 instead of detectMultiScale in Python as follows:

rects,r,w = cascade.detectMultiScale2(img, scaleFactor=1.3, minNeighbors=4, minSize=(30, 30), flags = cv2.CASCADE_SCALE_IMAGE,outputRejectLevels = 1)
edit flag offensive delete link more


Maybe make a pull request?

StevenPuttemans gravatar imageStevenPuttemans ( 2014-05-19 14:05:25 -0500 )edit

Thx for answer, really helped me!

Fox_21 gravatar imageFox_21 ( 2014-05-19 15:43:09 -0500 )edit

Is there any algorithm that show is image true detected or false with some percentage, on trained xml for certain face? If you have some code or experience.

Fox_21 gravatar imageFox_21 ( 2014-05-19 16:11:37 -0500 )edit

@StevenPuttemans : going to :)

Abid Rahman K gravatar imageAbid Rahman K ( 2014-05-19 20:28:28 -0500 )edit

By the way, what does this rejectLevels and levelWeights denote?

Abid Rahman K gravatar imageAbid Rahman K ( 2014-05-19 20:38:49 -0500 )edit

Hmm I never used these parameters explicitly but it sees that rejectLevels is the threshold used on each weak classifier stage, to reach the next stage based on the binary decisions. As to levelWeights it is probably the weighting of the positive and negative sampling of the adaBoost process.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-05-20 02:23:30 -0500 )edit

Question Tools


Asked: 2014-05-17 12:29:04 -0500

Seen: 6,229 times

Last updated: May 19 '14