使用Python选择第一个面OpenCV detectMultiScale
我正在测试OpenCV来检测人脸,我想知道如何才能有效地只检测第一张人脸 下面的代码适用于多个面,但如果我在面[0]上执行for循环,应用程序会抱怨:使用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
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)