-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandTrackingModule.py
More file actions
48 lines (37 loc) · 1.91 KB
/
HandTrackingModule.py
File metadata and controls
48 lines (37 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# we are gonna modularize the previous code.
# so we can use it easily on another project.
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(static_image_mode=self.mode, max_num_hands=self.maxHands,
min_detection_confidence=self.detectionCon,
min_tracking_confidence=self.trackCon) # gets the hands set in mediapipe module
self.mpDraw = mp.solutions.drawing_utils
# a class tht has methos to draw landmarks on the hand
def findHands(self, img, draw=True):
# now we have to send the rgb image to the hands object
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
self.result = self.hands.process(imgRGB)
# Processes an RGB image and returns the hand landmarks and handedness of each detected hand.
if draw:
if self.result.multi_hand_landmarks:
for handlms in self.result.multi_hand_landmarks:
self.mpDraw.draw_landmarks(img, handlms, self.mpHands.HAND_CONNECTIONS)
# draw the lines connecting 21 different landmarks
return img
def findPosition(self, img, handNo=0, draw=True):
lmList = []
if self.result.multi_hand_landmarks:
for id, lm in enumerate(self.result.multi_hand_landmarks[handNo].landmark):
h, w, c = img.shape
cx, cy = int(lm.x * w), int(lm.y * h)
# now we have got the pixel values of x,y cordinates of landmarks
lmList.append([id, cx, cy])
return lmList