关于Python mediapipe 0.8.8.1 手部识别,提示Traceback (most recent call last)......TypeError......的问题

问题:

        前几天不小心更新了mediapipe至0.8.8.1,发现以前写的手部识别小程序无法成功运行,系统提示TypeError。我想试试,对比官网教程里的程序来寻找错误,官网链接如下:Advance Computer Vision with Python - Computer Vision Zone

        教程里的Hand Tracking Module(手部追踪模块)程序:

"""
Hand Tracing Module
By: Murtaza Hassan
Youtube: http://www.youtube.com/c/MurtazasWorkshopRoboticsandAI
Website: https://www.computervision.zone
"""
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(self.mode, self.maxHands,
                                        self.detectionCon, self.trackCon)
        self.mpDraw = mp.solutions.drawing_utils
    def findHands(self, img, draw=True):
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        self.results = self.hands.process(imgRGB)
        # print(results.multi_hand_landmarks)
        if self.results.multi_hand_landmarks:
            for handLms in self.results.multi_hand_landmarks:
                if draw:
                    self.mpDraw.draw_landmarks(img, handLms,
                                               self.mpHands.HAND_CONNECTIONS)
        return img
    def findPosition(self, img, handNo=0, draw=True):
        lmList = []
        if self.results.multi_hand_landmarks:
            myHand = self.results.multi_hand_landmarks[handNo]
            for id, lm in enumerate(myHand.landmark):
                # print(id, lm)
                h, w, c = img.shape
                cx, cy = int(lm.x * w), int(lm.y * h)
                # print(id, cx, cy)
                lmList.append([id, cx, cy])
                if draw:
                    cv2.circle(img, (cx, cy), 15, (255, 0, 255), cv2.FILLED)
        return lmList
def main():
    pTime = 0
    cTime = 0
    cap = cv2.VideoCapture(1)
    detector = handDetector()
    while True:
        success, img = cap.read()
        img = detector.findHands(img)
        lmList = detector.findPosition(img)
        if len(lmList) != 0:
            print(lmList[4])
        cTime = time.time()
        fps = 1 / (cTime - pTime)
        pTime = cTime
        cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3,
                    (255, 0, 255), 3)
        cv2.imshow("Image", img)
        cv2.waitKey(1)
if __name__ == "__main__":
    main()

        但是用官网教程里的程序时,依然发生了运行错误,且错误原因同为TypeError。我只能自己去找bug。错误截图如下:

         根据Traceback,错误锁定至教程程序的line 17:

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

         发现在mediapipe.solutions.hands里的class Hands中,__init__中有5个参数(line 89),而更新前只有4个参数。

        mediapipe 0.8.8.1更新后:

  def __init__(self,
               static_image_mode=False,
               max_num_hands=2,
               model_complexity=1,
               min_detection_confidence=0.5,
               min_tracking_confidence=0.5):

        更新前:

        更新后多了model_complexity这一项参数,后面有注释model_complexity只能为0或1的整数。没错,TypeError就出现在这里。在教程程序中,默认mode=False, maxHands=2, detectionCon=0.5, trackCon=0.5,所以教程程序出错的第17行等同于

​self.hands = self.mpHands.Hands(False,2,0.5, 0.5)

        而库的更新导致__init__中参数的顺序出现偏差。导致第3项self.detectionCon的0.5赋值给了只能为整数的model_complexity,从而产生TypeError。

解决方法:

方法1:

        将17行改为

        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:

        补全5个参数,11行改为

    def __init__(self, mode=False, max_hands=2, complexity_con=1, detection_con=0.5, track_con=0.5):

        在__init__里插入

        self.complexityCon = complexity_con

        原17行改为

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

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值