实时分配唯一的人脸ID [英] Assigning unique face id realtime
问题描述
我使用以下代码生成每个人的ID.它部分起作用,但是问题是,当更多的人进来时,他们每个人都获得相同的ID.可以说如果总共有3个人,则将id 3分配给每个人.我希望它按递增顺序是唯一的.我该如何解决呢?
Im using the following code to generate each person an id. It works partially however the problem is that when more people comes in, each of them are getting the same id. Lets say if there are totally 3 persons, it is assigning the id 3 to everyone. I want it to be unique in incremental order. How can I sort this out?
while True:
ret, img = cap.read()
input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
detected = detector(input_img, 1)
current_viewers = len(detected)
if current_viewers > last_total_current_viewers:
user_id += current_viewers - last_total_current_viewers
last_total_current_viewers = current_viewers
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.imshow("result", img)
key = cv2.waitKey(30)
if key == 27:
break
推荐答案
仔细查看您的代码:
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
.
cv2.putText
is writing user_id
on each of the rectangles that it is drawing.
在for
循环的范围内,您没有更新user_id
参数,因此for
循环在所有矩形上写入相同的常数值.
Within the scope of the for
loop, you're not updating the user_id
parameter, hence the for
loop is writing the same constant value on all the rectangles.
您应该增加希望在矩形上看到的值,在此for
循环本身内.
You should increment the value that you wish to see on the rectangle, within this for
loop itself.
例如:
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
不同,i
的值在for
循环的每次迭代中都会递增,因此cv2.putText
将为每次迭代打印递增的值,这足以满足您的要求
Now unlike user_id
, the value i
is incremented in every iteration of the for
loop, hence cv2.putText
will print the incremented value for each iteration, which should suffice your requirement
这篇关于实时分配唯一的人脸ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!