You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从MediaPipe FaceDetection获取边界框裁剪人脸构建情感识别数据集

MediaPipe FaceDetection获取人脸边界框方法

可以直接从返回的检测结果中提取边界框,MediaPipe返回的边界框为归一化比例值,转换为原图像素坐标即可用于裁剪。

实现逻辑

  • 首先获取当前帧的宽高参数,用于归一化坐标转像素坐标
  • 从detection.location_data.relative_bounding_box中提取归一化边界框参数
  • 转换坐标时做边界截断,避免坐标超出图像范围引发报错

你标注位置需要插入的代码

# 取当前帧宽高
h, w = image.shape[:2]
# 提取归一化边界框
rel_bbox = detection.location_data.relative_bounding_box
# 转像素坐标,同时做边界校验
x_min = max(0, int(rel_bbox.xmin * w))
y_min = max(0, int(rel_bbox.ymin * h))
x_max = min(w, int((rel_bbox.xmin + rel_bbox.width) * w))
y_max = min(h, int((rel_bbox.ymin + rel_bbox.height) * h))
# 裁剪人脸
cropped_face = image[y_min:y_max, x_min:x_max]

# 若需要保存到数据集,直接调用imwrite即可,可自行定义保存路径和命名规则
# cv2.imwrite(f"./dataset/face_{time.time()}.jpg", cropped_face)

完整修改后的代码

import cv2
import mediapipe as mp
import time # 如需命名去重可导入

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

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)
        
        ## 插入的边界框提取代码开始
        h, w = image.shape[:2]
        rel_bbox = detection.location_data.relative_bounding_box
        x_min = max(0, int(rel_bbox.xmin * w))
        y_min = max(0, int(rel_bbox.ymin * h))
        x_max = min(w, int((rel_bbox.xmin + rel_bbox.width) * w))
        y_max = min(h, int((rel_bbox.ymin + rel_bbox.height) * h))
        cropped_face = image[y_min:y_max, x_min:x_max]
        # 保存裁剪的人脸
        # cv2.imwrite(f"./emotion_dataset/face_{int(time.time()*1000)}.jpg", cropped_face)
        ## 插入的代码结束
            
    cv2.imshow('MediaPipe Face Detection', cv2.flip(image, 1))
    if cv2.waitKey(5) & 0xFF == 27:
      break
cap.release()
cv2.destroyAllWindows()

小提示:如果担心裁剪的人脸太贴近边缘,可以在计算x_min、x_max、y_min、y_max时适当扩大范围,比如x_min = max(0, int(rel_bbox.xmin * w) - 10),上下左右各加10像素的边距,同样要注意做边界校验避免超出图像范围。

内容的提问来源于stack exchange,提问作者Houssem Elhadj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 15:24:09