簡體   English   中英

如何知道 mediapipe 中鏡頭前是否有不止一張臉?

[英]How to know if there is more than one face in front of the camera in mediapipe?

我正在通過 mediapipe 運行 python 腳本來檢測人臉,我能夠提取鼻子的 x 坐標。 這些坐標將串行發送到微控制器以做出決定。 當相機前面有兩張或更多人臉時,就會出現問題,打印的 x 坐標開始在檢測到的每張人臉之間來回擺動。 我希望能夠告訴微控制器相機前面有超過 1 張面孔以知道該怎么做,但我找不到從“檢測”變量中給出的數據中提取此信息的方法。 這是我用來跟蹤鼻子的 x position 的代碼:

    # face detection

import cv2
import mediapipe as mp
import time

mp_face_detection = mp.solutions.face_detection
mp_drawing = mp.solutions.drawing_utils


# capture video
cap = cv2.VideoCapture(0)
prevTime = 0

with mp_face_detection.FaceDetection( model_selection=1,
    min_detection_confidence=0.65) as face_detection:
  while True:
    success, image = cap.read()
    if not success:
      print("Ignoring empty camera frame.")
      break


    #Convert the BGR image to RGB.
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image.flags.writeable = False
    results = face_detection.process(image)

    # Draw the face detection annotations on the image.
    image.flags.writeable = True
    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    if results.detections:
      for detection in results.detections:
        mp_drawing.draw_detection(image, detection)
        position = detection.location_data.relative_bounding_box
        x = position.xmin 
        print(x) # x are the coordinates of the nose


    cv2.imshow('BlazeFace Face Detection', image)
    if cv2.waitKey(5) & 0xFF == 27:
      break
cap.release()

我嘗試保存舊 x 並將其與新 x 進行比較,如果差異很大,則意味着有兩個或更多面孔,但這不是一種干凈的方法,面孔可以彼此接近如果 x 坐標遠離相機,則很難判斷被認為是不正常的閾值。

你想要的是能夠跟蹤面部。 mediapipe 的人臉檢測模塊沒有提供這樣的內置功能。 但是,您可以使用Mediapipe Box Tracking或您選擇的其他跟蹤算法來跟蹤視頻 stream 中的多張面孔。

如果你能負擔得起每一幀人臉檢測的計算成本,這取決於你的任務和硬件,你可以實現一個更簡單的朴素解決方案。 這可能是將某個 label 分配給新的人臉,並將這個 label 分配給下一幀中最近的人臉。

此外,請注意,您存儲在x中的值不是鼻子坐標,而是面部邊界框的最小 x 坐標。 要訪問鼻子的坐標,您可以使用: mp_face_detection.get_key_point(detection, mp_face_detection.FaceKeyPoint.NOSE_TIP)花點時間熟悉Mediapipe 文檔model 中的人臉檢測 output 。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM