@@ -20,6 +20,7 @@ def __init__(self, appid, secret_id, secret_key):
20
20
self .IMAGES_EMPTY = - 7
21
21
self .FACE_IDS_EMPTY = - 8
22
22
self .FACE_ID_ETPTY = - 9
23
+ self .LIST_TYPE_INVALID = - 10
23
24
24
25
self .EXPIRED_SECONDS = 2592000
25
26
self ._secret_id = secret_id
@@ -55,12 +56,12 @@ def FaceCompare(self, imageA, imageB, userid = '0'):
55
56
r = {}
56
57
try :
57
58
r = requests .post (url , headers = headers , data = json .dumps (data ))
59
+ if r .status_code != 200 :
60
+ return {'httpcode' :r .status_code , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :'' , 'session_id' :'' , 'eye_sim' :0 , 'mouth_sim' :0 , 'nose_sim' :0 , 'eyebrow_sim' :0 , 'similarity' :0 }
58
61
ret = r .json ()
62
+
59
63
except Exception as e :
60
- if r :
61
- return {'httpcode' :r .status_code , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), 'session_id' :'' , 'eye_sim' :0 , 'mouth_sim' :0 , 'nose_sim' :0 , 'eyebrow_sim' :0 , 'similarity' :0 }
62
- else :
63
- return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), 'session_id' :'' , 'eye_sim' :0 , 'mouth_sim' :0 , 'nose_sim' :0 , 'eyebrow_sim' :0 , 'similarity' :0 }
64
+ return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), 'session_id' :'' , 'eye_sim' :0 , 'mouth_sim' :0 , 'nose_sim' :0 , 'eyebrow_sim' :0 , 'similarity' :0 }
64
65
65
66
return ret
66
67
@@ -90,22 +91,22 @@ def FaceVerify(self, person_id, image, userid = '0'):
90
91
r = {}
91
92
try :
92
93
r = requests .post (url , headers = headers , data = json .dumps (data ))
94
+ if r .status_code != 200 :
95
+ return {'httpcode' :r .status_code , 'errorcode' :'' , 'errormsg' :'' , "confidence" :0 , "ismatch" :0 , "session_id" :'' }
93
96
ret = r .json ()
97
+
94
98
except Exception as e :
95
- if r :
96
- return {'httpcode' :r .status_code , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "confidence" :0 , "ismatch" :0 , "session_id" :'' }
97
- else :
98
- return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "confidence" :0 , "ismatch" :0 , "session_id" :'' }
99
+ return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "confidence" :0 , "ismatch" :0 , "session_id" :'' }
99
100
100
101
return ret
101
102
102
103
def FaceIdentify (self , group_id , image , userid = '0' ):
103
104
filepath = os .path .abspath (image )
104
105
if not os .path .exists (filepath ):
105
- return {'httpcode' :0 , 'errorcode' :self .IMAGE_FILE_NOT_EXISTS , 'errormsg' :'IMAGE_FILE_NOT_EXISTS' , "session_id" :'' , "person_id" : '' , "face_id" : '' , "confidence" : 0 }
106
+ return {'httpcode' :0 , 'errorcode' :self .IMAGE_FILE_NOT_EXISTS , 'errormsg' :'IMAGE_FILE_NOT_EXISTS' , "session_id" :'' , "candidates" :[{}] }
106
107
107
108
if len (group_id ) == 0 :
108
- return {'httpcode' :0 , 'errorcode' :self .GROUP_ID_EMPTY , 'errormsg' :'GROUP_ID_EMPTY' , "session_id" :'' , "person_id" : '' , "face_id" : '' , "confidence" : 0 }
109
+ return {'httpcode' :0 , 'errorcode' :self .GROUP_ID_EMPTY , 'errormsg' :'GROUP_ID_EMPTY' , "session_id" :'' , "candidates" :[{}] }
109
110
110
111
expired = int (time .time ()) + self .EXPIRED_SECONDS
111
112
url = self .generate_res_url ('faceidentify' , userid )
@@ -126,12 +127,12 @@ def FaceIdentify(self, group_id, image, userid = '0'):
126
127
r = {}
127
128
try :
128
129
r = requests .post (url , headers = headers , data = json .dumps (data ))
130
+ if r .status_code != 200 :
131
+ return {'httpcode' :r .status_code , 'errorcode' :'' , 'errormsg' :'' , "session_id" :'' , "candidates" :[{}]}
129
132
ret = r .json ()
133
+
130
134
except Exception as e :
131
- if r :
132
- return {'httpcode' :r .status_code , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "session_id" :'' , "person_id" :'' , "face_id" :'' , "confidence" :0 }
133
- else :
134
- return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "session_id" :'' , "person_id" :'' , "face_id" :'' , "confidence" :0 }
135
+ return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "session_id" :'' , "candidates" :[{}]}
135
136
136
137
return ret
137
138
@@ -158,12 +159,12 @@ def DetectFace(self, image, userid = '0'):
158
159
r = {}
159
160
try :
160
161
r = requests .post (url , headers = headers , data = json .dumps (data ))
162
+ if r .status_code != 200 :
163
+ return {'httpcode' :r .status_code , 'errorcode' :'' , 'errormsg' :'' , "session_id" :'' , "image_id" :'' , "image_height" :0 , "image_width" :0 , "face" :[{}]}
161
164
ret = r .json ()
165
+
162
166
except Exception as e :
163
- if r :
164
- return {'httpcode' :r .status_code , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "session_id" :'' , "image_id" :'' , "image_height" :0 , "image_width" :0 , "face" :[{}]}
165
- else :
166
- return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "session_id" :'' , "image_id" :'' , "image_height" :0 , "image_width" :0 , "face" :[{}]}
167
+ return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "session_id" :'' , "image_id" :'' , "image_height" :0 , "image_width" :0 , "face" :[{}]}
167
168
168
169
return ret
169
170
@@ -178,7 +179,10 @@ def NewPerson(self, person_id, image, group_ids, person_name= '', tag='', userid
178
179
179
180
if len (group_ids ) == 0 :
180
181
return {'httpcode' :0 , 'errorcode' :self .GROUP_IDS_EMPTY , 'errormsg' :'GROUP_IDS_EMPTY' , "person_id" :'' , "suc_group" :'' , "suc_face" :0 , "session_id" :0 , "face_id" :'' , "person_name" :'' }
181
-
182
+
183
+ if type (group_ids ) != list :
184
+ return {'httpcode' :0 , 'errorcode' : self .LIST_TYPE_INVALID , 'errormsg' :'LIST_TYPE_INVALID' , "person_id" :'' , "suc_group" :'' , "suc_face" :0 , "session_id" :0 , "face_id" :'' , "person_name" :'' }
185
+
182
186
expired = int (time .time ()) + self .EXPIRED_SECONDS
183
187
url = self .generate_res_url ('newperson' , userid )
184
188
@@ -201,12 +205,12 @@ def NewPerson(self, person_id, image, group_ids, person_name= '', tag='', userid
201
205
r = {}
202
206
try :
203
207
r = requests .post (url , headers = headers , data = json .dumps (data ))
208
+ if r .status_code != 200 :
209
+ return {'httpcode' :r .status_code , 'errorcode' :'' , 'errormsg' :'' , "person_id" :'' , "suc_group" :'' , "suc_face" :0 , "session_id" :0 , "face_id" :'' , "person_name" :'' }
210
+
204
211
ret = r .json ()
205
212
except Exception as e :
206
- if r :
207
- return {'httpcode' :r .status_code , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "person_id" :'' , "suc_group" :'' , "suc_face" :0 , "session_id" :0 , "face_id" :'' , "person_name" :'' }
208
- else :
209
- return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "person_id" :'' , "suc_group" :'' , "suc_face" :0 , "session_id" :0 , "face_id" :'' , "person_name" :'' }
213
+ return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "person_id" :'' , "suc_group" :'' , "suc_face" :0 , "session_id" :0 , "face_id" :'' , "person_name" :'' }
210
214
211
215
return ret
212
216
@@ -232,12 +236,12 @@ def DelPerson(self, person_id, userid = '0') :
232
236
r = {}
233
237
try :
234
238
r = requests .post (url , headers = headers , data = json .dumps (data ))
239
+ if r .status_code != 200 :
240
+ return {'httpcode' :r .status_code , 'errorcode' :'' , 'errormsg' :'' , "deleted" :0 , "session_id" :'' }
241
+
235
242
ret = r .json ()
236
243
except Exception as e :
237
- if r :
238
- return {'httpcode' :r .status_code , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "deleted" :0 , "session_id" :'' }
239
- else :
240
- return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "deleted" :0 , "session_id" :'' }
244
+ return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "deleted" :0 , "session_id" :'' }
241
245
242
246
return ret
243
247
@@ -248,6 +252,9 @@ def AddFace(self, person_id, images, tag='', userid = '0'):
248
252
if len (images ) == 0 :
249
253
return {'httpcode' :0 , 'errorcode' :self .IMAGES_EMPTY , 'errormsg' :'IMAGES_EMPTY' , "face_ids" :[], "session_id" :'' , "added" : 0 }
250
254
255
+ if type (images ) != list :
256
+ return {'httpcode' :0 , 'errorcode' :self .LIST_TYPE_INVALID , 'errormsg' :'LIST_TYPE_INVALID' , "face_ids" :[], "session_id" :'' , "added" : 0 }
257
+
251
258
images_content = []
252
259
for image in images :
253
260
filepath = os .path .abspath (image )
@@ -276,12 +283,12 @@ def AddFace(self, person_id, images, tag='', userid = '0'):
276
283
r = {}
277
284
try :
278
285
r = requests .post (url , headers = headers , data = json .dumps (data ))
286
+ if r .status_code != 200 :
287
+ return {'httpcode' :r .status_code , 'errorcode' :'' , 'errormsg' :'' , "face_ids" :[], "session_id" :'' , "added" : 0 }
288
+
279
289
ret = r .json ()
280
290
except Exception as e :
281
- if r :
282
- return {'httpcode' :r .status_code , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "face_ids" :[], "session_id" :'' , "added" : 0 }
283
- else :
284
- return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "face_ids" :[], "session_id" :'' , "added" : 0 }
291
+ return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "face_ids" :[], "session_id" :'' , "added" : 0 }
285
292
286
293
return ret
287
294
@@ -291,7 +298,10 @@ def DelFace(self, person_id, face_ids, userid = '0'):
291
298
292
299
if len (face_ids ) == 0 :
293
300
return {'httpcode' :0 , 'errorcode' :self .FACE_IDS_IMPTY , 'errormsg' :'FACE_IDS_IMPTY' , "session_id" :'' , "deleted " : 0 }
294
-
301
+
302
+ if type (face_ids ) != list :
303
+ return {'httpcode' :0 , 'errorcode' :self .LIST_TYPE_INVALID , 'errormsg' :'LIST_TYPE_INVALID' , "session_id" :'' , "deleted " : 0 }
304
+
295
305
expired = int (time .time ()) + self .EXPIRED_SECONDS
296
306
url = self .generate_res_url ('delface' , userid )
297
307
@@ -311,12 +321,12 @@ def DelFace(self, person_id, face_ids, userid = '0'):
311
321
r = {}
312
322
try :
313
323
r = requests .post (url , headers = headers , data = json .dumps (data ))
324
+ if r .status_code != 200 :
325
+ return {'httpcode' :r .status_code , 'errorcode' :'' , 'errormsg' :'' , "session_id" :'' , "deleted " : 0 }
326
+
314
327
ret = r .json ()
315
328
except Exception as e :
316
- if r :
317
- return {'httpcode' :r .status_code , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "session_id" :'' , "deleted " : 0 }
318
- else :
319
- return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "session_id" :'' , "deleted " : 0 }
329
+ return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "session_id" :'' , "deleted " : 0 }
320
330
321
331
return ret
322
332
@@ -345,12 +355,12 @@ def SetInfo(self, person_id, person_name='', tag='',userid = '0'):
345
355
r = {}
346
356
try :
347
357
r = requests .post (url , headers = headers , data = json .dumps (data ))
358
+ if r .status_code != 200 :
359
+ return {'httpcode' :r .status_code , 'errorcode' :'' , 'errormsg' :'' , "person_id" :'' , "session_id " : '' }
360
+
348
361
ret = r .json ()
349
362
except Exception as e :
350
- if r :
351
- return {'httpcode' :r .status_code , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "person_id" :'' , "session_id " : '' }
352
- else :
353
- return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "person_id" :'' , "session_id " : '' }
363
+ return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "person_id" :'' , "session_id " : '' }
354
364
355
365
return ret
356
366
@@ -376,12 +386,12 @@ def GetInfo(self, person_id, userid = '0'):
376
386
r = {}
377
387
try :
378
388
r = requests .post (url , headers = headers , data = json .dumps (data ))
389
+ if r .status_code != 200 :
390
+ return {'httpcode' :r .status_code , 'errorcode' :'' , 'errormsg' :'' , "person_id" :'' , "person_name " : '' , "face_ids" :[], "tag" :'' , "secret_id" :'' }
379
391
ret = r .json ()
392
+
380
393
except Exception as e :
381
- if r :
382
- return {'httpcode' :r .status_code , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "person_id" :'' , "person_name " : '' , "face_ids" :[], "tag" :'' , "secret_id" :'' }
383
- else :
384
- return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "person_id" :'' , "person_name " : '' , "face_ids" :[], "tag" :'' , "secret_id" :'' }
394
+ return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "person_id" :'' , "person_name " : '' , "face_ids" :[], "tag" :'' , "secret_id" :'' }
385
395
386
396
return ret
387
397
@@ -403,12 +413,12 @@ def GetGroupIds(self, userid = '0'):
403
413
r = {}
404
414
try :
405
415
r = requests .post (url , headers = headers , data = json .dumps (data ))
416
+ if r .status_code != 200 :
417
+ return {'httpcode' :r .status_code , 'errorcode' :'' , 'errormsg' :'' , "group_ids" :[]}
418
+
406
419
ret = r .json ()
407
420
except Exception as e :
408
- if r :
409
- return {'httpcode' :r .status_code , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "group_ids" :[]}
410
- else :
411
- return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "group_ids" :[]}
421
+ return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "group_ids" :[]}
412
422
413
423
return ret
414
424
@@ -434,12 +444,12 @@ def GetPersonIds(self, group_id, userid = '0') :
434
444
r = {}
435
445
try :
436
446
r = requests .post (url , headers = headers , data = json .dumps (data ))
447
+ if r .status_code != 200 :
448
+ return {'httpcode' :r .status_code , 'errorcode' :'' , 'errormsg' :'' , "person_ids" :[]}
449
+
437
450
ret = r .json ()
438
451
except Exception as e :
439
- if r :
440
- return {'httpcode' :r .status_code , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "person_ids" :[]}
441
- else :
442
- return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "person_ids" :[]}
452
+ return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "person_ids" :[]}
443
453
444
454
return ret
445
455
@@ -465,12 +475,12 @@ def GetFaceIds(self, person_id, userid = '0'):
465
475
r = {}
466
476
try :
467
477
r = requests .post (url , headers = headers , data = json .dumps (data ))
478
+ if r .status_code != 200 :
479
+ return {'httpcode' :r .status_code , 'errorcode' :'' , 'errormsg' :'' , "face_ids" :[]}
480
+
468
481
ret = r .json ()
469
482
except Exception as e :
470
- if r :
471
- return {'httpcode' :r .status_code , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "face_ids" :[]}
472
- else :
473
- return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "face_ids" :[]}
483
+ return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "face_ids" :[]}
474
484
475
485
return ret
476
486
@@ -496,14 +506,46 @@ def GetFaceInfo(self, face_id, userid = '0'):
496
506
r = {}
497
507
try :
498
508
r = requests .post (url , headers = headers , data = json .dumps (data ))
509
+ if r .status_code != 200 :
510
+ return {'httpcode' :r .status_code , 'errorcode' :'' , 'errormsg' :'' , "face_info" :[]}
511
+
499
512
ret = r .json ()
500
513
except Exception as e :
501
- if r :
502
- return {'httpcode' :r .status_code , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "face_info" :[]}
503
- else :
504
- return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "face_info" :[]}
514
+ return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "face_info" :[]}
505
515
506
516
return ret
517
+
518
+ def FaceShape (self , image , userid = '0' ):
519
+ filepath = os .path .abspath (image )
520
+ if not os .path .exists (filepath ):
521
+ return {'httpcode' :0 , 'errorcode' :self .IMAGE_FILE_NOT_EXISTS , 'errormsg' :'IMAGE_FILE_NOT_EXISTS' , "face_shape" :[{}], "image_height" :0 , "image_width" :0 , "session_id" :'' }
522
+
523
+ expired = int (time .time ()) + self .EXPIRED_SECONDS
524
+ url = self .generate_res_url ('faceshape' , userid )
525
+
526
+ auth = Auth (self ._secret_id , self ._secret_key , self ._appid , userid )
527
+ sign = auth .app_sign (expired )
528
+
529
+ headers = {
530
+ 'Authorization' : sign
531
+ }
532
+
533
+ data = {
534
+ "app_id" : self ._appid ,
535
+ "image" : base64 .b64encode (open (filepath , 'rb' ).read ()).rstrip ()
536
+ }
537
+
538
+ r = {}
539
+ try :
540
+ r = requests .post (url , headers = headers , data = json .dumps (data ))
541
+ if r .status_code != 200 :
542
+ return {'httpcode' :r .status_code , 'errorcode' :'' , 'errormsg' :'' , "face_shape" :[{}], "image_height" :0 , "image_width" :0 , "session_id" :'' }
543
+
544
+ ret = r .json ()
545
+ except Exception as e :
546
+ return {'httpcode' :0 , 'errorcode' :self .IMAGE_NETWORK_ERROR , 'errormsg' :str (e ), "face_shape" :[{}], "image_height" :0 , "image_width" :0 , "session_id" :'' }
547
+
548
+ return ret
507
549
508
550
def generate_res_url (self , req_type , userid = '0' ):
509
551
app_info = conf .get_app_info ()
0 commit comments