【问题标题】:mediapipe solution::FaceDetectionmediapipe 解决方案::FaceDetection
【发布时间】:2021-12-16 23:14:39
【问题描述】:

我想使用 mediapipe 人脸检测模块从原始图像和视频中裁剪人脸图像,以构建用于情感识别的数据集。

有没有办法从 mediapipe faceDetection 解决方案中获取边界框?

cap = cv2.VideoCapture(0)
with mp_face_detection.FaceDetection(
    model_selection=0, min_detection_confidence=0.5) as face_detection:
  while cap.isOpened():
    success, image = cap.read()
    if not success:
      print("Ignoring empty camera frame.")
      continue

    image.flags.writeable = False
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    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)
        
        ## 
        '''
        
        #### here i want to grab the bounding box for the detected faces in order to crop the face image
        
        '''    
        ##
            
    cv2.imshow('MediaPipe Face Detection', cv2.flip(image, 1))
    if cv2.waitKey(5) & 0xFF == 27:
      break
cap.release()

谢谢

【问题讨论】:

  • 我不确定下面是否值得作为答案,所以把它放在这里。 1. 检查什么是“检测”:github.com/google/mediapipe/blob/master/mediapipe/framework/… 2. 我相信你应该使用 location_data。它应该有“格式”,应该是“边界框”,然后应该填充“边界框”字段。
  • 刚刚检查了我的假设,用 sn-p 作为答案发布。

标签: python face-detection mediapipe


【解决方案1】:

为了弄清楚格式,你可以遵循两种方法:

检查medipipe中的protobuf文件

  1. 查看什么是“检测”:https://github.com/google/mediapipe/blob/master/mediapipe/framework/formats/detection.proto

  2. 我们需要location_data。它应该有format字段,应该是BOUNDING_BOX,或者RELATIVE_BOUNDING_BOX(但实际上只有RELATIVE_BOUNDING_BOX)。

结帐drawing_utils 内容:

只需检查draw_detection 方法。您需要与cv2.rectangle call 保持一致

这是一个sn-p

    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:
                draw_detection(image, detection)

                ##
                '''
        
                #### here i want to grab the bounding box for the detected faces in order to crop the face image
        
                '''
                ##
                location_data = detection.location_data
                if location_data.format == LocationData.RELATIVE_BOUNDING_BOX:
                    bb = location_data.relative_bounding_box
                    bb_box = [
                        bb.xmin, bb.ymin,
                        bb.width, bb.height,
                    ]

                    print(f"RBBox: {bb_box}")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 2013-07-22
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    相关资源
    最近更新 更多