Reputation: 5001
I'm testing out OpenCV to detect faces and was wondering how can i effectively detect the first faces only?
the code below works for multiple but if I do a for loop on faces[0], the app complains with this:
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)
Upvotes: 0
Views: 397
Reputation: 2917
faces[0] is only one face so you cannot loop over it.
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)
Upvotes: 1
Reputation: 25
You cannot iterate faces[0] cause it is not array it will be a single value you just iterate the loop once and break at the end to display only the first face detected
Upvotes: 0