简体   繁体   中英

select first face OpenCV detectMultiScale with Python

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)

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)

您无法迭代faces [0],因为它不是数组,它将是一个单一值,您只需迭代一次循环并在最后中断以仅显示检测到的第一张脸

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM