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

Python使用Mediapipe开发手部检测器触发create_int()参数不兼容报错求助

问题排查及修复方案

核心报错原因

你遇到的TypeError: create_int(): incompatible function arguments报错由两个独立问题导致,逐一修复即可运行:

1. Mediapipe 版本API参数不匹配

你使用的是较新版Mediapipe(0.9.x及以上版本),官方对Hands类的构造函数参数顺序做了调整,新增了model_complexity(可选值0/1,int类型)参数,且位于参数列表第三位。
你原有代码按位置传参时,将浮点类型的detectionCon=0.5传给了需要int类型的model_complexity参数,触发类型错误。

修复方法:调用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
)

2. 代码缩进错误

你的代码存在两处缩进错误,会导致运行时找不到对应方法/变量:

  • findHands函数当前定义为全局函数,没有缩进归入handDetector类,调用detector.findHands()会触发属性不存在错误
  • main函数下的while循环没有缩进归入main函数,运行时会找不到cap、detector变量

修复方法:调整对应代码块的缩进层级即可。


修正后可运行完整代码

import cv2
import mediapipe as mp
import time

class handDetector():
    def __init__(self, mode=False, maxHands = 2, detectionCon=0.5, trackCon = 0.5):
        self.mode = mode
        self.maxHands = maxHands
        self.detectionCon = detectionCon
        self.trackCon = trackCon

        self.mpHands = mp.solutions.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
        )
        self.mpDraw = mp.solutions.drawing_utils

    # 缩进归入handDetector类,成为类成员方法
    def findHands(self, img, draw=True):
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        results = self.hands.process(imgRGB)

        if results.multi_hand_landmarks:
            for handLms in results.multi_hand_landmarks:
                if draw:
                    self.mpDraw.draw_landmarks(img, handLms, self.mpHands.HAND_CONNECTIONS)
        return img

def main():
    pTime = 0
    cTime = 0
    # 加cv2.CAP_DSHOW参数可消除末尾的摄像头警告,不加也不影响功能
    cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
    detector = handDetector()
    # 缩进归入main函数
    while True:
        success, img = cap.read()
        img = detector.findHands(img)
        
        cTime = time.time()
        fps = 1/(cTime-pTime)
        pTime = cTime

        cv2.putText(img, str(int(fps)),(10, 70), cv2.FONT_HERSHEY_COMPLEX, 3, (255,0,255),3)

        cv2.imshow("Image", img)
        cv2.waitKey(1)

if __name__ == "__main__":
    main()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 12:24:05