使用Python选择第一个面OpenCV detectMultiScale

2024-04-25 06:21:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在测试OpenCV来检测人脸,我想知道怎样才能有效地只检测第一张人脸?你知道吗

下面的代码适用于多个面,但如果我在面[0]上执行for循环,应用程序会抱怨:

for (x,y,w,h) in faces[0]:
TypeError: 'numpy.int32' object is not iterable


if len(faces) == 0:
        print('the list is empty', datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
    else:
        print('the list is NOT empty', 'Detected',len(faces),'Face(s)')
        print(faces)

        for (x,y,w,h) in faces:
            cv.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
            roi_color = img[y:y+h, x:x+w]  

    cv.imshow('Facial Recognition', img)

Tags: the代码in应用程序imgforlenis
2条回答

您不能迭代面[0],因为它不是数组它将是一个单一的值您只需迭代循环一次,并在末尾中断,以仅显示检测到的第一个面

面[0]只是一个面,因此不能在其上循环。你知道吗

if len(faces) == 0:
    print('the list is empty', datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
else:
    print('the list is NOT empty', 'Detected',len(faces),'Face(s)')
    print(faces)

    face = faces[0]
    (x,y,w,h) = face 
    cv.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
    roi_color = img[y:y+h, x:x+w]  

cv.imshow('Facial Recognition', img)

相关问题 更多 >