实时分配唯一的面部ID

时间:2017-09-05 07:19:47

标签: python opencv dlib

我使用以下代码为每个人生成一个id。它的工作原理部分但问题是当更多的人进来时,他们每个人都得到相同的身份。让我们说如果共有3个人,那就是将id 3分配给每个人。我希望它在增量顺序中是唯一的。我该如何解决这个问题?

{{1}}

1 个答案:

答案 0 :(得分:3)

仔细查看您的代码:

for i, d in enumerate(detected):
        x1, y1, x2, y2, w, h = d.left(), d.top(), d.right() + 1, d.bottom() + 1, d.width(), d.height()
        cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
        cv2.putText(img, str(user_id), (x1, y1), font, 0.5, (255, 255, 255), 1, cv2.LINE_AA)

cv2.putText正在为其绘制的每个矩形上写user_id

for循环的范围内,您没有更新user_id参数,因此for循环在所有矩形上写入相同的常量值。

您应该在此for循环中增加您希望在矩形上看到的值

例如:

for i, d in enumerate(detected):
            x1, y1, x2, y2, w, h = d.left(), d.top(), d.right() + 1, d.bottom() + 1, d.width(), d.height()
            cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
            cv2.putText(img, 'user_'+str(i), (x1, y1), font, 0.5, (255, 255, 255), 1, cv2.LINE_AA)

现在与user_id不同,值ifor循环的每次迭代中递增,因此cv2.putText将为每次迭代打印递增的值,这应该足够了你的要求

相关问题