-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
370 lines (321 loc) · 13.3 KB
/
client.py
File metadata and controls
370 lines (321 loc) · 13.3 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import argparse
import configparser
import os
import socket
import threading
import time
import RPi.GPIO as GPIO
import speech_recognition as sr
# from rp_client.speaker import Speaker
# from rp_client.speaker import SpeakersModel
from misc.receiver import Receiver
from misc.sender import Sender
from misc.text_normalizer import to_uniform
from rp_client.TTS import TTS
from rp_client.camera import Camera
from rp_client.keypad import Keypad
from rp_client.ocr import OCR
from rp_client.recognizer import Recognizer
from server.message.add_person_message import AddPersonMessage
from server.message.close_message import CloseMessage
from server.message.end_add_person_message import EndAddPersonMessage
from server.message.face_recognition_message import FaceRecognitionMessage
from server.message.image_to_text_message import ImageToTextMessage
from server.message.ocr_message import OcrMessage
from server.message.register_face_recognition_message import RegisterFaceRecognitionMessage
from server.message.remove_person_message import RemovePersonMessage
from server.message.start_face_recognition_message import StartFaceRecognitionMessage
from server.message.vqa_message import VqaMessage
Running = True
# MESSAGE TYPES
END_ADD_PERSON = 'end-add-person'
VQA = 'visual-question-answering'
IMAGE_TO_TEXT = 'image-to-text'
OCR_MSG = 'ocr'
FACE_RECOGNITION = 'face-recognition'
REMOVE_PERSON = 'remove-person'
ADD_PERSON = 'add-person'
UNKNOWN = 'Unknown'
SET_LAST_PERSON = 'set-last-person'
# user identify
START_FACE = 'start-face-recognition'
REGISTER_FACE = 'register-face-recognition'
class ClientAPI:
def __init__(self, host=socket.gethostname(), port=8888, configFilePath=r'./config.ini'):
self.host = host
self.port = port
self.speaker_name = 'Default'
self.id = 'Default'
self.cam = Camera(width=800, height=600)
self.tts = TTS(festival=False, espeak=False, pico=True)
self.recognizer = Recognizer(server=self, callback_function=self.data_callback)
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.last_person = None
self.configParser = configparser.RawConfigParser()
self.configParser.read(configFilePath)
self.last_msg = None
self.keypad_client = Keypad()
self._images_counter = 0
self.sender = Sender(self.socket, False)
self.receiver = Receiver(self.socket, False)
def handle_capture_button(self):
global Running
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
try:
while Running:
button_state = GPIO.input(23)
if not button_state:
self.add_person()
time.sleep(1)
time.sleep(0.05)
except Exception as e:
print('\033[93m' + 'capture thread stopped' + '\033[0m')
print(e)
GPIO.cleanup()
def add_person(self):
if self.last_person is None:
success = self.data_callback(data_id=SET_LAST_PERSON)
else:
self.tts.say(f'image number is . {self._images_counter} . for user . {self.last_person} . ')
success = self.data_callback(data_id=ADD_PERSON)
if success:
self.tts.say(f'image successfully added . ')
# images count per user
if self._images_counter >= 10:
self.data_callback(data_id=END_ADD_PERSON)
self.last_person = None
self._images_counter = 0
self._images_counter += 1
def start(self):
global Running
try:
self.socket.connect((self.host, self.port))
print('connected to server ' + self.host + ':' + str(self.port))
if self.configParser.get('user-data', 'id') == "":
# First Run
self.data_callback(data_id=REGISTER_FACE)
self.tts.say('Please Say your Name .')
self.speaker_name = self.speech_to_text('./temp/' + self.id + '.wav', mic=True)
self.write_config('id', self.id)
self.write_config('u_name', self.speaker_name)
else:
self.id = self.configParser.get('user-data', 'id')
self.speaker_name = self.configParser.get('user-data', 'u_name')
self.data_callback(data_id=START_FACE)
self.tts.say(f'Welcome {self.speaker_name}')
capture_handler = threading.Thread(
target=self.handle_capture_button,
)
self.keypad_client.start(self.keypad_callback)
capture_handler.start()
# start recogniser
self.recognizer.start()
finally:
self.tts.say('System Shutdown . Good bye')
Running = False
self.recognizer.set_interrupted(True)
print('closing camera')
self.keypad_client.cleanup()
self.cam.close()
self.sender.send(CloseMessage())
# Connection.send_pickle(self.socket, CloseMessage())
print('closing socket')
self.socket.close()
def data_callback(self, fname=None, data_id=None):
"""
data_callback is called when capture button is pressed
or when hot-word detected
:param fname: is recorded audio path after hot-word is detected
'currently contains question audio File else None'
:param data_id: callback message type
"""
message = None
if data_id in [VQA, SET_LAST_PERSON, REMOVE_PERSON]:
# verify speaker
print("converting audio to text")
speech = self.speech_to_text(fname)
if data_id == SET_LAST_PERSON:
if speech != '':
self.last_person = speech
self.tts.say(f'selected user is . {speech}')
return True
else:
message = self._build_message(data_id, text_from_speech=speech)
# os.remove(fname)
else:
message = self._build_message(data_id)
if message is not None:
return self.communicate_with_server(message)
return False
def get_speaker(self, fname):
# Speaker() used for import speaker class only
# Speaker(name='test')
# model: SpeakersModel = SpeakersModel.load("models/gmms.model")
# return model.verify_speaker(fname, self.speaker_name.title())
return 1
def speech_to_text(self, fname=None, mic=False):
r = sr.Recognizer()
if fname is None:
fname = './temp/mic_' + time.strftime("%Y%m%d-%H%M%S") + '.wav'
mic = True
if mic:
self.recognizer.pause()
print('Recording')
with sr.Microphone() as source:
audio = r.listen(source)
# write audio to a WAV file
with open(fname, "wb") as f:
f.write(audio.get_wav_data())
self.recognizer.resume()
else:
with sr.AudioFile(fname) as source:
audio = r.record(source) # read the entire audio file
threshold = 0.5
if self.get_speaker(fname) > threshold:
try:
# print("Sphinx thinks you said " + r.recognize_sphinx(audio))
print("")
except sr.UnknownValueError:
print("Sphinx could not understand audio")
except sr.RequestError as e:
print("Sphinx error; {0}".format(e))
# recognize speech using Google Speech Recognition
googleSTT = ''
try:
googleSTT = r.recognize_google(audio)
print(googleSTT)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
return to_uniform(googleSTT)
else:
print('speaker is not verified')
return ''
def close(self):
self.socket.close()
def communicate_with_server(self, message):
if self.last_msg == OCR_MSG:
response = OCR.get_text(message.image)
else:
self.sender.send(message)
# Connection.send_pickle(self.socket, message)
response = self.receiver.receive()
# response = Connection.receive_json(self.socket)
if self.last_msg == REGISTER_FACE:
self.id = response['result']
# register face response
print('registering')
print(response)
self.say_message(response['result'])
return True
def write_config(self, field, data):
self.configParser.set('user-data', field, data)
with open('config.ini', 'w') as f:
self.configParser.write(f)
def say_message(self, response):
phrase = 'we recognise . '
print(response)
if response.strip() != '':
if self.last_msg == FACE_RECOGNITION:
persons = response.split(',')
unk_count = sum([x.split(' ').count(UNKNOWN) for i, x in enumerate(persons)])
# remove Unknown
persons = [x for i, x in enumerate(persons) if x.split(' ')[0] != UNKNOWN]
for idx, person in enumerate(persons):
person = person.split(' ')
persons[idx] = person[0].replace('_', ' ').title()
persons[idx] += ' . '
# persons[idx] += ' With probability of {} Percent . '.format(person[1])
if unk_count > 0:
persons.append(str(unk_count) + ' Unknown persons . ')
persons_count = len(persons)
for i in range(persons_count):
phrase += persons[i]
phrase += ' And ' if i == persons_count - 2 and persons_count > 1 else ''
elif self.last_msg == IMAGE_TO_TEXT or self.last_msg == OCR_MSG:
phrase += response
elif self.last_msg == VQA:
answers = response.split(',')
answers_count = len(answers)
for i in range(answers_count):
phrase += answers[i].capitalize() + (
' . Or ' if i == answers_count - 2 and answers_count > 1 else ' . ')
else:
phrase = ''
self.tts.say(phrase)
def take_image(self, face_count=0):
self.tts.say('Taking Photo')
img = self.cam.take_image(face_count=face_count)
if img == -1:
print('Sorry,Please Take a new Image.')
self.tts.say('Sorry Please Take a new Image.')
return None
return img
def _build_message(self, type, text_from_speech=None):
self.last_msg = type
if type == ADD_PERSON:
image, _ = self.take_image(face_count=1)
return AddPersonMessage(image) if image is not None else None
if type == OCR_MSG:
_, fname = self.take_image()
return OcrMessage(fname)
elif type == END_ADD_PERSON:
return EndAddPersonMessage(self.last_person)
elif type == REGISTER_FACE:
return RegisterFaceRecognitionMessage(self.id)
elif type == START_FACE:
return StartFaceRecognitionMessage(self.id)
elif type == REMOVE_PERSON:
return RemovePersonMessage(text_from_speech)
elif type == IMAGE_TO_TEXT:
image, _ = self.take_image()
return ImageToTextMessage(image) if image is not None else None
elif type == VQA:
image, _ = self.take_image()
return VqaMessage(image, text_from_speech) if image is not None else None
elif type == FACE_RECOGNITION:
image, _ = self.take_image()
return FaceRecognitionMessage(image) if image is not None else None
return None
def keypad_callback(self, key):
global Running
print(key)
callbacks = [
lambda: self.data_callback(data_id=FACE_RECOGNITION),
lambda: self.data_callback(data_id=VQA),
lambda: self.data_callback(data_id=IMAGE_TO_TEXT),
lambda: self.data_callback(data_id=OCR_MSG),
lambda: self.add_person(),
lambda: self.data_callback(data_id=REMOVE_PERSON),
]
if key in range(1, 6):
print(callbacks[key - 1])
callbacks[key - 1]()
elif key == 'A':
Running = False
time.sleep(5)
os.execv('/home/pi/myFolder/RestartMySelf.py', [''])
elif key == 'B':
Running = False
os.system('sudo reboot')
elif key == 'C':
Running = False
os.system('sudo shutdown now')
def main(args):
api = ClientAPI(host=args.host, port=args.port)
try:
api.start()
finally:
api.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--host', type=str,
default='192.168.1.4')
parser.add_argument('--port', type=int,
default=8888)
parser.add_argument('--config', type=str,
default='config.ini')
arguments = parser.parse_args()
main(arguments)