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

手势控制Arduino LED亮度项目中MediaPipe TypeError问题求助

解决MediaPipe TypeError: create_int()传入浮点数0.8的问题

问题重现

开发手势控制Arduino LED亮度项目时,初始化HandDetector触发TypeError,错误提示如下:

TypeError: create_int(): incompatible function arguments. The following argument types are supported:
1. (arg0: int) -> mediapipe.python._framework_bindings.packet.Packet

Invoked with: 0.8

出错代码片段:

import serial
import cvzone
import math
import cv2
from cvzone.HandTrackingModule import HandDetector
serialcomm = serial.Serial('COM7', 9600)
serialcomm.timeout = 1
cap=cv2.VideoCapture(0) 
# 触发错误的代码行
detector = HandDetector(detectionCon=0.8, maxHands=1)
# 其余代码省略

错误原因

从报错栈可以看出,cvzone的HandDetector.__init__方法中,错误地将detectionCon(浮点数0.8)传递给了MediaPipe Hands类的**model_complexity参数**——而这个参数要求传入整数(0/1/2,代表模型复杂度级别),而非浮点数。

MediaPipe Hands类的初始化参数顺序为:
static_image_mode=False, max_num_hands=2, model_complexity=1, min_detection_confidence=0.5, min_tracking_confidence=0.5

cvzone的封装代码未使用关键字参数传参,导致detectionCon被错位传递到了需要整数的参数位置,触发类型错误。

解决方案

方案1:改用MediaPipe原生接口(推荐)

绕过cvzone的封装,直接使用MediaPipe原生Hands类,确保参数传递准确:

import serial
import math
import cv2
import mediapipe as mp
from mediapipe.python.solutions.hands import Hands, HAND_CONNECTIONS

serialcomm = serial.Serial('COM7', 9600)
serialcomm.timeout = 1
cap=cv2.VideoCapture(0) 
# 显式指定参数名,避免顺序错误
detector = Hands(static_image_mode=False, 
                 max_num_hands=1, 
                 model_complexity=1,
                 min_detection_confidence=0.8, 
                 min_tracking_confidence=0.5)

# 注意:原生接口的手势检测逻辑需要调整,示例如下
mp_draw = mp.solutions.drawing_utils
l=[]
while True:
    success,img =cap.read()
    img=cv2.resize(img,(500, 500))
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    results = detector.process(img_rgb)
    
    if results.multi_hand_landmarks:
        for hand_landmarks in results.multi_hand_landmarks:
            # 获取关键点坐标
            l = []
            for id, lm in enumerate(hand_landmarks.landmark):
                h, w, c = img.shape
                cx, cy = int(lm.x * w), int(lm.y * h)
                l.append([id, cx, cy])
            # 后续手势计算逻辑和原代码一致
            x1=l[4][1]
            y1=l[4][2]
            x2=l[8][1]
            y2=l[8][2]
            cv2.circle(img,(x1,y1),7,(0,255,255),1)
            cv2.circle(img,(x2,y2),7,(0,255,255),1)
            cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)
            d=int(math.sqrt(math.pow(x2 - x1, 2)+math.pow(y2 - y1, 2) * 1.0))
            d=int((d/110)*255)
            e='\n'
            if 0<d<256:
                cv2.putText(img,str(d),(20,30),cv2.FONT_HERSHEY_COMPLEX,.7,(255,255,255),1)     
                serialcomm.write(str(d).encode())
                serialcomm.write(e.encode())
            
    cv2.imshow('Image',img)  
    if cv2.waitKey(20) & 0xFF == 27:
        break        
cv2.destroyAllWindows()

方案2:修改cvzone封装代码

找到cvzone的HandTrackingModule.py文件(路径参考报错信息:~\AppData\Local\Programs\Python\Python311\Lib\site-packages\cvzone\HandTrackingModule.py),修改第33-34行:

原代码:

self.hands = self.mpHands.Hands(self.mode, self.maxHands,
                                self.detectionCon, self.minTrackCon)

修改为(显式指定参数名):

self.hands = self.mpHands.Hands(static_image_mode=self.mode, 
                                max_num_hands=self.maxHands,
                                min_detection_confidence=self.detectionCon,
                                min_tracking_confidence=self.minTrackCon)

修改后重新运行原代码即可解决参数错位问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 19:20:00