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

Mediapipe手部追踪调用process()报缺少self/image参数问题求助

问题原因
  • 你直接调用了mp_hands.Hands类的实例方法process(),没有先对类做实例化操作。process()是实例方法,第一个参数self需要绑定实例对象后才能自动传入,你直接用类调用的话就需要手动传self,所以才会报缺少参数的错误。
  • 另外你的代码里存在笔误:打印时写的是print(result),但前面定义的变量名是results,后续运行也会报未定义变量的错误。
修复方案

你可以按下方代码修改,有两种常用写法:

写法1:上下文管理器写法(官方示例推荐)

import cv2
import mediapipe as mp

mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
cap = cv2.VideoCapture(0)

# 先实例化Hands类,用with管理生命周期
with mp_hands.Hands(
    static_image_mode=False,
    max_num_hands=2,
    min_detection_confidence=0.5,
    min_tracking_confidence=0.5) as hands:
    while cap.isOpened():
        success, img = cap.read()
        if not success:
            continue
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        # 用实例化后的hands对象调用process方法
        results = hands.process(imgRGB)
        print(results)
        if cv2.waitKey(10) & 0xFF == ord('q'):
            break

cap.release()
cv2.destroyAllWindows()

写法2:直接实例化对象写法

import cv2
import mediapipe as mp

mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
cap = cv2.VideoCapture(0)
# 先实例化Hands类得到对象
hands = mp_hands.Hands(
    static_image_mode=False,
    max_num_hands=2,
    min_detection_confidence=0.5,
    min_tracking_confidence=0.5)

while True:
    success, img = cap.read()
    if not success:
        continue
    imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    # 用实例对象调用process
    results = hands.process(imgRGB)
    print(results)
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
额外说明

你看到的OpenCV警告terminating async callback是Windows系统下OpenCV调用摄像头的常见兼容性警告,不影响功能运行,如果要消除可以在创建VideoCapture对象时添加参数:cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 17:57:07