2021-10-31

Getting errors with opencv and mediapipe for making Hand Tracking Module in Python 3.7.9

I am getting the following error whenever I try to run my code, I am following a tutorial for hand tracking, I followed the steps correctly but I still to have some sort of error.

Link to Video: https://www.youtube.com/watch?v=01sAkU_NvOY&t=2100s

Traceback (most recent call last):
  File "C:/Users/aryan/Desktop/user/Study/Com_vis/Hand_T_M.py", line 56, in <module>
    main()
  File "C:/Users/aryan/Desktop/user/Study/Com_vis/Hand_T_M.py", line 41, in main
    detector = handDetector()
  File "C:/Users/aryan/Desktop/user/Study/Com_vis/Hand_T_M.py", line 12, in __init__
    self.detectionCon, self.trackCon)
  File "C:\Users\aryan\Desktop\user\Study\Com_vis\venv\lib\site-packages\mediapipe\python\solutions\hands.py", line 127, in __init__
    outputs=['multi_hand_landmarks', 'multi_handedness'])
  File "C:\Users\aryan\Desktop\user\Study\Com_vis\venv\lib\site-packages\mediapipe\python\solution_base.py", line 260, in __init__
    for name, data in (side_inputs or {}).items()
  File "C:\Users\aryan\Desktop\user\Study\Com_vis\venv\lib\site-packages\mediapipe\python\solution_base.py", line 260, in <dictcomp>
    for name, data in (side_inputs or {}).items()
  File "C:\Users\aryan\Desktop\user\Study\Com_vis\venv\lib\site-packages\mediapipe\python\solution_base.py", line 513, in _make_packet
    return getattr(packet_creator, 'create_' + packet_data_type.value)(data)
TypeError: create_int(): incompatible function arguments. The following argument types are supported:
    1. (arg0: int) -> mediapipe.python._framework_bindings.packet.Packet

Invoked with: 0.5

Process finished with exit code 1

I am unable to figure out the problem with the code. I am running python 3.7.9, and here is my code.

Filename: Hand_T_M.py

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(0)
    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()


from Recent Questions - Stack Overflow https://ift.tt/3bqpNwp
https://ift.tt/eA8V8J

8 comments:

  1. Facing similar issue, did you find any solution?

    ReplyDelete
  2. i am also facing the same issue if u have a solution now can u pls help ?

    ReplyDelete
  3. Install mediapipe=0.8.8, solved the issue for me

    ReplyDelete
  4. I Install mediapipe=0.8.8, solved the issue Hand Tracking Module for me

    ReplyDelete