2017-09-05 46 views
-1

使用下面的代碼來生成每個人的ID。它的部分作用,但問題是,當更多的人進來時,他們每個人都得到相同的ID。可以說,如果總共有3個人,那麼它就將ID 3分配給每個人。我希望它按增量順序是唯一的。我怎樣才能解決這個問題?實時分配獨特的人臉ID

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 

回答

2

看看你的代碼密切:

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將打印遞增值每次迭代,這應該足夠了您的需求

+0

可以請你詳細說說嗎? –

+0

更新了答案。請看看 –

+0

然而這不會算從早上到現在的人 –