-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_create_webcam.py
203 lines (186 loc) · 7.16 KB
/
data_create_webcam.py
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# import cv2
# import numpy as np
# import mediapipe as mp
# import os
# import traceback
#
# mp_drawing = mp.solutions.drawing_utils
# mp_hands = mp.solutions.hands
#
# capture = cv2.VideoCapture(0)
# hands = mp_hands.Hands(max_num_hands=1)
#
# count = len(os.listdir(r"D:\Mediapipe ASL\MediaPipe ASL\Input\landmark_images\A\\"))
# c_dir = 'A'
#
# offset = 15
# step = 1
# flag = False
# suv = 0
#
# white = np.ones((400, 400), np.uint8) * 255
# cv2.imwrite("./white.jpg", white)
#
# while True:
# try:
# _, frame = capture.read()
# frame = cv2.flip(frame, 1)
#
# # Convert the image to RGB format for Mediapipe
# frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# results = hands.process(frame_rgb)
#
# white = cv2.imread("./white.jpg")
#
# if results.multi_hand_landmarks:
# for hand_landmarks in results.multi_hand_landmarks:
# pts = []
# for landmark in hand_landmarks.landmark:
# x = int(landmark.x * frame.shape[1])
# y = int(landmark.y * frame.shape[0])
# pts.append((x, y))
#
# os = ((400 - frame.shape[1]) // 2) - 15
# os1 = ((400 - frame.shape[0]) // 2) - 15
# for t in range(0, 4, 1):
# cv2.line(white, (pts[t][0] + os, pts[t][1] + os1), (pts[t + 1][0] + os, pts[t + 1][1] + os1),
# (0, 255, 0), 3)
# for t in range(5, 8, 1):
# cv2.line(white, (pts[t][0] + os, pts[t][1] + os1), (pts[t + 1][0] + os, pts[t + 1][1] + os1),
# (0, 255, 0), 3)
# for t in range(9, 12, 1):
# cv2.line(white, (pts[t][0] + os, pts[t][1] + os1), (pts[t + 1][0] + os, pts[t + 1][1] + os1),
# (0, 255, 0), 3)
# for t in range(13, 16, 1):
# cv2.line(white, (pts[t][0] + os, pts[t][1] + os1), (pts[t + 1][0] + os, pts[t + 1][1] + os1),
# (0, 255, 0), 3)
# for t in range(17, 20, 1):
# cv2.line(white, (pts[t][0] + os, pts[t][1] + os1), (pts[t + 1][0] + os, pts[t + 1][1] + os1),
# (0, 255, 0), 3)
# cv2.line(white, (pts[5][0] + os, pts[5][1] + os1), (pts[9][0] + os, pts[9][1] + os1), (0, 255, 0), 3)
# cv2.line(white, (pts[9][0] + os, pts[9][1] + os1), (pts[13][0] + os, pts[13][1] + os1), (0, 255, 0),
# 3)
# cv2.line(white, (pts[13][0] + os, pts[13][1] + os1), (pts[17][0] + os, pts[17][1] + os1), (0, 255, 0),
# 3)
# cv2.line(white, (pts[0][0] + os, pts[0][1] + os1), (pts[5][0] + os, pts[5][1] + os1), (0, 255, 0), 3)
# cv2.line(white, (pts[0][0] + os, pts[0][1] + os1), (pts[17][0] + os, pts[17][1] + os1), (0, 255, 0),
# 3)
#
# skeleton0 = np.array(white)
# zz = np.array(white)
# for i in range(21):
# cv2.circle(white, (pts[i][0] + os, pts[i][1] + os1), 2, (0, 0, 255), 1)
#
# skeleton1 = np.array(white)
#
# cv2.imshow("1", skeleton1)
#
# frame = cv2.putText(frame, "dir=" + str(c_dir) + " count=" + str(count), (50, 50),
# cv2.FONT_HERSHEY_SIMPLEX,
# 1, (255, 0, 0), 1, cv2.LINE_AA)
# cv2.imshow("frame", frame)
# interrupt = cv2.waitKey(1)
# if interrupt & 0xFF == 27:
# # esc key
# break
#
# if interrupt & 0xFF == ord('n'):
# c_dir = chr(ord(c_dir) + 1)
# if ord(c_dir) == ord('Z') + 1:
# c_dir = 'A'
# flag = False
# count = len(
# os.listdir(r"D:\Mediapipe ASL\MediaPipe ASL\Input\landmark_images" + (c_dir) + "\\"))
#
# if interrupt & 0xFF == ord('a'):
# if flag:
# flag = False
# else:
# suv = 0
# flag = True
#
# print("=====", flag)
# if flag:
# if suv == 180:
# flag = False
# if step % 3 == 0:
# cv2.imwrite(r"D:\Mediapipe ASL\MediaPipe ASL\Input\landmark_images" + (c_dir) + "\\" + str(count) + ".jpg",skeleton1)
# count += 1
# suv += 1
# step += 1
#
# except Exception:
# print("==", traceback.format_exc())
#
# capture.release()
# cv2.destroyAllWindows()
#
import cv2
import numpy as np
from mediapipe.python.solutions import drawing_utils as mp_drawing
from mediapipe.python.solutions import hands as mp_hands
import os
import traceback
mp_drawing = mp_drawing
mp_hands = mp_hands
capture = cv2.VideoCapture(0)
hands = mp_hands.Hands(max_num_hands=1)
count = len(os.listdir(r"D:\Mediapipe ASL\MediaPipe ASL\Input\landmark_images\A"))
c_dir = 'A'
offset = 15
step = 1
flag = False
suv = 0
white = np.ones((400, 400), np.uint8) * 255
cv2.imwrite("static_images/white.jpg", white)
while True:
try:
_, frame = capture.read()
frame = cv2.flip(frame, 1)
# Convert the image to RGB format for Mediapipe
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(frame_rgb)
white = cv2.imread("static_images/white.jpg")
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(white, hand_landmarks, mp_hands.HAND_CONNECTIONS,
mp_drawing.DrawingSpec(color=(0, 0, 255), thickness=1, circle_radius=2),
mp_drawing.DrawingSpec(color=(0, 255, 0), thickness=3))
frame = cv2.putText(frame, "dir=" + str(c_dir) + " count=" + str(count), (50, 50),
cv2.FONT_HERSHEY_SIMPLEX,
1, (255, 0, 0), 1, cv2.LINE_AA)
cv2.imshow("frame", frame)
cv2.imshow("1", white) # Display the hand skeleton image
interrupt = cv2.waitKey(1)
if interrupt & 0xFF == 27:
# esc key
break
if interrupt & 0xFF == ord('n'):
c_dir = chr(ord(c_dir) + 1)
if ord(c_dir) == ord('Z') + 1:
c_dir = 'A'
flag = False
count = len(
os.listdir(r"D:\Mediapipe ASL\MediaPipe ASL\Input\landmark_images\\" + (c_dir) + "\\"))
if interrupt & 0xFF == ord('a'):
if flag:
flag = False
else:
suv = 0
flag = True
print("=====", flag)
if flag:
if suv == 180:
flag = False
if step % 3 == 0:
cv2.imwrite(
r"D:\Mediapipe ASL\MediaPipe ASL\Input\landmark_images\\" + (c_dir) + "\\" + str(
count) + ".jpg",
white) # Save the hand skeleton image
count += 1
suv += 1
step += 1
except Exception:
print("==", traceback.format_exc())
capture.release()
cv2.destroyAllWindows()