forked from nthuy190991/facial_recognition_on_Bluemix
/
facial_recog_server_using_oxford_final.py
1271 lines (1015 loc) · 48.4 KB
/
facial_recog_server_using_oxford_final.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 -*-
"""
Created on Fri Jun 03 05:22:08 2016
@author: thnguyen
"""
import numpy as np
import os, sys
import time
from read_xls import read_xls
#from edit_xls import edit_xls
import xlrd
from threading import Thread
from flask import Flask, request, render_template, send_from_directory
import operator
from binascii import a2b_base64, b2a_base64
from watson_developer_cloud import NaturalLanguageClassifierV1
import face_api
import emotion_api
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from datetime import datetime
import requests
_username = 'thanhhuynguyenorange'
_password = 'GGQN0871abc'
_url_github = 'https://api.github.com/repos/nthuy190991/facial_recognition_on_Bluemix/contents/'
def put_image_to_github(image_path, data):
headers = {
'Content-Type': 'application/json'
}
json = {
"message": "bluemix",
"committer": {
"name": "thanhhuynguyenorange",
"email": "thanhhuy.nguyen@orange.com"
},
"content": b2a_base64(data)
}
url = _url_github + image_path
response = requests.put(url, headers=headers, json=json, auth=(_username, _password))
result = response.json() if response.content else None
return result
def get_image_from_github(image_path):
url = _url_github + image_path
response = requests.get(url, auth=(_username, _password))
# result = response.json() if response.content else None
if (response.content):
result = response.json()
data_read = result['content']
else:
data_read = None
binary_data = a2b_base64(data_read)
return binary_data
def delete_image_on_github(image_path):
url = _url_github + image_path
response = requests.get(url, auth=(_username, _password))
result = response.json() if response.content else None
sha_img = result['sha']
headers = {
'Content-Type': 'application/json'
}
json = {
"message": "bluemix",
"committer": {
"name": "thanhhuynguyenorange",
"email": "thanhhuy.nguyen@orange.com"
},
"sha": sha_img
}
response = requests.delete(url, headers=headers, json=json, auth=(_username, _password))
result = response.json() if response.content else None
return result
"""
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()
data = global_var['binary_data']
chrome_server2client(clientId, 'Veuillez patienter pendant quelques secondes...')
time.sleep(0.5)
chrome_server2client(clientId, 'START')
# Face API
faceResult = face_api.faceDetect(None, None, data)
# Emotion API
emoResult = emotion_api.recognizeEmotion(None, None, data)
# 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]
chrome_server2client(clientId, 'DONE')
time.sleep(0.5)
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, textToSpeak)
else:
print 'Found no faces'
simple_message(clientId, u'Désolé, aucun visage trouvé')
time.sleep(0.5)
def convert_datetime(str_datetime): # Format "mm/dd/yyyy hh:mm:ss AM"
mm, dd = str_datetime.split('/')[0:2]
yyyy, tt, ampm = str_datetime.split('/')[2].split(' ')
h, m, s = tt.split(':')
if ((ampm=='pm') and (h!=12)):
h=h+12
return int(yyyy), int(mm), int(dd), int(h), int(m), int(s)
"""
==============================================================================
Create PersonGroup, Add images and Train PersonGroup
==============================================================================
"""
def create_group_add_person(groupId, groupName):
# Create PersonGroup
result = face_api.createPersonGroup(groupId, groupName, "")
flag_reuse_person_group = False
# if (result!=''):
if ('error' in result):
result = eval(result)
if (result["error"]["code"] == "PersonGroupExists"):
res_train_status = face_api.getPersonGroupTrainingStatus(groupId)
res_train_status = res_train_status.replace('null','None')
res_train_status_dict = eval(res_train_status)
print res_train_status
if 'error' not in res_train_status_dict:
createdDateTime = res_train_status_dict['createdDateTime']
year, month, day, hour, mi, sec = convert_datetime(createdDateTime)
structTime = time.localtime()
dt_now = datetime(*structTime[:6])
# Compare if the PersonGroup has expired or not (24 hours)
if (dt_now.year==year):
if (dt_now.month==month):
if (dt_now.day==day):
del_person_group = False
elif (dt_now.day-1==day):
if (dt_now.hour<hour):
del_person_group = False
else:
del_person_group = True
else:
del_person_group = True
else:
del_person_group = True
else:
del_person_group = True
if (del_person_group):
print 'PersonGroup exists, deleting...'
res_del = face_api.deletePersonGroup(groupId)
print ('Deleting PersonGroup succeeded' if res_del=='' else 'Deleting PersonGroup failed')
result = face_api.createPersonGroup(groupId, groupName, "")
print ('Re-create PersonGroup succeeded' if res_del=='' else 'Re-create PersonGroup failed')
flag_reuse_person_group = False
elif (not del_person_group):
# Get PersonGroup training status
training_status = res_train_status_dict['status']
if (training_status=='succeeded'):
flag_reuse_person_group = True
elif (result["error"]["code"] == "RateLimitExceeded"):
print 'RateLimitExceeded, please retry after 30 seconds'
sys.exit()
if not flag_reuse_person_group:
# Create person and add person image
image_paths = [os.path.join(imgPath, f) for f in os.listdir(imgPath)]
nbr = 0
for image_path in image_paths:
nom = os.path.split(image_path)[1].split(".")[0]
if nom not in list_nom:
# Create a Person in PersonGroup
personName = nom
personId = face_api.createPerson(groupId, personName, "")
list_nom.append(nom)
list_personId.append(personId)
nbr += 1
else:
personId = list_personId[nbr-1]
# Add image
image_data = get_image_from_github(image_path)
face_api.addPersonFace(groupId, personId, None, None, image_data)
print "Add image...", nom, '\t', image_path
time.sleep(0.25)
def train_person_group(groupId):
# Train PersonGroup
face_api.trainPersonGroup(groupId)
# Get training status
res = face_api.getPersonGroupTrainingStatus(groupId)
res = res.replace('null','None')
res_dict = eval(res)
training_status = res_dict['status']
print training_status
while (training_status=='running'):
time.sleep(0.25)
res = face_api.getPersonGroupTrainingStatus(groupId)
res = res.replace('null','None')
res_dict = eval(res)
training_status = res_dict['status']
print training_status
return training_status
"""
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['respFromHTML']==""):
pass
res = global_var['respFromHTML']
global_var['respFromHTML'] = ""
return res
"""
==============================================================================
Dialogue from Chrome
==============================================================================
"""
def chrome_server2client(clientId, text): # Text-to-Speech
global global_vars
global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()
global_var['sendToHTML'] = text
time.sleep(0.1)
def chrome_client2server(clientId): # Speech-to-Text
global global_vars
global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()
global_var['respFromHTML'] = ''
while (not (global_var['startListening']) and (global_var['respFromHTML'] == '')):
pass
global_var['startListening'] = False # Reset after using
t0 = time.time() # Start counting time
while (global_var['respFromHTML'] == ''):
pass
if (time.time()-t0>=7 and global_var['respFromHTML'] == ''): # Time outs after 7 secs
global_var['respFromHTML'] = '@' # Silence
resp = global_var['respFromHTML']
time.sleep(0.1)
global_var['respFromHTML'] = '' # Rewrite '' after getting result
return resp
def chrome_yes_or_no(clientId, question):
global global_vars
global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()
chrome_server2client(clientId, question) # Ask a question
response = chrome_client2server(clientId) # Wait for an answer
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
"""
==============================================================================
Display Formation Panel for a recognized or username-known user
==============================================================================
"""
def go_to_formation(clientId, xls_filename, name):
resp = ask_go_to_formation(clientId)
if (resp==1):
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 == '.'):
text2 = "Votre information n'est pas disponible !"
text3 = "Veuillez contacter contact@orange.com"
global_var['text2'] = replace_accents2(text2)
global_var['text3'] = replace_accents2(text3)
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, text2 + ' ' + text3)
time.sleep(1)
link='<a href="http://centre-formation-orange.mybluemix.net">ici</a>'
simple_message(clientId, u"SILENT Cliquez " + link + u" pour accéder à la page Formation pour plus d'information")
time.sleep(0.5)
return_to_recog(clientId) # Return to recognition program immediately or 20 seconds before returning
"""
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()
time.sleep(5)
if not global_var['flag_quit']:
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=='GGQN0871'):
firstname = 'thanhhuy'
lastname = 'nguyen'
email_suffix = '@orange.com'
elif (name=='cleblain'):
firstname = 'christian'
lastname = 'leblainvaux'
email_suffix = '@orange.com'
elif (name=='catherie'):
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)
personId = face_api.createPerson(groupId, name, "")
list_personId.append(personId)
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, 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)
delete_image_on_github(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'])
time.sleep(0.5)
chrome_server2client(clientId, 'START')
nb_img = 0
while (nb_img < nb_img_max):
image_path = image_to_paths[nb_img]
put_image_to_github(image_path, global_var['binary_data'])
# with open(image_path, 'wb') as f:
# f.write(global_var['binary_data'])
# f.close()
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)
chrome_server2client(clientId, 'DONE')
# 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, personId, step_time, flag_show_photos, imgPath, name), name = 'thread_retake_validate_photos_'+clientId)
thread_retake_validate_photos.start()
# TODO: new
# time.sleep(1)
# print "Adding faces to person group..."
# image_to_paths = [root_path+imgPath+str(name)+"."+str(j)+suffix for j in range(nb_img_max)]
# for image_path in image_to_paths:
# image_data = get_image_from_github(image_path)
# face_api.addPersonFace(groupId, personId, None, None, image_data)
# # Retrain Person Group
# resultTrainPersonGroup = face_api.trainPersonGroup(groupId)
# print "Re-train Person Group: ", resultTrainPersonGroup
# global_var['flag_enable_recog'] = 1 # Re-enable recognition
# global_var['flag_ask'] = 1 # Reset asking
"""
Retaking and validating photos
"""
def retake_validate_photos(clientId, personId, 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['respFromHTML'] == ""):
pass
nb = global_var['respFromHTML']
global_var['respFromHTML'] = ""
if ('-' in nb):
nb2 = ''
for i in range(int(nb[0]), int(nb[2])+1):
nb2 = nb2 + str(i)
nb = nb2
elif (nb=='*' or nb=='all'):
nb=''
for j in range(0, nb_img_max):
nb = nb+str(j+1)
elif any(nb[idx] in a for idx in range(0, len(nb))): # If there is any number in string
nb2 = ''
for j in range(0, len(nb)):
if (nb[j] in a):
nb2 = nb2 + nb[j]
nb = nb2
else:
print 'Fatal error: invalid response'
nb = ''
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, 'Vous souhaitez changer les photos: ' + str_nb + ' ?')
global_var['text'] = 'Re-prenant photos'
global_var['text2'] = 'Veuillez patienter... '
global_var['text3'] = ''
simple_message(clientId, global_var['text'] + ' ' + global_var['text2'])
time.sleep(0.25)
chrome_server2client(clientId, 'START')
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]
#TODO: add a facedetect here to cut the face from image
image_path = image_to_paths[int(nb[j])-1]
# os.remove(image_path) # Remove old image
delete_image_on_github(image_path)
# with open(image_path, 'wb') as f:
# f.write(global_var['binary_data'])
# f.close()
put_image_to_github(image_path, global_var['binary_data'])
print "Enregistrer photo " + image_path + ", nb de photos prises : " + nb[j]
chrome_server2client(clientId, 'DONE')
time.sleep(0.25)
a = yes_or_no(clientId, 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)
print "Adding faces to person group..."
image_to_paths = [root_path+imgPath+str(name)+"."+str(j)+suffix for j in range(nb_img_max)]
for image_path in image_to_paths:
image_data = get_image_from_github(image_path)
face_api.addPersonFace(groupId, personId, None, None, image_data)
# Retrain Person Group
resultTrainPersonGroup = face_api.trainPersonGroup(groupId)
print "Re-train Person Group: ", resultTrainPersonGroup
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):
image_to_paths = [root_path + imgPath + str(name) + "." + str(j) + suffix for j in range(nb_img_max)]
# for img_path in image_to_paths:
# image_data = get_image_from_github(img_path)
# data_read = b2a_base64(image_data)
# fh = open(img_path, "wb")
# fh.write(data_read.decode('base64'))
# fh.close()
# for img_path in image_to_paths:
# print 'display', img_path
# plt.figure()
# img = mpimg.imread(img_path)
# plt.imshow(img)
# plt.show()
# time.sleep(2.5) # wait 5 secs
# for ind in range(nb_img_max):
# plt.close("all")
html = ''
for img_path in image_to_paths:
alt = str(name) + ' - Photos'
link = "https://github.com/nthuy190991/facial_recognition_on_Bluemix/blob/master/" + img_path + "?raw=true"
html = html + ' <img src="'+ link + '" class="w3-border w3-padding-4 w3-padding-tiny" alt="'+ alt +'" style="width:128px;">'
simple_message(clientId, u"SILENT " + html)
time.sleep(0.25)
"""
==============================================================================
Re-identification: when a user is not recognized or not correctly recognized
==============================================================================
"""
def re_identification(clientId, nb_time_max, name0):
simple_message(clientId, 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()
global_var['text'] = ''
global_var['text2'] = ''
global_var['text3'] = ''
tb_old_name = np.chararray(shape=(nb_time_max+1), itemsize=10) # All of the 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
global_var['flag_ask'] = 0
while (nb_time < nb_time_max):
time.sleep(wait_time) # wait until after the re-identification is done
name1 = global_var['nom'] # New result
if np.all(tb_old_name != name1) and global_var['flag_recog']:
print 'Essaie ' + str(nb_time+1) + ': reconnu comme ' + str(name1)
resp = validate_recognition(clientId, str(name1))
print resp
if (resp == 1):
result = 1
name = name1
break
else:
result = 0
nb_time += 1
tb_old_name[nb_time] = name1
elif (not global_var['flag_recog']):
print 'Essaie ' + str(nb_time+1) + ': personne inconnue'
result = 0
nb_time += 1
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)
time.sleep(2)
go_to_formation(clientId, xls_filename, name)
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'Désolé je vous reconnaît pas, veuillez me donner votre identifiant')
name = ask_name(clientId, 0)
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, '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, "Malheureusement, les photos correspondant au nom "+ str(name) +" n'existent pas. Je vous conseille de reprendre vos photos")
time.sleep(1)
global_var['flag_take_photo'] = 1 # Enable photo taking
global_var['flag_reidentify'] = 0
"""
==============================================================================
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
start_time = time.time() # For recognition timer (will reset after each 3 secs)
time_origine = time.time() # For display (unchanged)
"""
Permanent loop
"""
i = 0
j = 0
while True:
data = global_var['binary_data']
"""
Decision part
"""
if not (global_var['flag_quit']):
elapsed_time = time.time() - start_time
if ((elapsed_time > wait_time) and global_var['flag_enable_recog']): # Identify after each 3 seconds
faceDetectResult = face_api.faceDetect(None, None, data)
# print faceDetectResult
if (len(faceDetectResult)>=1):
new_faceId = faceDetectResult[0]['faceId']
resultIdentify = face_api.faceIdentify(groupId, [new_faceId], maxNbOfCandidates)
if (len(resultIdentify[0]['candidates'])>=1): # If the number of times recognized is big enough
global_var['flag_recog'] = 1 # Known Person
global_var['flag_ask'] = 0
recognizedPersonId = resultIdentify[0]['candidates'][0]['personId']
conf = resultIdentify[0]['candidates'][0]['confidence']
recognizedPerson = face_api.getPerson(groupId, recognizedPersonId)
recognizedPerson = recognizedPerson.replace('null','None')
recognizedPerson = eval(recognizedPerson)
global_var['nom'] = recognizedPerson['name']
global_var['text'] = 'Reconnu : ' + global_var['nom'] + ' (confidence={})'.format(conf)
print global_var['text']
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
global_var['nom'] = '@' # '@' is for unknown person
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')
time.sleep(0.25)
else:
global_var['flag_recog'] = -1
global_var['text'] = 'Aucune personne'
global_var['text2'] = ''
global_var['text3'] = ''
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 i==0:
chrome_server2client(clientId, 'START')
i=1
if (global_var['flag_quit']):
break
else:
"""
Start Redirecting after the first 1.5 seconds
"""
if j==0:
chrome_server2client(clientId, 'DONE')
j=1
if (global_var['flag_quit']):
break
if (global_var['flag_recog']==1):
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)
go_to_formation(clientId, xls_filename, global_var['nom'])
global_var['key'] = 0
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']==1 and global_var['flag_wrong_recog']==1) or (global_var['flag_recog']==0)): # Not recognized or not correctly recognized
if (global_var['flag_ask']==1):# and (not flag_quit)):
resp_deja_photos = deja_photos(clientId) # Ask user if he has already had a database of face photos
print 'resp_deja_photos = ', resp_deja_photos
# if (resp_deja_photos==-1):
# global_var['flag_ask'] = 0
if (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 = 2 # 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
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)
go_to_formation(clientId, xls_filename, name)
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
if (global_var['flag_take_photo']==1):# and (not flag_quit)):
step_time = 1 # Interval of time (in second) between two times of taking photo
thread_take_photo = Thread(target = take_photos, args = (clientId, step_time, 1), name = 'thread_take_photo_'+clientId)
thread_take_photo.start()