为每个面孔分配ID [英] Assign id for each faces

查看:92
本文介绍了为每个面孔分配ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码检测人脸.检测后,Im在面部周围绘制矩形.然后,我分配人脸的ID,即人脸编号.如果有两张脸,则一张脸的id为1,另一张脸的ID为2.下面是我的代码.

Im using the following code to detect faces. After detection, Im drawing rectangle around the face. Then Im assigning id of the face which is the face number. If there are two faces,one face will have face id as 1 and the other one as 2.Following is my code.

detections = face_cascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=5,
    minSize=(30, 30),
    flags=cv2.CASCADE_SCALE_IMAGE
)

# Draw a rectangle around the faces
for (x, y, w, h) in detections:
    cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

    face_no = detections.shape[0];

    cv2.putText(frame, str(face_no), (x, y - 30), cv2.FONT_HERSHEY_TRIPLEX,
                .7, (0, 0, 0), 1, cv2.LINE_AA)

但是此代码始终返回面孔总数.如何获得每张脸的编号?

But this code always returns the total number of faces. How can I get number for each face?

推荐答案

如果使用detections.shape[0],则每次都将获得相同的值,因为其中没有变量会根据循环而变化.您可以做的是使用enumerate标记面孔,这会将face_no设置为循环迭代编号:

If you use detections.shape[0] you are going to get the same value every time since there is no variable in there that changes based on the loop. What you could do is label the faces as you go using enumerate which will set face_no to the loop iteration number:

for face_no, (x, y, w, h) in enumerate(detections):
    cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

    cv2.putText(frame, str(face_no), (x, y - 30), cv2.FONT_HERSHEY_TRIPLEX,
                .7, (0, 0, 0), 1, cv2.LINE_AA)

这篇关于为每个面孔分配ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆