how to track hands python opencv 2fmediapipe

Solutions on MaxInterview for how to track hands python opencv 2fmediapipe by the best coders in the world

showing results for - "how to track hands python opencv 2fmediapipe"
Edwin
11 Jun 2019
1"""
2Hand Tracing Module
3By: Murtaza Hassan
4Youtube: http://www.youtube.com/c/MurtazasWorkshopRoboticsandAI
5Website: https://www.computervision.zone
6"""
7
8import cv2
9import mediapipe as mp
10import time
11
12
13class handDetector():
14    def __init__(self, mode=False, maxHands=2, detectionCon=0.5, trackCon=0.5):
15        self.mode = mode
16        self.maxHands = maxHands
17        self.detectionCon = detectionCon
18        self.trackCon = trackCon
19
20        self.mpHands = mp.solutions.hands
21        self.hands = self.mpHands.Hands(self.mode, self.maxHands,
22                                        self.detectionCon, self.trackCon)
23        self.mpDraw = mp.solutions.drawing_utils
24
25    def findHands(self, img, draw=True):
26        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
27        self.results = self.hands.process(imgRGB)
28        # print(results.multi_hand_landmarks)
29
30        if self.results.multi_hand_landmarks:
31            for handLms in self.results.multi_hand_landmarks:
32                if draw:
33                    self.mpDraw.draw_landmarks(img, handLms,
34                                               self.mpHands.HAND_CONNECTIONS)
35        return img
36
37    def findPosition(self, img, handNo=0, draw=True):
38
39        lmList = []
40        if self.results.multi_hand_landmarks:
41            myHand = self.results.multi_hand_landmarks[handNo]
42            for id, lm in enumerate(myHand.landmark):
43                # print(id, lm)
44                h, w, c = img.shape
45                cx, cy = int(lm.x * w), int(lm.y * h)
46                # print(id, cx, cy)
47                lmList.append([id, cx, cy])
48                if draw:
49                    cv2.circle(img, (cx, cy), 15, (255, 0, 255), cv2.FILLED)
50
51        return lmList
52
53
54def main():
55    pTime = 0
56    cTime = 0
57    cap = cv2.VideoCapture(1)
58    detector = handDetector()
59    while True:
60        success, img = cap.read()
61        img = detector.findHands(img)
62        lmList = detector.findPosition(img)
63        if len(lmList) != 0:
64            print(lmList[4])
65
66        cTime = time.time()
67        fps = 1 / (cTime - pTime)
68        pTime = cTime
69
70        cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3,
71                    (255, 0, 255), 3)
72
73        cv2.imshow("Image", img)
74        cv2.waitKey(1)
75
76
77if __name__ == "__main__":
78    main()
similar questions