Repository navigation
Expand file tree
/
Copy path2.0.py
More file actions
69 lines (52 loc) · 1.81 KB
/
Copy path2.0.py
File metadata and controls
69 lines (52 loc) · 1.81 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import cv2
import mediapipe as mp
import time
from mediapipe.tasks.python import vision
from mediapipe.tasks.python import BaseOptions
# ===== MODEL =====
model_path = "hand_landmarker.task"
options = vision.HandLandmarkerOptions(
base_options=BaseOptions(model_asset_path=model_path),
num_hands=2,
running_mode=vision.RunningMode.VIDEO
)
detector = vision.HandLandmarker.create_from_options(options)
# ===== HAND CONNECTIONS (Manual) =====
HAND_CONNECTIONS = [
(0,1),(1,2),(2,3),(3,4), # Thumb
(0,5),(5,6),(6,7),(7,8), # Index
(5,9),(9,10),(10,11),(11,12), # Middle
(9,13),(13,14),(14,15),(15,16),# Ring
(13,17),(17,18),(18,19),(19,20),# Pinky
(0,17) # Palm base
]
# ===== CAMERA =====
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
print("Camera starting... Press ESC to exit")
while True:
ret, frame = cap.read()
if not ret:
break
frame = cv2.flip(frame,1)
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=rgb)
timestamp = int(time.time()*1000)
result = detector.detect_for_video(mp_image, timestamp)
# ===== DRAW =====
if result.hand_landmarks:
h, w, _ = frame.shape
for hand in result.hand_landmarks:
# draw points
for p in hand:
cx, cy = int(p.x*w), int(p.y*h)
cv2.circle(frame,(cx,cy),5,(0,255,0),-1)
# draw lines
for start,end in HAND_CONNECTIONS:
x1,y1 = int(hand[start].x*w), int(hand[start].y*h)
x2,y2 = int(hand[end].x*w), int(hand[end].y*h)
cv2.line(frame,(x1,y1),(x2,y2),(255,0,0),2)
cv2.imshow("Tasks Hand Tracker", frame)
if cv2.waitKey(1)==27:
break
cap.release()
cv2.destroyAllWindows()