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

Mediapipe姿态检测模块报错:create_bool()参数类型不兼容求助

解决MediaPipe姿态检测中的TypeError: create_bool()参数不兼容问题

问题描述

开发姿态检测模块时,封装代码后运行触发TypeError: create_bool(): incompatible function arguments错误,怀疑与变量声明或参数传递有关。以下是完整代码:

import cv2
import mediapipe as mp
import time


class poseDetector():

    def __init__(self, mode=False, upBody=False, smooth=True, detectionCon=0.5, trackCon=0.5):
        self.mode = mode
        self.upBody = upBody
        self.smooth = smooth
        self.detectionCon = detectionCon
        self.trackCon = trackCon

        self.mpPose = mp.solutions.pose
        self.mpDraw = mp.solutions.drawing_utils
        self.pose = self.mpPose.Pose(self.mode, self.upBody, self.smooth, self.detectionCon, self.trackCon)

    def findPose(self, img, draw=True):
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        results = self.pose.process(imgRGB)
        if results.pose_landmarks:
            if draw:
                self.mpDraw.draw_landmarks(img, results.pose_landmarks, self.mpPose.POSE_CONNECTIONS)
        return img


def main():
    cap = cv2.VideoCapture("squats.mp4")
    ptime = 0
    detector = poseDetector()

    while True:
        success, img = cap.read()
        img = detector.findPose(img)

        ctime = time.time()
        fps = 1 / (ctime - ptime)
        ptime = ctime

        cv2.putText(img, str(int(fps)), (70, 50), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
        cv2.imshow("Image", img)
        cv2.waitKey(1)


if __name__ == "__main__":
    main()

报错信息

"C:\Users\213353\Desktop\Ambaka\Coding\Computer Vision\venv\Scripts\python.exe" "C:\Users\213353\Desktop\Ambaka\Coding\Computer Vision\PoseModule.py" 
Traceback (most recent call last):
  File "C:\Users\213353\Desktop\Ambaka\Coding\Computer Vision\PoseModule.py", line 52, in <module>
    main()
  File "C:\Users\213353\Desktop\Ambaka\Coding\Computer Vision\PoseModule.py", line 36, in main
    detector = poseDetector()
  File "C:\Users\213353\Desktop\Ambaka\Coding\Computer Vision\PoseModule.py", line 18, in __init__
    self.pose = self.mpPose.Pose(self.mode, self.upBody, self.smooth, self.detectionCon, self.trackCon, self.modelComplex)
  File "C:\Users\213353\Desktop\Ambaka\Coding\Computer Vision\venv\lib\site-packages\mediapipe\python\solutions\pose.py", line 146, in __init__
    super().__init__(
  File "C:\Users\213353\Desktop\Ambaka\Coding\Computer Vision\venv\lib\site-packages\mediapipe\python\solution_base.py", line 290, in __init__
    self._input_side_packets = {
  File "C:\Users\213353\Desktop\Ambaka\Coding\Computer Vision\venv\lib\site-packages\mediapipe\python\solution_base.py", line 291, in <dictcomp>
    name: self._make_packet(self._side_input_type_info[name], data)
  File "C:\Users\213353\Desktop\Ambaka\Coding\Computer Vision\venv\lib\site-packages\mediapipe\python\solution_base.py", line 593, in _make_packet
    return getattr(packet_creator, 'create_' + packet_data_type.value)(data)
TypeError: create_bool(): incompatible function arguments. The following argument types are supported:
    1. (arg0: bool) -> mediapipe.python._framework_bindings.packet.Packet

Invoked with: 0.5

错误原因

报错核心是参数传递位置不匹配:按位置传递的detectionCon=0.5被传入了MediaPipe Pose构造函数中需要布尔值的参数位置(比如enable_segmentation),导致类型转换失败。此外,upBody参数在新版MediaPipe中已被移除,替换为model_complexity,这也是参数混乱的原因之一。

解决方法

1. 使用关键字参数传递,避免位置错误

直接指定参数名,确保每个值对应正确的参数:

self.pose = self.mpPose.Pose(
    static_image_mode=self.mode,
    model_complexity=1,  # 0=轻量,1=标准,2=高精度,替代原upBody逻辑
    smooth_landmarks=self.smooth,
    min_detection_confidence=self.detectionCon,
    min_tracking_confidence=self.trackCon
)

2. 更新类的初始化参数

移除过时的upBody参数,替换为符合新版MediaPipe的参数:

def __init__(self, mode=False, model_complexity=1, smooth=True, detectionCon=0.5, trackCon=0.5):
    self.mode = mode
    self.model_complexity = model_complexity
    self.smooth = smooth
    self.detectionCon = detectionCon
    self.trackCon = trackCon

    self.mpPose = mp.solutions.pose
    self.mpDraw = mp.solutions.drawing_utils
    self.pose = self.mpPose.Pose(
        static_image_mode=self.mode,
        model_complexity=self.model_complexity,
        smooth_landmarks=self.smooth,
        min_detection_confidence=self.detectionCon,
        min_tracking_confidence=self.trackCon
    )

3. 验证参数类型

确保所有参数类型与MediaPipe要求一致:

  • static_image_mode:布尔值
  • model_complexity:整数(0/1/2)
  • smooth_landmarks:布尔值
  • min_detection_confidence:浮点数(0-1)
  • min_tracking_confidence:浮点数(0-1)

修改后的完整代码

import cv2
import mediapipe as mp
import time


class poseDetector():

    def __init__(self, mode=False, model_complexity=1, smooth=True, detectionCon=0.5, trackCon=0.5):
        self.mode = mode
        self.model_complexity = model_complexity
        self.smooth = smooth
        self.detectionCon = detectionCon
        self.trackCon = trackCon

        self.mpPose = mp.solutions.pose
        self.mpDraw = mp.solutions.drawing_utils
        self.pose = self.mpPose.Pose(
            static_image_mode=self.mode,
            model_complexity=self.model_complexity,
            smooth_landmarks=self.smooth,
            min_detection_confidence=self.detectionCon,
            min_tracking_confidence=self.trackCon
        )

    def findPose(self, img, draw=True):
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        results = self.pose.process(imgRGB)
        if results.pose_landmarks:
            if draw:
                self.mpDraw.draw_landmarks(img, results.pose_landmarks, self.mpPose.POSE_CONNECTIONS)
        return img


def main():
    cap = cv2.VideoCapture("squats.mp4")
    ptime = 0
    detector = poseDetector()

    while True:
        success, img = cap.read()
        if not success:
            break  # 处理视频结束的情况
        img = detector.findPose(img)

        ctime = time.time()
        fps = 1 / (ctime - ptime) if (ctime - ptime) != 0 else 0
        ptime = ctime

        cv2.putText(img, str(int(fps)), (70, 50), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
        cv2.imshow("Image", img)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break  # 按q退出循环

    cap.release()
    cv2.destroyAllWindows()


if __name__ == "__main__":
    main()

内容的提问来源于stack exchange,提问作者Ambaka Le Gregam

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 10:55:24