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

Python+Mediapipe多特征检测运行报错create_bool()参数不兼容

错误产生原因

这个报错的核心原因是findPose类中实例化MediaPipe Pose对象时,参数传递顺序与Pose构造函数要求的顺序不匹配,导致浮点类型的置信度参数被传给了需要布尔类型的参数位。

MediaPipe Pose的官方构造函数参数顺序如下:

Pose(
    static_image_mode=False,
    model_complexity=1,
    smooth_landmarks=True,
    min_detection_confidence=0.5,
    min_tracking_confidence=0.5
)

你当前的传参顺序是(self.mode, self.complexity, self.detectionCon, self.trackCon, self.landmarks),第三个位置本应接收布尔类型的smooth_landmarks参数,却被你传入了浮点类型的detectionCon=0.5,就触发了类型不匹配的报错。

同时你代码中还存在几个额外的隐藏问题:

  • findPose类的__init__方法中,你硬编码覆盖了传入的detectionCon和trackCon参数,固定为0.5,无法自定义参数值
  • findPose方法中存在变量名拼写错误:self.result应为self.results
  • findPose方法中调用mpDraw时没有加实例前缀,drawSpec变量未定义就使用
解决方案

1. 修正findPose类的参数传递

优先使用关键字参数传递参数,完全避免顺序匹配错误,同时修正参数覆盖的问题:

class findPose():
    def __init__(self, mode=False, complexity=1, smooth_landmarks=True, detectionCon=0.5, trackCon=0.5):
        self.mode = mode
        self.complexity = complexity
        self.smooth_landmarks = smooth_landmarks
        self.detectionCon = detectionCon
        self.trackCon = trackCon
        self.mpPose = mp.solutions.pose
        # 用关键字参数传参,避免顺序错误
        self.pose = self.mpPose.Pose(
            static_image_mode=self.mode,
            model_complexity=self.complexity,
            smooth_landmarks=self.smooth_landmarks,
            min_detection_confidence=self.detectionCon,
            min_tracking_confidence=self.trackCon
        )
        self.mpDraw = mp.solutions.drawing_utils

2. 修正findPose方法的代码错误

def findPose(self, img, draw=True):
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        self.results = self.pose.process(imgRGB)
        if self.results.pose_landmarks:
            PoseLms = self.results.pose_landmarks
            # 定义drawSpec,用self.mpDraw调用方法
            drawSpec = self.mpDraw.DrawingSpec(thickness=1, circle_radius=1)
            self.mpDraw.draw_landmarks(img, PoseLms, self.mpPose.POSE_CONNECTIONS, drawSpec, drawSpec)
        return img

3. (可选)其他模块也改用关键字参数

为了避免后续MediaPipe版本更新导致参数顺序变化引发同类错误,建议你把Hands、FaceMesh的实例化也改成关键字参数形式:

# Hands实例化
self.hands = self.mpHands.Hands(
    static_image_mode=self.mode,
    max_num_hands=self.maxHands,
    min_detection_confidence=self.detectionCon,
    min_tracking_confidence=self.trackCon
)

# FaceMesh实例化
self.face_mesh = self.mpFaceMesh.FaceMesh(
    static_image_mode=self.mode,
    max_num_faces=self.maxFaces,
    min_detection_confidence=self.detectionCon,
    min_tracking_confidence=self.trackCon
)

内容的提问来源于stack exchange,提问作者이승환

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 23:51:03