I am trying to make a hand recognition module using mediapipe and opencv and I was half done and tried to see if the code worked. It didn't and I don't know how to fix it.
This is the code I wrote-
import mediapipe as mp
import cv2
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)
results = self.hands.process(imgRGB)
#print(results.multi_hand_landmarks)
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
#for id, lm in enumerate(handLms.landmark):
#print(id, lm)
# h, w, c = img.shape
# cx, cy = int(lm.x * w), int(lm.y * h)
# print(id, cx, cy)
#if id == 4:
# cv2.circle(img, (cx,cy), 15, (255,0,255), cv2.FILLED)
def main():
pTime = 0
cTime = 0
cap = cv2.VideoCapture(0)
detector = handDetector()
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_SIMPLEX,2,(0,255,0),3)
cv2.imshow("Image", img)
cv2.waitKey(1)
if __name__ == "__main__":
main()
And this is the error which I got-
Traceback (most recent call last):
File "f:/Aryav files/J.A.R.V.I.S/Experiments/HandTrackingModule.py", line 60, in <module>
main()
File "f:/Aryav files/J.A.R.V.I.S/Experiments/HandTrackingModule.py", line 45, in main
detector = handDetector()
File "f:/Aryav files/J.A.R.V.I.S/Experiments/HandTrackingModule.py", line 14, in __init__
self.hands = self.mpHands.Hands(self.mode,self.maxHands,
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\mediapipe\python\solutions\hands.py",
line 114, in __init__
super().__init__(
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\mediapipe\python\solution_base.py", line 274, in __init__
self._input_side_packets = {
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\mediapipe\python\solution_base.py", line 275, in <dictcomp>
name: self._make_packet(self._side_input_type_info[name], data)
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\mediapipe\python\solution_base.py", line 533, 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
[ WARN:0@5.410] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (539) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
I am using python version 3.8.3
What changes do I need to make in the code to get it to work ?