Repository navigation
Expand file tree
/
Copy pathdata.py
More file actions
159 lines (133 loc) · 3.94 KB
/
Copy pathdata.py
File metadata and controls
159 lines (133 loc) · 3.94 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import cv2
import mediapipe as mp
import time
import csv
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)
# ===== CAMERA =====
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
# ===== DATASET FILE =====
file = open("isl_dataset.csv","a",newline="")
writer = csv.writer(file)
current_label = -1 # -1 means not recording
print("Press 1/2/3 to record gestures, 0 to stop")
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)
h,w,_ = frame.shape
if result.hand_landmarks:
for hand in result.hand_landmarks:
# ===== NORMALIZE USING WRIST =====
base_x = hand[0].x
base_y = hand[0].y
base_z = hand[0].z
row = []
for p in hand:
row.append(p.x - base_x)
row.append(p.y - base_y)
row.append(p.z - base_z)
# draw points
cx,cy = int(p.x*w), int(p.y*h)
cv2.circle(frame,(cx,cy),4,(0,255,0),-1)
# ===== SAVE DATA =====
if current_label != -1:
row.append(current_label)
writer.writerow(row)
print("Saved sample for label:",current_label)
cv2.putText(frame,f"Recording Label: {current_label}",
(10,40),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)
cv2.imshow("Dataset Recorder", frame)
key = cv2.waitKey(1)
if key == 27: # ESC
break
# Numbers
elif key == ord('1'):
current_label = 1
elif key == ord('2'):
current_label = 2
elif key == ord('3'):
current_label = 3
elif key == ord('4'):
current_label = 4
elif key == ord('5'):
current_label = 5
elif key == ord('6'):
current_label = 6
elif key == ord('7'):
current_label = 7
elif key == ord('8'):
current_label = 8
elif key == ord('9'):
current_label = 9
# Alphabets (lowercase)
elif key == ord('a'):
current_label = 'a'
elif key == ord('b'):
current_label = 'b'
elif key == ord('c'):
current_label = 'c'
elif key == ord('d'):
current_label = 'd'
elif key == ord('e'):
current_label = 'e'
elif key == ord('f'):
current_label = 'f'
elif key == ord('g'):
current_label = 'g'
elif key == ord('h'):
current_label = 'h'
elif key == ord('i'):
current_label = 'i'
elif key == ord('j'):
current_label = 'j'
elif key == ord('k'):
current_label = 'k'
elif key == ord('l'):
current_label = 'l'
elif key == ord('m'):
current_label = 'm'
elif key == ord('n'):
current_label = 'n'
elif key == ord('o'):
current_label = 'o'
elif key == ord('p'):
current_label = 'p'
elif key == ord('q'):
current_label = 'q'
elif key == ord('r'):
current_label = 'r'
elif key == ord('s'):
current_label = 's'
elif key == ord('t'):
current_label = 't'
elif key == ord('u'):
current_label = 'u'
elif key == ord('v'):
current_label = 'v'
elif key == ord('w'):
current_label = 'w'
elif key == ord('x'):
current_label = 'x'
elif key == ord('y'):
current_label = 'y'
elif key == ord('z'):
current_label = 'z'
elif key == ord('0'):
current_label = -1
cap.release()
file.close()
cv2.destroyAllWindows()