Using OpenCV detectMultiScale to find my face

I am sure that my common theme is correct, but I do not find any faces. My code is read from c=cv2.VideoCapture(0) , i.e. a computer video camera. Then I get the following setting to get where the faces are. As you can see, I iterate over various scaleFactors and minNeighbors, but corrections always return empty. I also tried each of the four different haarcascade XML files included in the opencv / data / haarcascades package.

Any tips?

 while(1): ret, frame = c.read() rects = find_face_from_img(frame) def detect(img, cascade): for scale in [float(i)/10 for i in range(11, 15)]: for neighbors in range(2,5): rects = cascade.detectMultiScale(img, scaleFactor=scale, minNeighbors=neighbors, minSize=(20, 20), flags=cv2.cv.CV_HAAR_SCALE_IMAGE) print 'scale: %s, neighbors: %s, len rects: %d' % (scale, neighbors, len(rects)) def find_face_from_img(img): gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.equalizeHist(gray) rects = detect(gray, cascade) 
+6
source share
1 answer

I changed my code a bit to make it work on my computer. When I run, I get results

 import cv2 import cv2.cv as cv import getopt, sys def detect(img, cascade): for scale in [float(i)/10 for i in range(11, 15)]: for neighbors in range(2,5): rects = cascade.detectMultiScale(img, scaleFactor=scale, minNeighbors=neighbors, minSize=(20, 20), flags=cv2.cv.CV_HAAR_SCALE_IMAGE) print 'scale: %s, neighbors: %s, len rects: %d' % (scale, neighbors, len(rects)) def find_face_from_img(img): gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.equalizeHist(gray) rects = detect(gray, cascade) if __name__ == '__main__': args, video_src = getopt.getopt(sys.argv[1:], '', ['cascade=', 'nested-cascade=']) try: video_src = video_src[0] except: video_src = 0 args = dict(args) cascade_fn = args.get('--cascade', "cascades/haarcascade_frontalface_alt.xml") cascade = cv2.CascadeClassifier(cascade_fn) c=cv2.VideoCapture(0) while(1): ret, frame = c.read() rects = find_face_from_img(frame) if 0xFF & cv2.waitKey(5) == 27: break 

Output:

 scale: 1.2, neighbors: 2, len rects: 1 scale: 1.2, neighbors: 3, len rects: 1 scale: 1.2, neighbors: 4, len rects: 1 scale: 1.3, neighbors: 2, len rects: 1 scale: 1.3, neighbors: 3, len rects: 1 scale: 1.3, neighbors: 4, len rects: 0 scale: 1.4, neighbors: 2, len rects: 1 scale: 1.4, neighbors: 3, len rects: 0 scale: 1.4, neighbors: 4, len rects: 0 scale: 1.1, neighbors: 2, len rects: 1 scale: 1.1, neighbors: 3, len rects: 1 scale: 1.1, neighbors: 4, len rects: 1 scale: 1.2, neighbors: 2, len rects: 1 scale: 1.2, neighbors: 3, len rects: 1 scale: 1.2, neighbors: 4, len rects: 1 scale: 1.3, neighbors: 2, len rects: 1 

Some tips: do not choose too small minSize ... otherwise, every small object that looks like a face will be detected.

I assume that you are looking through all of these options to find which ones are the best. I found out that minNeighors should not be too high, otherwise he will not find.

Make sure your cascade xml file is linked correctly. If he does not find him, he will not give an error, he simply will not find any faces.

+6
source

All Articles