6

我很确定我的总体主题是正确的,但我没有找到任何面孔。我的代码读取自c=cv2.VideoCapture(0),即计算机的摄像机。然后,我进行了以下设置以产生面部所在的位置。如您所见,我正在循环遍历不同的 scaleFactors 和 minNeighbors 但 rects 总是返回空。我还尝试了 opencv/data/haarcascades 包中包含的四个不同的 haarcascade xml 文件。

有小费吗?

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

1 回答 1

6

我稍微更改了您的代码以使其在我的电脑上运行。当我跑步时,我会得到结果

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

输出:

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

一些建议:不要把你的 minSize 选得太低……否则每一个像人脸的小项目都会被检测到。

我假设您正在运行所有这些参数以找到最好的参数。我发现 minNeighors 不应该太高,否则找不到。

确保您的级联 xml 文件正确链接。如果它没有找到它,它不会给出错误,它只会找不到任何面孔。

于 2013-05-13T08:27:46.683 回答