forked from nthuy190991/facial_recognition_on_Pepper
/
facial_recog_by_pepper.py
1223 lines (982 loc) · 48.2 KB
/
facial_recog_by_pepper.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import numpy as np
import os, sys
import cv2
import time
from read_xls import read_xls
import xlrd
from threading import Thread
from flask import Flask, request, render_template
import operator
from binascii import b2a_hex
from watson_developer_cloud import NaturalLanguageClassifierV1
import face_api
import emotion_api
import qi # Aldebaran Python SDK
"""
Replace French accents in texts
"""
def replace_accents(text):
chars_origine = ['Ê','à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ò', 'ó', 'ô', 'õ', 'ö', 'ù', 'ú', 'û', 'ü']
chars_replace = ['\xC3','\xE0', '\xE1', '\xE2', '\xE3', '\xE4', '\xE5', '\xE6', '\xE7', '\xE8', '\xE9', '\xEA', '\xEB', '\xEC', '\xED', '\xEE', '\xEF', '\xF2', '\xF3', '\xF4', '\xF5', '\xF6', '\xF9', '\xFA', '\xFB', '\xFC']
text2 = str_replace_chars(text, chars_origine, chars_replace)
return text2
def replace_accents2(text):
chars_origine = ['Ê','à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ò', 'ó', 'ô', 'õ', 'ö', 'ù', 'ú', 'û', 'ü']
chars_replace = ['E','a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u']
text2 = str_replace_chars(text, chars_origine, chars_replace)
return text2
"""
Replace characters in a string
"""
def str_replace_chars(text, chars_origine, chars_replace):
for i in range(len(chars_origine)):
text2 = text.replace(chars_origine[i], chars_replace[i])
text = text2
return text2
"""
==============================================================================
Face and Emotion API
==============================================================================
"""
def retrieve_face_emotion_att(clientId):
global global_vars
global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()
# Face API
# faceResult = face_api.faceDetect(None, None, data)
faceResult = face_api.faceDetect(None, 'output.png', None)
# Emotion API
# emoResult = emotion_api.recognizeEmotion(None, None, data)
emoResult = emotion_api.recognizeEmotion(None, 'output.png', None)
# Results
print 'Found {} '.format(len(faceResult)) + ('faces' if len(faceResult)!=1 else 'face')
nb_faces = len(faceResult)
tb_face_rect = [{} for ind in range(nb_faces)]
tb_age = ['' for ind in range(nb_faces)]
tb_gender = ['' for ind in range(nb_faces)]
tb_glasses = ['' for ind in range(nb_faces)]
tb_emo = ['' for ind in range(len(emoResult))]
if (len(faceResult)>0 and len(emoResult)>0):
ind = 0
for currFace in faceResult:
faceRectangle = currFace['faceRectangle']
faceAttributes = currFace['faceAttributes']
tb_face_rect[ind] = faceRectangle
tb_age[ind] = str(faceAttributes['age'])
tb_gender[ind] = faceAttributes['gender']
tb_glasses[ind] = faceAttributes['glasses']
ind += 1
ind = 0
for currFace in emoResult:
tb_emo[ind] = max(currFace['scores'].iteritems(), key=operator.itemgetter(1))[0]
ind += 1
faceWidth = np.zeros(shape=(nb_faces))
faceHeight = np.zeros(shape=(nb_faces))
for ind in range(nb_faces):
faceWidth[ind] = tb_face_rect[ind]['width']
faceHeight[ind] = tb_face_rect[ind]['height']
ind_max = np.argmax(faceWidth*faceHeight.T)
global_var['age'] = tb_age[ind_max]
global_var['gender'] = tb_gender[ind_max]
global_var['emo'] = tb_emo[ind_max]
return tb_age, tb_gender, tb_glasses, tb_emo
else:
return 'N/A','N/A','N/A','N/A'
"""
Yield Face and Emotion API results
"""
def get_face_emotion_api_results(clientId):
resp = detect_face_attributes(clientId)
if (resp==1):
print 'Calling APIs to retrieve facial and emotional attributes, please wait'
tb_age, tb_gender, tb_glasses, tb_emo = retrieve_face_emotion_att(clientId)
if ([tb_age, tb_gender, tb_glasses, tb_emo] != ['N/A','N/A','N/A','N/A']):
# Translate emotion to french
tb_emo_eng = ['happiness', 'sadness', 'surprise', 'anger', 'fear',
'contempt', 'disgust', 'neutral']
tb_emo_correspond = ['joyeux', 'trist', 'surprise',
'en colère', "d'avoir peur", ' mépris',
'dégoût', 'neutre']
# Translate glasses to french
tb_glasses_eng = ['NoGlasses', 'ReadingGlasses',
'sunglasses', 'swimmingGoggles']
tb_glasses_correspond = ['ne portez pas de lunettes',
'portez des lunettes',
'portez des lunettes de soleil',
'portez des lunettes de natation']
for ind in range(len(tb_age)):
glasses_str = tb_glasses_correspond[tb_glasses_eng.index(tb_glasses[ind])]
emo_str = tb_emo_correspond[tb_emo_eng.index(tb_emo[ind])]
textToSpeak = "Bonjour " + ('Monsieur' if tb_gender[ind] =='male' else 'Madame') + \
", vous avez " + tb_age[ind].replace('.',',') + " ans, votre état d'émotion est " + emo_str + \
", et vous " + glasses_str
simple_message(clientId, 'Attributs faciales', textToSpeak)
else:
print 'Found no faces'
simple_message(clientId, 'Attributs faciales', u'Désolé, aucun visage trouvé')
"""
Ask a name or id as a string
"""
def ask_name(clientId, flag):
global global_vars
global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()
global_var['text'] = ''
global_var['text2'] = ''
global_var['text3'] = "Donnez-moi votre identifiant, s'il vous plait !"
if (flag):
simple_message(clientId, '', global_var['text3'])
while (global_var['textFromHTML']==""):
pass
res = global_var['textFromHTML']
global_var['textFromHTML'] = ""
return res
"""
Using Haar Cascade detector to detect faces from a grayscale image
"""
def detect_faces(faceCascade, gray):
faces = faceCascade.detectMultiScale(
gray,
scaleFactor = 1.1,
minNeighbors = 5,
minSize = (50, 50),
flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)
return faces
"""
Get all images in database alongside with their labels
"""
def get_images_and_labels(path, list_nom):
image_paths = [os.path.join(path, f) for f in os.listdir(path)]
images = [] # images will contains face images
labels = [] # labels which are assigned to the image
for image_path in image_paths:
# Read the image
image = cv2.imread(image_path)
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Get the label of the image
nom = os.path.split(image_path)[1].split(".")[0]
if nom not in list_nom:
list_nom.append(nom)
nbr = list_nom.index(nom) + 1
images.append(gray)
labels.append(nbr)
# return the images list and labels list
return images, labels
"""
==============================================================================
Flask Initialization
==============================================================================
"""
def flask_init():
global app
app = Flask(__name__)
@app.route('/')
def render_hmtl():
return render_template('index_old.html')
@app.route('/start/<clientId>', methods=['POST'])
def onStart(clientId):
global_var_init(clientId)
# global global_vars
# global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()
global flag_pepper_start
flag_pepper_start = False
print flag_pepper_start
# Pepper
thread_pepper = Thread(target=run_app_pepper, args=(clientId,), name='pepper_'+str(clientId))
thread_pepper.start()
# run_program
#time.sleep(1)
while (not flag_pepper_start):#global_var['flag_pepper_start']:
time.sleep(0.5)
print flag_pepper_start
thread_program = Thread(target = run_program, args= (clientId,), name = 'thread_prog_'+clientId)
thread_program.start()
return "", 200
@app.route('/StT/<data>', methods=['POST'])
def runSpeechToText(data):
clientId = data[0:5]
text = data[6:]
global global_vars
global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()
global_var['stt'] = text
return "", 200
@app.route('/textFromHTML/<data>', methods=['POST'])
def getTextFromHTML(data):
clientId = data[0:5]
text = data[6:]
global global_vars
global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()
global_var['textFromHTML'] = text
#print 'textFromHTML', clientId, text
return "", 200
@app.route('/longpolling/<clientId>', methods=['POST'])
def longPolling(clientId):
time.sleep(0.5)
global global_vars
global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()
temp = global_var['todo']
global_var['todo'] = ""
return temp, 200
"""
Pepper
"""
class Pepper(object):
def __init__(self):
self.ip = "10.69.128.84"
self.port = 9559
self.session = qi.Session()
try:
self.session.connect("tcp://" + self.ip + ":" + str(self.port))
except RuntimeError:
print ( "Can't connect to Naoqi at ip \"" + self.ip + "\" on port " + str(self.port) +".\n"
"Please check your script arguments. Run with -h option for help.")
sys.exit(1)
self.ALTextToSpeech = self.session.service('ALTextToSpeech')
self.ALTextToSpeech.setLanguage('French')
self.ALTextToSpeech.setParameter('speed', 110)
self.ALVideoDevice = self.session.service('ALVideoDevice')
self.ALVideoDevice.unsubscribe("CameraTop_0")
self.ALVideoDevice.setParameter(0, 14, 2)
self.handle = self.ALVideoDevice.subscribeCamera("CameraTop", 0, 2, 11, 5)
def run_camera(self, clientId):
global global_vars
global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()
while True:
self.ALVideoDevice.releaseImage(self.handle)
data_pepper = self.ALVideoDevice.getImageRemote(self.handle)
width, height, nbLayers = data_pepper[0:3] # from Documentation
image_data = np.zeros((len(data_pepper[6]),1)) # data[6]: array of height*width*nbLayes containing image data
data_bin = b2a_hex(str(data_pepper[6]))
for k in range(0,len(data_pepper[6])):
image_data[k] = int(data_bin[2*k:2*k+2], 16)
image_reshape = np.reshape(image_data, (nbLayers, width, height), order='F')
#TODO: re-check when new Pepper comes
# data_uint8 = np.fromstring(data_pepper[6], np.uint8)
# image_reshape = np.reshape(data_uint8, (nbLayers, width, height), order='F')
imgRGB = np.dstack((image_reshape[2].T,image_reshape[1].T,image_reshape[0].T))
cv2.imwrite('output.png', imgRGB)
frame = cv2.imread('output.png')
global_var['frameFromHTML'] = frame
def pepper_tts(self, clientId, text):
global global_vars
global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()
global_var['todo'] = "TTS " + str(clientId) + " " + text
self.ALTextToSpeech.say(text)
global_var['tts'] = text
# Calculate the time needed to wait, until the TTS is finished
text2 = str_replace_chars(text, [' ?',' !',' :',' ;'], ['?','!',':',';'])
nbOfWords = len(text2.split())
timeNeeded = float(nbOfWords)/120*60 # Average words-per-min = 130
time.sleep(timeNeeded)
def run_app_pepper(clientId):
global app_pepper, flag_pepper_start
# global global_vars
# global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()
app_pepper = Pepper()
# global_var['flag_pepper_start']= True
flag_pepper_start = True
print flag_pepper_start
app_pepper.run_camera(clientId)
"""
==============================================================================
Dialogue from Chrome
==============================================================================
"""
def chrome_tts(clientId, text): # Text-to-Speech
global global_vars
global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()
global_var['todo'] = "TTS " + str(clientId) + " " + text
global_var['tts'] = text
# Calculate the time needed to wait, until the TTS is finished
text2 = str_replace_chars(text, [' ?',' !',' :',' ;'], ['?','!',':',';'])
nbOfWords = len(text2.split())
rate = 1.1 # speech rate (which is set in index.html)
timeNeeded = float(nbOfWords)/130/rate*60 # Average words-per-min = 130
time.sleep(timeNeeded)
def chrome_stt(clientId): # Speech-to-Text
global global_vars
global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()
global_var['todo'] = 'STT'
global_var['stt'] = ''
t0 = time.time()
while (global_var['stt'] == ''):
pass
if (time.time()-t0>=8): # Time outs after 8 secs
global_var['stt'] = '@' # Silence
resp = global_var['stt']
return resp
def chrome_yes_or_no(clientId, question):
#chrome_tts(clientId, question) # Ask a question
app_pepper.pepper_tts(clientId, question)
global global_vars
global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()
t0 = time.time()
while ((global_var['textFromHTML']=="") and (time.time()-t0<5)):
pass
response = global_var['textFromHTML'] # Get answer from userInput during 3 seconds
global_var['textFromHTML'] = ""
if (response == ""):
response = chrome_stt(clientId) # Listen for an answer
# if not(global_var['flag_quit']):
# if ('oui' in response):
# result = 1
# elif ('non' in response):
# result = 0
# elif (response == '@'):
# result, response = chrome_yes_or_no(clientId, u"Je ne vous entends pas, veuillez répéter")
# else:
# result, response = chrome_yes_or_no(clientId, u"Je ne vous comprends pas, veuillez répéter")
# else:
# result = -1
# response = ''
if (response == '@'):
result, response = chrome_yes_or_no(clientId, u"Je ne vous entends pas, veuillez répéter")
if (response=='oui' or response=='non'):
responseYesOrNo = response
else:
classes = natural_language_classifier.classify('2374f9x68-nlc-1265', response)
responseYesOrNo = classes["top_class"]
if not(global_var['flag_quit']):
if (responseYesOrNo=='oui'):
result = 1
elif (responseYesOrNo=='non'):
result = 0
elif (responseYesOrNo=='not_relevant'):
result, response = chrome_yes_or_no(clientId, u"Votre réponse n'est pas pertinente, veuillez ré-répondre")
# else:
# result = -1
# responseYesOrNo = ''
return result, response
"""
==============================================================================
Streaming Video: runs streaming video independently with other activities
==============================================================================
"""
def video_streaming(clientId):
global global_vars
global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()
time_origine = time.time()
while True:
frame = global_var['frameFromHTML'] # Get frame from HTML
#frame = cv2.flip(frame, 1) # Vertically flip frame
global_var['key'] = cv2.waitKey(1)
if (global_var['key'] == 27 or global_var['key2'] == 27): # wait for ESC key to exit
cv2.destroyWindow('ClientId: ' + str(clientId) + ' - Video streaming')
global_var['flag_quit'] = 1 # Use global_vars
break
"""
Face Detection part
"""
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Convert frame to a grayscale image
faces = detect_faces(faceCascade, gray) # Detect faces on grayscale image
"""
Recognition part
"""
for (x, y, w, h) in faces:
if (len(faces)>1): # Consider only the biggest face appears in the video
w_vect = faces.T[2,:]
h_vect = faces.T[3,:]
x0, y0, w0, h0 = faces[np.argmax(w_vect*h_vect)]
elif (len(faces)==1): # If there is only one face
x0, y0, w0, h0 = faces[0]
if not global_var['flag_disable_detection']:
cv2.rectangle(frame, (x0, y0), (x0+w0, y0+h0), (25, 199, 247), 1) # Draw a rectangle around the biggest face
#cv2.rectangle(frame, (x, y), (x+w, y+h), (25, 199, 247), 1) # Draw a rectangle around the faces
if (len(faces)>=1):
global_var['image_save'] = gray[y0 : y0 + h0, x0 : x0 + w0]
nbr_predicted, conf = recognizer.predict(global_var['image_save']) # Predict function
nom = list_nom[nbr_predicted-1] # Get resulting name
if (conf < thres): # if recognizing distance is less than the predefined threshold -> FACE RECOGNIZED
if not global_var['flag_disable_detection']:
txt = nom + ', distance: ' + str(conf)
message_xy(frame, txt, x0, y0-5, 'w', 1)
global_var['tb_nb_times_recog'][nbr_predicted-1] = global_var['tb_nb_times_recog'][nbr_predicted-1] + 1 # Increase nb of recognize times
message_xy(frame, global_var['age'], x0+w0, y0, 'b', 1)
message_xy(frame, global_var['gender'], x0+w0, y0+10, 'b', 1)
message_xy(frame, global_var['emo'], x0+w0, y0+20, 'b', 1)
# End of For-loop
# Texts to display on video
count_time = time.time() - time_origine
fps = count_fps()
message(frame, "Time: " + str(count_time)[0:4], 0, 1, 'g', 2)
message(frame, "FPS: " + str(fps)[0:5], 0, 2, 'g', 1)
message(frame, global_var['text'], 0, 3, 'g', 1)
message(frame, global_var['text2'], 0, 4, 'g', 1)
message(frame, global_var['text3'], 0, 5, 'g', 1)
# Frame display
cv2.imshow('ClientId: ' + str(clientId) + ' - Video streaming', frame)
cv2.destroyWindow('ClientId: ' + str(clientId) + ' - Video streaming')
"""
Put Texts on frame to display on streaming video at a predefined position (row,column)
"""
def message(frame, text, col, line, color, thickness):
height, width = frame.shape[:2]
if (col==0): x = 10
if (line==1):
y = 20
elif (line==2):
y = 40
elif (line==3):
y = height-50
elif (line==4):
y = height-30
elif (line==5):
y = height-10
message_xy(frame, text, x, y, color, thickness)
"""
Put texts on frame to display on streaming video at position (x,y)
"""
def message_xy(frame, text, x, y, color, thickness):
if color=='r':
rgb = (0, 0, 255)
elif color=='g':
rgb = (0, 255, 0)
elif color=='b':
rgb = (255, 0, 0)
elif color=='w':
rgb = (255, 255, 255)
cv2.putText(frame, text, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.0, rgb, thickness, lineType=cv2.CV_AA)
"""
Display Formation Panel for a recognized or username-known user
"""
def go_to_formation(clientId, xls_filename, name):
global global_vars
global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()
global_var['flag_disable_detection'] = 1 # Disable the detection when entering Formation page
global_var['flag_enable_recog'] = 0
tb_formation = read_xls(xls_filename, 0) # Read Excel file which contains Formation info
mail = reform_username(name) # Find email from name
global_var['text'] = "Bonjour " + str(name)
if (mail == '.'):
global_var['text2'] = "Votre information n'est pas disponible !"
global_var['text3'] = "Veuillez contacter contact@orange.com"
else:
mail_idx = tb_formation[0][:].index('Mail')
# Get mail list
mail_list = []
for idx in range(0, len(tb_formation)):
mail_list.append(tb_formation[idx][mail_idx])
ind = mail_list.index(mail) # Find user in xls file based on his/her mail
date = xlrd.xldate_as_tuple(tb_formation[ind][tb_formation[0][:].index('Date du jour')],0)
text2 = "Bienvenue à la formation de "+str(tb_formation[ind][tb_formation[0][:].index('Prenom')])+" "+str(tb_formation[ind][tb_formation[0][:].index('Nom')] + ' !')
text3 = "Vous avez un cours de " + str(tb_formation[ind][tb_formation[0][:].index('Formation')]) + ", dans la salle " + str(tb_formation[ind][tb_formation[0][:].index('Salle')]) + ", à partir du " + "{}/{}/{}".format(str(date[2]), str(date[1]),str(date[0]))
global_var['text2'] = replace_accents2(text2)
global_var['text3'] = replace_accents2(text3)
simple_message(clientId, 'Page Formation', text2 + ' ' + text3)
return global_var['text'], global_var['text2'], global_var['text3']
"""
Return to recognition program after displaying Formation
"""
def return_to_recog(clientId):
global global_vars
global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()
if not global_var['flag_quit']:
cv2.waitKey(5000)
resp_quit_formation = quit_formation(clientId)
if (resp_quit_formation == 0):
time.sleep(5) # wait for more 5 seconds before quitting
global_var['flag_disable_detection'] = 0
global_var['flag_enable_recog'] = 1
global_var['flag_ask'] = 1
global_var['flag_reidentify'] = 0
"""
Find valid username
"""
def reform_username(name):
if (name=='huy' or name=='huy_new'):
firstname = 'thanhhuy'
lastname = 'nguyen'
email_suffix = '@orange.com'
elif (name=='cleblain'):
firstname = 'christian'
lastname = 'leblainvaux'
email_suffix = '@orange.com'
elif (name=='catherine' or name=='lemarquis'):
firstname = 'catherine'
lastname = 'lemarquis'
email_suffix = '@orange.com'
elif (name=='ionel'):
firstname = 'ionel'
lastname = 'tothezan'
email_suffix = '@orange.com'
else:
firstname = ''
lastname = ''
email_suffix = ''
mail = firstname + '.' + lastname + email_suffix
return mail
"""
==============================================================================
Taking photos
==============================================================================
"""
def take_photos(clientId, step_time, flag_show_photos):
global global_vars
global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()
name = ask_name(clientId, 1)
image_to_paths = [imgPath+str(name)+"."+str(i)+suffix for i in range(nb_img_max)]
if os.path.exists(imgPath+str(name)+".0"+suffix):
print u"Les fichiers avec le nom " + str(name) + u" existent déjà"
b = yes_or_no(clientId,"Existence de fichiers", u"Les fichiers avec le nom " + str(name) + u" existent déjà, écraser ces fichiers ?", 3)
if (b==1):
for image_del_path in image_to_paths:
os.remove(image_del_path)
elif (b==0):
name = ask_name(clientId, 1)
image_to_paths = [imgPath + str(name)+"."+str(i)+suffix for i in range(nb_img_max)]
global_var['text'] = 'Prenant photos'
global_var['text2'] = 'Veuillez patienter... '
simple_message(clientId, '', global_var['text']+', '+global_var['text2'])
nb_img = 0
while (nb_img < nb_img_max):
image_path = image_to_paths[nb_img]
cv2.imwrite(image_path, global_var['image_save'])
print "Enregistrer photo " + image_path + ", nb de photos prises : " + str(nb_img+1)
global_var['text3'] = str(nb_img+1) + ' ont ete prises, reste a prendre : ' + str(nb_img_max-nb_img-1)
nb_img += 1
time.sleep(step_time)
# Display photos that has just been taken
if flag_show_photos:
thread_show_photos = Thread(target = show_photos, args = (clientId, imgPath, name), name = 'thread_show_photos_'+clientId)
thread_show_photos.start()
time.sleep(0.5)
# Allow to retake photos and validate after finish taking
thread_retake_validate_photos = Thread(target = retake_validate_photos, args = (clientId, step_time, flag_show_photos, imgPath, name), name = 'thread_retake_validate_photos_'+clientId)
thread_retake_validate_photos.start()
"""
Retaking and validating photos
"""
def retake_validate_photos(clientId, step_time, flag_show_photos, imgPath, name):
global global_vars
global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()
# Ask users if they want to change photo(s) or validate them
b = validate_photo(clientId)
image_to_paths = [root_path+imgPath+str(name)+"."+str(j)+suffix for j in range(nb_img_max)]
while (b==0):
global_var['text3'] = "Veuillez repondre"
simple_message(clientId, '', u"Veuillez répondre quelles photos que vous voulez changer ?")
while (global_var['textFromHTML'] == ""):
pass
nb = global_var['textFromHTML']
global_var['textFromHTML'] = ""
if ('-' in nb):
nb2 = ''
for i in range(int(nb[0]), int(nb[2])+1):
nb2 = nb2 + str(i)
nb = nb2
nb = str_replace_chars(nb, [',',';','.',' '], ['','','',''])
if (nb!=""):
str_nb = ""
for j in range(0, len(nb)):
if (j==len(nb)-1):
str_nb = str_nb + "'" + nb[j] + "'"
else:
str_nb = str_nb + "'" + nb[j] + "', "
simple_message(clientId, 'Reprise de photos', 'Vous souhaitez changer les photos: ' + str_nb + ' ?')
global_var['text'] = 'Prenant photos'
global_var['text2'] = 'Veuillez patienter... '
global_var['text3'] = ''
for j in range(0, len(nb)):
global_var['text3'] = str(j) + ' ont ete prises, reste a prendre : ' + str(len(nb)-j)
time.sleep(step_time)
print "Reprendre photo ", nb[j]
image_path = image_to_paths[int(nb[j])-1]
os.remove(image_path) # Remove old image
cv2.imwrite(image_path, global_var['image_save'])
print "Enregistrer photo " + image_path + ", nb de photos prises : " + nb[j]
a = yes_or_no(clientId, 'Nouvelles photos', u'Reprise de photos finie, souhaitez-vous réviser vos photos ?', 4)
if (a==1):
thread_show_photos2 = Thread(target = show_photos, args = (clientId, imgPath, name), name = 'thread_show_photos2_'+clientId)
thread_show_photos2.start()
b = validate_photo(clientId)
global_var['text'] = ''
global_var['text2'] = ''
global_var['text3'] = ''
if (b==1):
break
# End of While(b==0)
# Update recognizer after taking and validating photos
images, labels = get_images_and_labels(imgPath, list_nom)
recognizer.update(images, np.array(labels))
print u"Recognizer a été mis a jour..."
global_var['flag_enable_recog'] = 1 # Re-enable recognition
global_var['flag_ask'] = 1 # Reset asking
"""
Display photos that have just been taken, close them if after 5 seconds or press any key
"""
def show_photos(clientId, imgPath, name):
x = 100; y = 600
image_to_paths = [root_path + imgPath + str(name) + "." + str(j) + suffix for j in range(nb_img_max)]
ind = 1
for img_path in image_to_paths:
#print img_path
img = cv2.imread(img_path)
cv2.imshow('clientId '+clientId+' - Photo '+str(ind), img)
height, width = img.shape[:2]
cv2.moveWindow('clientId '+clientId+' - Photo '+str(ind), x, y)
x += width
ind += 1
cv2.waitKey(7000) # wait a key for 7 seconds
for ind in range(nb_img_max):
cv2.destroyWindow('clientId '+clientId+' - Photo '+str(ind+1))
"""
==============================================================================
Re-identification: when a user is not recognized or not correctly recognized
==============================================================================
"""
def re_identification(clientId, nb_time_max, name0):
simple_message(clientId, 'Autre positionnement', u'Veuillez rapprocher vers la camera, ou bouger votre tête')
global global_vars
global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()
tb_old_name = np.chararray(shape=(nb_time_max+1), itemsize=10) # Old recognition results, which are wrong
tb_old_name[:] = ''
tb_old_name[0] = name0
nb_time = 0
global_var['flag_enable_recog'] = 1
global_var['flag_reidentify'] = 1
time.sleep(wait_time) # wait until after the first re-identification is done
global_var['flag_ask'] = 0
a = 0
while (nb_time < nb_time_max):
name1 = global_var['nom'] # New result
# TODO: if unknown person --> Count instead of retrying (done, but should be verified)
if np.all(tb_old_name != name1) and global_var['flag_recog']: # if new result is different to old results
print 'Essaie ' + str(nb_time+1) + ': reconnu comme ' + str(name1)
if (a==0):
a = 1 # Small trick to not to ask twice (not start two Speech Recognizer) at the same time
resp = validate_recognition(clientId, name1)
if (resp == 1):
a = 0
result = 1
name = name1
break
else:
result = 0
a = 0
nb_time += 1
tb_old_name[nb_time] = name1
time.sleep(wait_time)
elif (not global_var['flag_recog']):
print 'Essaie ' + str(nb_time+1) + ': personne inconnue'
a = 0
result = 0
nb_time += 1
time.sleep(wait_time)
if (result==1): # User confirms that the recognition is correct now
global_var['flag_enable_recog'] = 0
global_var['flag_reidentify'] = 0
global_var['flag_wrong_recog'] = 0
get_face_emotion_api_results(clientId)
global_var['text'], global_var['text2'], global_var['text3'] = go_to_formation(clientId, xls_filename, name)
return_to_recog(clientId) # Return to recognition program immediately or 20 seconds before returning
else: # Two time failed to recognized
global_var['flag_enable_recog'] = 0 # Disable recognition when two tries have failed
global_var['flag_reidentify'] = 0
simple_message(clientId, u'Problème méconnaissable', u'Désolé je vous reconnaît pas, veuillez me donner votre identifiant')
name = ask_name(clientId, 1)
if os.path.exists(imgPath+str(name)+".0"+suffix): # Assume that user's face-database exists if the photo 0.png exists
simple_message(clientId, 'Reprise de photos', 'Bonjour '+ str(name)+', je vous conseille de changer vos photos')
flag_show_photos = 1
step_time = 1
thread_show_photos3 = Thread(target = show_photos, args = (clientId, imgPath, name), name = 'thread_show_photos3_'+clientId)
thread_show_photos3.start()
time.sleep(0.5)
thread_retake_validate_photos2 = Thread(target = retake_validate_photos, args = (clientId, step_time, flag_show_photos, imgPath, name), name = 'thread_retake_validate_photos2_'+clientId)
thread_retake_validate_photos2.start()
else:
simple_message(clientId, 'Erreur', "Malheureusement, les photos correspondant au nom "+ str(name) +" n'existent pas. Je vous conseille de reprendre vos photos")
global_var['flag_take_photo'] = 1 # Enable photo taking
"""
==============================================================================
Main program body with decision and redirection
==============================================================================
"""
def run_program(clientId):
global global_vars
global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()
# Autorisation to begin Streaming Video
optin0 = allow_streaming_video(clientId)
if (optin0 == 1):
global_var['key'] = 0
global_var['flag_quit'] = 0
# Thread of streaming video
thread_video = Thread(target = video_streaming, args = (clientId,), name = 'thread_video_' + clientId)
thread_video.start()
start_time = time.time() # For recognition timer (will reset after each 3 secs)
time_origine = time.time() # For display (unchanged)
"""
Permanent loop
"""
while True:
# Break While-loop and quit program as soon as the Esc key is pressed
if (global_var['key'] == 27):
break
"""
Decision part
"""
if not (global_var['flag_quit']): #TODO: new
global_var['key2'] = cv2.waitKey(1)
if (global_var['key2'] == 27):# or HTML_refresh:
break
elapsed_time = time.time() - start_time
if ((elapsed_time > wait_time) and global_var['flag_enable_recog']): # Identify after each 3 seconds
if (max(global_var['tb_nb_times_recog']) >= nb_max_times/2): # If the number of times recognized is big enough
global_var['flag_recog'] = 1 # Known Person
global_var['flag_ask'] = 0
global_var['nom'] = list_nom[np.argmax(global_var['tb_nb_times_recog'])] # Get name of recognizing face
global_var['text'] = 'Reconnu : ' + global_var['nom']
if (not global_var['flag_reidentify']):
global_var['text2'] = "Appuyez [Y] si c'est bien vous"
global_var['text3'] = "Appuyez [N] si ce n'est pas vous"
res_verify_recog = verify_recog(clientId, global_var['nom'])
if (res_verify_recog==1):
global_var['key'] = ord('y')
elif (res_verify_recog==0):
global_var['key'] = ord('n')
else: # If the number of times recognized anyone from database is too low
global_var['flag_recog'] = 0 # Unknown Person
#nom = '' # XXX: new: à vérifier
global_var['text'] = 'Personne inconnue'
global_var['text2'] = ''
global_var['text3'] = ''
if (not global_var['flag_reidentify']):
global_var['flag_ask'] = 1
simple_message(clientId, '', u'Désolé, je ne vous reconnaît pas')
global_var['tb_nb_times_recog'].fill(0) # reinitialize with all zeros
start_time = time.time() # reset timer
"""
Redirecting user based on recognition result and user's status (already took photos or not) in database
"""
count_time = time.time() - time_origine
if (count_time <= wait_time):
global_var['text3'] = 'Initialisation (pret dans ' + str(wait_time-count_time)[0:4] + ' secondes)...'
if (global_var['flag_quit']):
break
else:
"""
Start Redirecting after the first 3 seconds
"""
if (global_var['flag_quit']):
break
if (global_var['flag_recog']):
if (global_var['key']==ord('y') or global_var['key']==ord('Y')): # User chooses Y to go to Formation page
global_var['flag_wrong_recog'] = 0
get_face_emotion_api_results(clientId)
global_var['text'], global_var['text2'], global_var['text3'] = go_to_formation(clientId, xls_filename, global_var['nom'])
global_var['key'] = 0
return_to_recog(clientId) # Return to recognition program, after displaying Formation
if (global_var['key']==ord('n') or global_var['key']==ord('N')): # User confirms that the recognition result is wrong by choosing N
global_var['flag_wrong_recog'] = 1
global_var['flag_ask'] = 1
global_var['key'] = 0
if ((global_var['flag_recog'] and global_var['flag_wrong_recog']) or (not global_var['flag_recog'])): # Not recognized or not correctly recognized
if (global_var['flag_ask']):# and (not flag_quit)):
resp_deja_photos = deja_photos(clientId) # Ask user if he has already had a database of face photos
# if (resp_deja_photos==-1):
# global_var['flag_ask'] = 0
elif (resp_deja_photos==1): # User has a database of photos
global_var['flag_enable_recog'] = 0 # Disable recognition in order not to recognize while re-identifying
global_var['flag_ask'] = 0
name0 = global_var['nom'] # Save the recognition result, which is wrong, in order to compare later
nb_time_max = 5 # Number of times to retry recognize
thread_reidentification = Thread(target = re_identification, args = (clientId, nb_time_max, name0), name = 'thread_reidentification_'+clientId)
thread_reidentification.start()
elif (resp_deja_photos == 0): # User doesnt have a database of photos
global_var['flag_enable_recog'] = 0 # Disable recognition in order not to recognize while taking photos
resp_allow_take_photos = allow_take_photos(clientId)
if (resp_allow_take_photos==1): # User allows to take photos
global_var['flag_take_photo'] = 1 # Enable photo taking
#flag_enable_recog = 0 # Stop recognition while taking photos
else: # User doesnt want to take photos
global_var['flag_take_photo'] = 0
res = allow_go_to_formation_by_id(clientId)
if (res==1): # User agrees to go to Formation in providing his id manually
name = ask_name(clientId, 1)
global_var['text'], global_var['text2'], global_var['text3'] = go_to_formation(clientId, xls_filename, name)
# Return to recognition program (if user wishs to, otherwise, wait 20 seconds before returning anyway)
return_to_recog(clientId)
else: # Quit if user refuses to provide manually his id (after all other functionalities)
break
resp_allow_take_photos = 0
resp_deja_photos = 0
global_var['flag_ask'] = 0