Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python选择第一个面OpenCV detectMultiScale_Python_Opencv - Fatal编程技术网

使用Python选择第一个面OpenCV detectMultiScale

使用Python选择第一个面OpenCV detectMultiScale,python,opencv,Python,Opencv,我正在测试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

我正在测试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)

无法迭代面[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)