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

使用MediaPipe PoseLandmarker时出现AttributeError: NoneType无pose_landmarks属性

修复AttributeError问题的解决方案

核心问题分析

  1. 异步模式使用错误:LIVE_STREAM运行模式下,detect_async()是异步执行的,不会返回检测结果,结果会通过指定的回调函数传递。你直接把它的返回值(None)传给绘制函数,必然触发NoneType的属性错误。
  2. 回调参数设置错误:result_callback需要传入自定义的结果处理函数,而非PoseLandmarkerResult类本身。

修复后的完整代码

from mediapipe import solutions
from mediapipe.framework.formats import landmark_pb2
import numpy as np
import mediapipe as mp
from mediapipe.tasks import python
from mediapipe.tasks.python import vision
import cv2
import time

VisionRunningMode = mp.tasks.vision.RunningMode
# 全局变量存储标注后的图像
annotated_image = None

def draw_landmarks_on_image(rgb_image, detection_result):
    annotated_image = np.copy(rgb_image)
    # 先判断是否检测到姿态关键点,避免空列表报错
    if detection_result.pose_landmarks:
        for pose_landmarks in detection_result.pose_landmarks:
            pose_landmarks_proto = landmark_pb2.NormalizedLandmarkList()
            pose_landmarks_proto.landmark.extend([
                landmark_pb2.NormalizedLandmark(x=landmark.x, y=landmark.y, z=landmark.z) 
                for landmark in pose_landmarks
            ])
            solutions.drawing_utils.draw_landmarks(
                annotated_image,
                pose_landmarks_proto,
                solutions.pose.POSE_CONNECTIONS,
                solutions.drawing_styles.get_default_pose_landmarks_style())
    return annotated_image

# 自定义回调函数,处理异步返回的检测结果
def result_callback(result: vision.PoseLandmarkerResult, output_image: mp.Image, timestamp_ms: int):
    global annotated_image
    # 将MediaPipe格式的图像转为numpy数组
    rgb_image = output_image.numpy_view()
    # 绘制姿态关键点
    annotated_image = draw_landmarks_on_image(rgb_image, result)
    # 转换回BGR格式适配OpenCV显示
    annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_RGB2BGR)

base_options = python.BaseOptions(model_asset_path='pose_landmarker_full.task')
options = vision.PoseLandmarkerOptions(
    base_options=base_options,
    running_mode=VisionRunningMode.LIVE_STREAM,
    result_callback=result_callback,  # 传入自定义回调函数
    num_poses=3,
    output_segmentation_masks=False)
detector = vision.PoseLandmarker.create_from_options(options)

cap = cv2.VideoCapture(0)
    
while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break
    # OpenCV读取的是BGR格式,转为RGB供MediaPipe处理
    rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    timestamp = int(round(time.time() * 1000))
    # 创建MediaPipe图像对象
    mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=rgb_frame)
    # 异步提交图像,结果由回调函数处理
    detector.detect_async(mp_image, timestamp)
    
    # 仅当回调已生成标注图像时才显示
    if annotated_image is not None:
        cv2.imshow("Pose Estimation", annotated_image)
    
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

关键修改说明

  • 新增result_callback函数:专门接收异步返回的检测结果,完成关键点绘制和图像格式转换。
  • 修正PoseLandmarkerOptions配置:将result_callback参数改为自定义函数,而非类对象。
  • 移除无效的返回值赋值:detect_async()无返回值,只需提交图像即可。
  • 补充颜色空间转换:解决OpenCV与MediaPipe的图像格式差异问题。
  • 增加空值判断:避免回调未返回结果时尝试显示空图像。

内容的提问来源于stack exchange,提问作者bad at programming

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 07:12:07