如何修复基于MediaPipe与OpenCV的Python手部追踪报错?
解决Mediapipe手部追踪模块封装时的TypeError问题
你的报错核心问题是mediapipe的Hands类参数传递顺序错误,导致本该传入整数类型的model_complexity参数位置,被传入了浮点数0.5,触发类型不匹配错误。
看你的handDetector类__init__方法里的Hands初始化代码:
self.hands = self.mpHands.Hands(self.mode, self.maxHands, self.detectionCon, self.trackCon)
而Mediapipe官方的Hands类构造函数参数顺序是:
- static_image_mode(对应你的mode)
- max_num_hands(对应你的maxHands)
- model_complexity(需要整数,可选0、1、2)
- min_detection_confidence(对应你的detectionCon)
- min_tracking_confidence(对应你的trackCon)
你跳过了model_complexity参数,直接把detectionCon(0.5,浮点数)传到了第三个参数位置,而这个位置要求是整数,所以出现create_int(): incompatible function arguments的错误。
修正后的代码
修改__init__方法里的Hands初始化部分,补上model_complexity参数(你已经定义了hand_complexity变量),或者直接用关键字参数传递避免顺序错误:
方法1:按顺序传递参数
self.hands = self.mpHands.Hands(self.mode, self.maxHands, self.handcom, self.detectionCon, self.trackCon)
方法2:用关键字参数(更推荐,避免顺序混淆)
self.hands = self.mpHands.Hands( static_image_mode=self.mode, max_num_hands=self.maxHands, model_complexity=self.handcom, min_detection_confidence=self.detectionCon, min_tracking_confidence=self.trackCon )
这样就能解决类型不匹配的问题,模块版代码可以正常运行。
内容的提问来源于stack exchange,提问作者user22436336
相关产品推荐
相关产品推荐

