-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathlive_demo.py
203 lines (149 loc) · 5.23 KB
/
live_demo.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 OpenCV for image processing
import sys # Import for time
import os # Import for reading files
import threading # Import for separate thread for image classification
import numpy as np # Import for converting vectors
from gtts import gTTS # Import Google Text to Speech
#import spell_checker # Import for spelling corrections
# Disable tensorflow compilation warnings
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf # Import tensorflow for Inception Net's backend
# Language in which you want to convert
language = 'en'
# Get a live stream from the webcam
live_stream = cv2.VideoCapture(0)
# Word for which letters are currently being signed
current_word = ""
# Load training labels file
label_lines = [line.rstrip() for line in tf.gfile.GFile("training_set_labels.txt")]
# Load trained model's graph
with tf.gfile.FastGFile("trained_model_graph.pb", 'rb') as f:
# Define a tensorflow graph
graph_def = tf.GraphDef()
# Read and import line by line from the trained model's graph
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
def predict(image_data):
# Focus on Region of Interest (Image within the bounding box)
resized_image = image_data[70:350, 70:350]
# Resize to 200 x 200
resized_image = cv2.resize(resized_image, (200, 200))
image_data = cv2.imencode('.jpg', resized_image)[1].tostring()
predictions = sess.run(softmax_tensor, {'DecodeJpeg/contents:0': image_data})
# Sort to show labels of first prediction in order of confidence
top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]
max_score = 0.0
res = ''
for node_id in top_k:
# Just to get rid of the Z error for demo
if label_lines[node_id].upper() == 'Z':
human_string = label_lines[node_id+1]
else:
human_string = label_lines[node_id]
score = predictions[0][node_id]
if score > max_score:
max_score = score
res = human_string
return res, max_score
def speak_letter(letter):
# Create the text to be spoken
prediction_text = letter
# Create a speech object from text to be spoken
speech_object = gTTS(text=prediction_text, lang=language, slow=False)
# Save the speech object in a file called 'prediction.mp3'
speech_object.save("prediction.mp3")
# Playing the speech using mpg321
os.system("afplay prediction.mp3")
with tf.Session() as sess:
# Feed the image_data as input to the graph and get first prediction
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
# Global variable to keep track of time
time_counter = 0
# Flag to check if 'c' is pressed
captureFlag = False
# Toggle real time processing
realTime = True
# Toggle spell checking
spell_check = False
# Infinite loop
while True:
# Display live feed until ESC key is pressed
# Press ESC to exit
keypress = cv2.waitKey(1)
# Flip the image laterally
#img = cv2.flip(img, 1)
# TESTING:
#threading.Timer(5.0, printit).start()
# Read a single frame from the live feed
img = live_stream.read()[1]
# Set a region of interest
cv2.rectangle(img, (70, 70), (350, 350), (0,255,0), 2)
# Show the live stream
cv2.imshow("Live Stream", img)
# To get time intervals
if time_counter % 45 == 0 and realTime:
letter, score = predict(img)
#cv2.imshow("Resized Image", img)
print("Letter: ",letter.upper(), " Score: ", score)
print("Current word: ", current_word)
if letter.upper() != 'NOTHING' and letter.upper() != 'SPACE' and letter.upper() != 'DEL':
current_word += letter.upper()
speak_letter(letter)
# Say the letter out loud
elif letter.upper() == 'SPACE':
if len(current_word) > 0:
if spell_check:
speak_letter(spell_checker.correction(current_word))
else:
speak_letter(current_word)
current_word = ""
elif letter.upper() == 'DEL':
if len(current_word) > 0:
current_word = current_word[:-1]
elif letter.upper() == 'NOTHING':
pass
else:
print("UNEXPECTED INPUT: ", letter.upper())
# 'C' is pressed
if keypress == ord('c'):
captureFlag = True
realTime = False
# 'R' is pressed
if keypress == ord('r'):
realTime = True
if captureFlag:
captureFlag = False
# Show the image considered for classification
# Just for Debugging
#cv2.imshow("Resized Image", resized_image)
# Get the letter and the score
letter, score = predict(img)
print("Letter: ",letter.upper(), " Score: ", score)
print("Current word: ", current_word)
if letter.upper() != 'NOTHING' and letter.upper() != 'SPACE' and letter.upper() != 'DEL':
current_word += letter.upper()
speak_letter(letter)
# Say the letter out loud
elif letter.upper() == 'SPACE':
if len(current_word) > 0:
if spell_check:
speak_letter(spell_checker.correction(current_word))
else:
speak_letter(current_word)
current_word = ""
elif letter.upper() == 'DEL':
if len(current_word) > 0:
current_word = current_word[:-1]
elif letter.upper() == 'NOTHING':
pass
else:
print("UNEXPECTED INPUT: ", letter.upper())
# If ESC is pressed
if keypress == 27:
exit(0)
# Update time
time_counter = time_counter + 1
# Stop using camers
live_stream.release()
# Destroy windows created by OpenCV
cv2.destroyAllWindows()