使用OpenCV detectMultiScale查找我的脸 [英] Using OpenCV detectMultiScale to find my face

查看:182
本文介绍了使用OpenCV detectMultiScale查找我的脸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

有什么提示吗?

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)

推荐答案

为了使代码在我的PC上运行,我做了一些更改. 当我跑到这样的速度时,我会得到结果

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

输出:

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选择得太低...否则将检测到每张类似于面孔的小物件.

Some advice: Don't pick your minSize too low ... else every small item which resembles a face will be detected.

我假设您正在遍历所有这些参数以查找最佳参数.我发现minNeighors不能太高,否则找不到.

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.

确保您的级联xml文件已正确链接.如果找不到,就不会出错,只会发现没有面孔.

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.

这篇关于使用OpenCV detectMultiScale查找我的脸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆