gpt4 book ai didi

python - 使用 OpenCV detectMultiScale 找我的脸

转载 作者:太空狗 更新时间:2023-10-29 23:59:40 25 4
gpt4 key购买 nike

我很确定我的一般主题是正确的,但我没有找到任何面孔。我的代码从 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)

最佳答案

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

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 文件链接正确。如果找不到,它不会报错,它只会找不到人脸。

关于python - 使用 OpenCV detectMultiScale 找我的脸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16493008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com