-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhand_tracking.py
More file actions
24 lines (21 loc) · 899 Bytes
/
hand_tracking.py
File metadata and controls
24 lines (21 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import cv2
import mediapipe as mp
class HandTracker:
def __init__(self):
self.mp_hands = mp.solutions.hands
self.hands = self.mp_hands.Hands(
max_num_hands=1,
min_detection_confidence=0.7,
min_tracking_confidence=0.7
)
self.mp_draw = mp.solutions.drawing_utils
def detect_hand(self, frame):
"""Detects hand and extracts index finger position."""
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = self.hands.process(rgb_frame)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
index_finger = hand_landmarks.landmark[self.mp_hands.HandLandmark.INDEX_FINGER_TIP]
h, w, _ = frame.shape
return int(index_finger.x * w), int(index_finger.y * h)
return None