-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
49 lines (36 loc) · 1.64 KB
/
Copy pathmain.py
File metadata and controls
49 lines (36 loc) · 1.64 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
import cv2
import mediapipe.python.solutions.hands as mp_hands
import mediapipe.python.solutions.drawing_utils as mp_drawing
import mediapipe.python.solutions.drawing_styles as mp_drawing_styles
def run_hand_tracking():
cam = cv2.VideoCapture(index = 0)
with mp_hands.Hands(
model_complexity=0,
max_num_hands=2,
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as hands:
# Frame capture loop
while cam.isOpened():
success, frame = cam.read()
if not success:
print("empty camera frame.")
continue
# Convert the BGR image to RGB.
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(frame_rgb)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
image = frame,
landmark_list = hand_landmarks,
connections = mp_hands.HAND_CONNECTIONS,
landmark_drawing_spec = mp_drawing_styles.get_default_hand_landmarks_style(),
connection_drawing_spec = mp_drawing_styles.get_default_hand_connections_style())
# Dsplay window and flip frame
cv2.imshow('Hand Tracking', cv2.flip(frame, 1))
# Exit on Q key
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cam.release()
if __name__ == "__main__":
run_hand_tracking()