-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranslator.py
253 lines (204 loc) · 8.48 KB
/
translator.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""
Translates Sign Language to Speech in 2 modes
Mode: "y" -> Detect handregion using skin segmentation and translates
Mode : "n" -> Cropp ROI and translates
"""
import pyttsx3
import argparse
from predict import *
# Parse arguments from command line if any
ap = argparse.ArgumentParser()
ap.add_argument("-m", "--mode", required=False, help="Segmentation or not")
args = vars(ap.parse_args())
# Initialise Text to speech engine
engine = pyttsx3.init()
engine.setProperty('rate', 105)
engine.setProperty('voice', 1)
# Use "Yes" for Segmentation
TRANSLATOR_MODE = args["mode"]
# By default use ROI mode
if TRANSLATOR_MODE is None:
TRANSLATOR_MODE = "no"
window_name = "ASL"
frame_height, frame_width, roi_height, roi_width = 480, 900, 600, 300
def getMaxContour(contours, minArea = -1):
maxC = None
maxArea = minArea
for cnt in contours:
area = cv2.contourArea(cnt)
if (area > maxArea):
maxArea = area
maxC = cnt
return maxC
def centreCrop(x, y, w, h):
# Centre cropping the image
x_start, y_start, x_end, y_end = 0, 0, 0, 0
mx = 0
if w > 0 and h > 0:
mx = max(w, h, 150)
if x + 7 * mx // 8 > roi_width and y + mx > roi_height:
mx = max(roi_width - x, roi_height - y, 150)
x_start, x_end = roi_width - mx, roi_width
y_start, y_end = roi_height - mx, roi_height
elif y + mx > roi_height and x == 0:
mx = max(w, roi_height - y, 150)
x_start, x_end = x, x + mx
y_start, y_end = roi_height - mx, roi_height
elif y + mx > roi_height:
mx = max(w, roi_height - y, 150)
x_start, x_end = x - mx // 8, x + 7 * mx // 8
y_start, y_end = roi_height - mx, roi_height
elif x + 7 * mx // 8 > roi_width:
mx = max(roi_width - x, h, 150)
x_start, x_end = roi_width - mx, roi_width
y_start, y_end = y, y + mx
elif x == 0:
x_start, x_end = 0, mx
y_start, y_end = y, y + mx
else:
x_start, x_end = x - mx // 8, x + 7 * mx // 8
y_start, y_end = y, y + mx
return (x_start, y_start), (x_end, y_end)
def withSkinSegment():
cap = cv2.VideoCapture(0)
cv2.namedWindow(window_name, cv2.WND_PROP_FULLSCREEN)
# cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
sentence = ""
while True:
ret, frame = cap.read()
if ret is None:
print("No Frame Captured")
continue
cv2.rectangle(frame, (0, 0), (roi_width, roi_height), (255, 0, 0), 3) # bounding box which captures ASL sign to be detected by the system
# Crop blue rectangular area(ROI)
img1 = frame[0: roi_height, 0: roi_width]
img_ycrcb = cv2.cvtColor(img1, cv2.COLOR_BGR2YCR_CB)
blur = cv2.GaussianBlur(img_ycrcb, (11, 11), 0)
# lower and upper skin color
skin_ycrcb_min = np.array((0, 138, 67))
skin_ycrcb_max = np.array((255, 173, 133))
mask = cv2.inRange(blur, skin_ycrcb_min, skin_ycrcb_max) # detecting the hand in the bounding box
kernel = np.ones((2, 2), dtype = np.uint8)
# Fixes holes in foreground
mask = cv2.dilate(mask, kernel, iterations = 1)
contours, _ = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, 2)
# _, contours, _ = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, 2)
cnt = getMaxContour(contours, minArea = 2000)
naya = cv2.bitwise_and(img1, img1, mask = mask)
cv2.imshow("mask", mask)
x, y, w, h = cv2.boundingRect(cnt)
(x_start, y_start), (x_end, y_end) = centreCrop(x, y, w, h)
# Draw Green Square around the hand
cv2.rectangle(img1, (x_start, y_start), (x_end, y_end), (0, 255, 0), 2)
cv2.imshow("naya", naya)
hand_bg_rm = naya[y_start: y_end, x_start: x_end]
hand = img1[y_start: y_end, x_start: x_end]
# Control Key
c = cv2.waitKey(1) & 0xff
# Speak the sentence
if len(sentence) > 0 and c == ord('s'):
engine.say(sentence)
engine.runAndWait()
# Clear the sentence
if c == ord('c') or c == ord('C'):
sentence = ""
# Delete the last character
if c == ord('d') or c == ord('D'):
sentence = sentence[:-1]
# Put Space between words
if c == ord('m') or c == ord('M'):
sentence += " "
# If valid hand area is cropped
if hand.shape[0] != 0 and hand.shape[1] != 0:
conf, label = which(hand_bg_rm)
if conf >= THRESHOLD:
cv2.putText(frame, label, (50, 50), cv2.FONT_HERSHEY_COMPLEX_SMALL, .7, (0, 0, 255))
if c == ord('n') or c == ord('N'):
sentence += label
cv2.putText(frame, sentence, (50, 70), cv2.FONT_HERSHEY_COMPLEX_SMALL, .7, (0, 0, 255))
cv2.imshow(window_name, frame)
# Space to Save the Image
if c == 32:
if hand.shape[0] == hand.shape[1] + 1:
hand = img1[y_start: y_end, x_start: x_end + 1]
elif hand.shape[1] == hand.shape[0] + 1:
hand = img1[y_start: y_end + 1, x_start: x_end]
elif hand.shape[0] != hand.shape[1]:
print(hand.shape)
print("Outside ROI")
continue
cv2.imwrite("test.jpg", hand)
cv2.imwrite("test_bg_less.jpg", hand_bg_rm)
# If pressed ESC break
if c == 27:
cap.release()
cv2.destroyAllWindows()
exit()
cap.release()
cv2.destroyAllWindows()
def withoutSkinSegment():
window_name = "ASL"
frame_height, frame_width, roi_height, roi_width = 480, 900, 200, 200
cap = cv2.VideoCapture(0)
cv2.namedWindow(window_name, cv2.WND_PROP_FULLSCREEN)
x_start, y_start = 100, 100
sentence = ""
while True:
ret, frame = cap.read()
if ret is None:
print("No Frame Captured")
continue
cv2.rectangle(frame, (x_start, y_start), (x_start + roi_width, y_start + roi_height), (255, 0, 0),
3) # bounding box which captures ASL sign to be detected by the system
# Crop blue rectangular area(ROI)
img1 = frame[y_start: y_start + roi_height, x_start: x_start + roi_width]
img_ycrcb = cv2.cvtColor(img1, cv2.COLOR_BGR2YCR_CB)
blur = cv2.GaussianBlur(img_ycrcb, (11, 11), 0)
# lower and upper skin color
skin_ycrcb_min = np.array((0, 138, 67))
skin_ycrcb_max = np.array((255, 173, 133))
mask = cv2.inRange(blur, skin_ycrcb_min, skin_ycrcb_max) # detecting the hand in the bounding box
kernel = np.ones((2, 2), dtype=np.uint8)
# Fixes holes in foreground
mask = cv2.dilate(mask, kernel, iterations=1)
naya = cv2.bitwise_and(img1, img1, mask=mask)
cv2.imshow("mask", mask)
cv2.imshow("naya", naya)
hand_bg_rm = naya
hand = img1
# Control Key
c = cv2.waitKey(1) & 0xff
# Speak the sentence
if len(sentence) > 0 and c == ord('s'):
engine.say(sentence)
engine.runAndWait()
# Clear the sentence
if c == ord('c') or c == ord('C'):
sentence = ""
# Delete the last character
if c == ord('d') or c == ord('D'):
sentence = sentence[:-1]
# Put Space between words
if c == ord('m') or c == ord('M'):
sentence += " "
# If valid hand area is cropped
if hand.shape[0] != 0 and hand.shape[1] != 0:
conf, label = which(hand_bg_rm)
if conf >= THRESHOLD:
cv2.putText(frame, label, (50, 50), cv2.FONT_HERSHEY_COMPLEX_SMALL, .7, (0, 0, 255))
if c == ord('n') or c == ord('N'):
sentence += label
cv2.putText(frame, sentence, (50, 70), cv2.FONT_HERSHEY_COMPLEX_SMALL, .7, (0, 0, 255))
cv2.imshow(window_name, frame)
# If pressed ESC break
if c == 27:
cap.release()
cv2.destroyAllWindows()
exit()
cap.release()
cv2.destroyAllWindows()
# Use skin segmentation version
if TRANSLATOR_MODE[0].upper() == "Y" :
withSkinSegment()
else:
withoutSkinSegment()