-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGesture_Recognize_sign.py
127 lines (86 loc) · 2.18 KB
/
Gesture_Recognize_sign.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
# coding: utf-8
# In[2]:
import cv2
import numpy as np
from keras.models import load_model
# In[3]:
model = load_model('./new_model6.h5')
# In[4]:
gestures = {
1:'A',
2:'B',
3:'C',
4:'D',
5:'E',
6:'F',
7:'G',
8:'H',
9:'I',
10:'K',
11:'L',
12:'M',
13:'N',
14:'O',
15:'P',
16:'Q',
17:'R',
18:'S',
19:'T',
20:'U',
21:'V',
22:'W',
23:'X',
24:'Y',
}
# In[10]:
def predict(gesture):
img = cv2.resize(gesture, (50,50))
img = img.reshape(1,50,50,1)
img = img/255.0
prd = model.predict(img)
index = prd.argmax()
return gestures[index]
# In[15]:
vc = cv2.VideoCapture(0)
rval, frame = vc.read()
old_text = ''
pred_text = ''
count_frames = 0
total_str = ''
flag = False
# In[16]:
while True:
if frame is not None:
frame = cv2.flip(frame, 1)
frame = cv2.resize( frame, (400,400) )
cv2.rectangle(frame, (300,300), (100,100), (0,255,0), 2)
crop_img = frame[100:300, 100:300]
grey = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(grey,210,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)[1]
blackboard = np.zeros(frame.shape, dtype=np.uint8)
cv2.putText(blackboard, "Predicted text - ", (30, 40), cv2.FONT_HERSHEY_TRIPLEX, 1, (255, 255, 0))
if count_frames > 20 and pred_text != "":
total_str += pred_text
count_frames = 0
if flag == True:
old_text = pred_text
pred_text = predict(thresh)
if old_text == pred_text:
count_frames += 1
else:
count_frames = 0
cv2.putText(blackboard, total_str, (30, 80), cv2.FONT_HERSHEY_TRIPLEX, 1, (255, 255, 127))
res = np.hstack((frame, blackboard))
cv2.imshow("image", res)
cv2.imshow("hand", thresh)
rval, frame = vc.read()
keypress = cv2.waitKey(1)
if keypress == ord('c'):
flag = True
if keypress == ord('q'):
break
vc.release()
cv2.destroyAllWindows()
cv2.waitKey(1)
# In[17]:
vc.release()