-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhand_gesture.py
26 lines (24 loc) · 987 Bytes
/
hand_gesture.py
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
import cv2
import mediapipe as mp
class HandGesture:
def __init__(self):
self.mpHands = mp.solutions.hands
self.hands = self.mpHands.Hands()
self.mpDraw = mp.solutions.drawing_utils
self.length = 0
def process(self, img):
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = self.hands.process(imgRGB)
if results.multi_hand_landmarks:
for handLms in results.multi_hand_landmarks:
for id, lm in enumerate(handLms.landmark):
h, w, c = img.shape
cx, cy = int(lm.x * w), int(lm.y * h)
if id == 4:
x4, y4 = cx, cy
if id == 8:
x8, y8 = cx, cy
self.length = math.hypot(x8 - x4, y8 - y4)
# Add drawing code here...
self.mpDraw.draw_landmarks(img, handLms, self.mpHands.HAND_CONNECTIONS)
return img