【问题标题】:How can I extract the x and y position of the face detected in mediapipe?如何提取在 mediapipe 中检测到的人脸的 x 和 y 位置?
【发布时间】:2023-01-28 02:56:30
【问题描述】:

我正在尝试使用此代码来获取x 和 y 坐标面部位置的实时。我从媒体管道解决方案在线的。 运行此代码时,实际上会检测到面部,并且其所有特征都在显示的框架上显示为红点。 我希望能够以整数形式获取面部坐标,以便稍后使用它们跟踪伺服电机的位置,有什么办法可以做到吗?

text

# 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(2)
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)
        print(detection) # I can get the score, x, y,.. 


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

我尝试在 for 循环中打印变量 persons,我可以清楚地看到其中有 x 和 y 坐标,但我未能提取这些特定信息。 关于如何更好地操纵这个变量的任何想法? 我将使用检测到的人脸数量、他们的位置坐标和置信度。

【问题讨论】:

    标签: python opencv variables detection vision


    【解决方案1】:

    看看你的print(detection)的结果结构:

    label_id: 0
    score: 0.8402262330055237
    location_data {
      format: RELATIVE_BOUNDING_BOX
      relative_bounding_box {
        xmin: 0.4553905725479126
        ymin: 0.6456842422485352
        width: 0.24106884002685547
        height: 0.32147008180618286
      }
      relative_keypoints {
        x: 0.45961669087409973
        y: 0.7614946961402893
      }
    [...]
    }
    

    这些字段是mediapipe.framework.formats.detection_pb2.Detection 类型输出的属性。 我假设你所说的面部坐标是指边界框坐标。

    您可以像这样访问这些坐标:

    relative_bbox = detection.location_data.relative_bounding_box
    my_relative_bbox_list = [relative_bbox.xmin,relative_bbox.ymin,relative_bbox.width,relative_bbox.height]
    

    【讨论】:

      猜你喜欢
      • 2021-06-27
      • 2013-07-16
      • 2017-06-25
      • 2020-04-15
      • 2016-05-05
      • 2020-10-15
      • 1970-01-01
      • 2016-06-02
      • 2020-11-01
      相关资源
      最近更新 更多