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

使用MediaPipe vision.PoseLandmarker新API无法访问特定姿态关键点问题

解决MediaPipe PoseLandmarker无法访问姿态关键点的问题

新旧MediaPipe姿态检测API的结果结构差异是导致无法访问关键点的核心原因,直接替换为以下适配新API的代码即可解决:

import cv2
import mediapipe as mp
from mediapipe.tasks import python
from mediapipe.tasks.python import vision

# 初始化PoseLandmarker(需提前下载pose_landmarker.task模型文件)
base_options = python.BaseOptions(model_asset_path='pose_landmarker.task')
options = vision.PoseLandmarkerOptions(
    base_options=base_options,
    output_segmentation_masks=False)
detector = vision.PoseLandmarker.create_from_options(options)

cap = cv2.VideoCapture("video.mp4")

while cap.isOpened():
    success, frame = cap.read()
    if not success:
        break
    
    # 转换帧为MediaPipe要求的RGB格式
    rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=rgb_frame)
    
    # 执行姿态检测
    results = detector.detect(mp_image)
    
    height, width, _ = frame.shape
    
    # 访问第一个人体的第16号关键点(与旧版索引一致)
    if results.pose_landmarks:
        # 取第一个检测到的人体关键点集合
        target_landmarks = results.pose_landmarks[0]
        # 获取第16号关键点坐标
        landmark_16 = target_landmarks[16]
        x1 = int(landmark_16.x * width)
        y1 = int(landmark_16.y * height)
        
        # 可选:在帧上绘制关键点
        cv2.circle(frame, (x1, y1), 5, (0, 255, 0), -1)
    
    cv2.imshow('Pose Detection', frame)
    if cv2.waitKey(5) & 0xFF == 27:
        break

cap.release()
cv2.destroyAllWindows()

关键差异说明

  • 旧版API的results.pose_landmarks是单个人体的关键点集合,新版results.pose_landmarks是列表(支持多人体检测),需通过索引指定目标人体(如results.pose_landmarks[0]取第一个人)。
  • 新版中单个关键点的访问方式与旧版一致(.x/.y获取归一化坐标),关键点索引和旧版完全匹配,无需调整。
  • 必须提前下载对应的pose_landmarker.task模型文件,确保模型路径正确。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 08:09:59