Using OpenCV detectMultiScale to find my face
Asked Answered
V

1

6

I'm pretty sure I have the general theme correct, but I'm not finding any faces. My code reads from c=cv2.VideoCapture(0), i.e. the computer's videocamera. I then have the following set up to yield where the faces are. As you can see, I'm looping through different scaleFactors and minNeighbors but rects always comes back empty. I've 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)
Vuong answered 11/5, 2013 at 1:55 Comment(0)
C
6

I changed your code a little in order to make it run on my pc. When I run is at such 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 advice: Don't pick your minSize too low ... else every small item which resembles a face will be detected.

I assume you are running through all these parameters to find the ones that are the best. I found out the minNeighors shouldn't be too high, else it won't find any.

Make sure your cascade xml file is linked to correctly. If it doesn't find it, it won't give an error, it will just find no faces.

Chicoine answered 13/5, 2013 at 8:27 Comment(7)
Thanks for trying this, although I don't see where your posted code is different from mine aside from a little bit of refactoring. The file location is correct - I can copy it out, cat it, and the full xml file is printed.Vuong
I didn't change anything about your 2 functions. I just changed the main. You're not getting any rects ?Chicoine
Surprisingly not. I have a beard, but it's not catching my friend either.Vuong
Have you displayed the rawstream of your camera ? Maybe you're using the wrong channel ?Chicoine
Yes, I'm using imshow to open the stream so that I can see what it's viewing. I'm also opening the gray in the same way to see what that's seeing. Ultimately, my problem is that I'm not sure how to debug this.Vuong
depends on your IDE. If you have no rects, you haven't found a face (or anything resembling a face). Does the facedetect.py in the opencv sample folder work ? (It's a standard example for face detection)Chicoine
Woah, it did work. Looking into the difference now. Thanks for pointing me that wayVuong

© 2022 - 2024 — McMap. All rights reserved.