-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollect_data.py
More file actions
47 lines (35 loc) · 1.08 KB
/
collect_data.py
File metadata and controls
47 lines (35 loc) · 1.08 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
import cv2
import mediapipe as mp
import numpy as np
import os
GESTURE = "circle" #substitute with the gesture of which, data is to be collected
SAVE_DIR = f"dataset/{GESTURE}"
os.makedirs(SAVE_DIR, exist_ok=True)
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(max_num_hands=1)
cap = cv2.VideoCapture(0)
count = 0
while True:
ret, frame = cap.read()
frame = cv2.flip(frame, 1)
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
result = hands.process(rgb)
if result.multi_hand_landmarks:
lm = result.multi_hand_landmarks[0].landmark
wrist = lm[0]
data = []
for p in lm:
data.extend([
p.x - wrist.x,
p.y - wrist.y,
p.z - wrist.z
])
np.save(f"{SAVE_DIR}/{count}.npy", np.array(data))
count += 1
cv2.putText(frame, f"Samples: {count}", (20,40),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 2)
cv2.imshow("Collecting Data", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()