Skip to content

Commit c3ecbfe

Browse files
committed
添加错误提示,添加人脸配准接口,添加传入参数列表判断
1 parent b009f5c commit c3ecbfe

File tree

1 file changed

+102
-60
lines changed

1 file changed

+102
-60
lines changed

TencentYoutuyun/youtu.py

Lines changed: 102 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def __init__(self, appid, secret_id, secret_key):
2020
self.IMAGES_EMPTY = -7
2121
self.FACE_IDS_EMPTY = -8
2222
self.FACE_ID_ETPTY = -9
23+
self.LIST_TYPE_INVALID = -10
2324

2425
self.EXPIRED_SECONDS = 2592000
2526
self._secret_id = secret_id
@@ -55,12 +56,12 @@ def FaceCompare(self, imageA, imageB, userid = '0'):
5556
r = {}
5657
try:
5758
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}
5861
ret = r.json()
62+
5963
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}
6465

6566
return ret
6667

@@ -90,22 +91,22 @@ def FaceVerify(self, person_id, image, userid = '0'):
9091
r = {}
9192
try:
9293
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":''}
9396
ret = r.json()
97+
9498
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":''}
99100

100101
return ret
101102

102103
def FaceIdentify(self, group_id, image, userid = '0'):
103104
filepath = os.path.abspath(image)
104105
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":[{}]}
106107

107108
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":[{}]}
109110

110111
expired = int(time.time()) + self.EXPIRED_SECONDS
111112
url = self.generate_res_url('faceidentify', userid)
@@ -126,12 +127,12 @@ def FaceIdentify(self, group_id, image, userid = '0'):
126127
r = {}
127128
try:
128129
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":[{}]}
129132
ret = r.json()
133+
130134
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":[{}]}
135136

136137
return ret
137138

@@ -158,12 +159,12 @@ def DetectFace(self, image, userid = '0'):
158159
r = {}
159160
try:
160161
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":[{}]}
161164
ret = r.json()
165+
162166
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":[{}]}
167168

168169
return ret
169170

@@ -178,7 +179,10 @@ def NewPerson(self, person_id, image, group_ids, person_name= '', tag='', userid
178179

179180
if len(group_ids) == 0:
180181
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+
182186
expired = int(time.time()) + self.EXPIRED_SECONDS
183187
url = self.generate_res_url('newperson', userid)
184188

@@ -201,12 +205,12 @@ def NewPerson(self, person_id, image, group_ids, person_name= '', tag='', userid
201205
r = {}
202206
try:
203207
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+
204211
ret = r.json()
205212
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":''}
210214

211215
return ret
212216

@@ -232,12 +236,12 @@ def DelPerson(self, person_id, userid = '0') :
232236
r = {}
233237
try:
234238
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+
235242
ret = r.json()
236243
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":''}
241245

242246
return ret
243247

@@ -248,6 +252,9 @@ def AddFace(self, person_id, images, tag='', userid = '0'):
248252
if len(images) == 0:
249253
return {'httpcode':0, 'errorcode':self.IMAGES_EMPTY, 'errormsg':'IMAGES_EMPTY', "face_ids":[], "session_id":'', "added": 0}
250254

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+
251258
images_content = []
252259
for image in images:
253260
filepath = os.path.abspath(image)
@@ -276,12 +283,12 @@ def AddFace(self, person_id, images, tag='', userid = '0'):
276283
r = {}
277284
try:
278285
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+
279289
ret = r.json()
280290
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}
285292

286293
return ret
287294

@@ -291,7 +298,10 @@ def DelFace(self, person_id, face_ids, userid = '0'):
291298

292299
if len(face_ids) == 0:
293300
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+
295305
expired = int(time.time()) + self.EXPIRED_SECONDS
296306
url = self.generate_res_url('delface', userid)
297307

@@ -311,12 +321,12 @@ def DelFace(self, person_id, face_ids, userid = '0'):
311321
r = {}
312322
try:
313323
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+
314327
ret = r.json()
315328
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}
320330

321331
return ret
322332

@@ -345,12 +355,12 @@ def SetInfo(self, person_id, person_name='', tag='',userid = '0'):
345355
r = {}
346356
try:
347357
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+
348361
ret = r.json()
349362
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 ": ''}
354364

355365
return ret
356366

@@ -376,12 +386,12 @@ def GetInfo(self, person_id, userid = '0'):
376386
r = {}
377387
try:
378388
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":''}
379391
ret = r.json()
392+
380393
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":''}
385395

386396
return ret
387397

@@ -403,12 +413,12 @@ def GetGroupIds(self, userid = '0'):
403413
r = {}
404414
try:
405415
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+
406419
ret = r.json()
407420
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":[]}
412422

413423
return ret
414424

@@ -434,12 +444,12 @@ def GetPersonIds(self, group_id, userid = '0') :
434444
r = {}
435445
try:
436446
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+
437450
ret = r.json()
438451
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":[]}
443453

444454
return ret
445455

@@ -465,12 +475,12 @@ def GetFaceIds(self, person_id, userid = '0'):
465475
r = {}
466476
try:
467477
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+
468481
ret = r.json()
469482
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":[]}
474484

475485
return ret
476486

@@ -496,14 +506,46 @@ def GetFaceInfo(self, face_id, userid = '0'):
496506
r = {}
497507
try:
498508
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+
499512
ret = r.json()
500513
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":[]}
505515

506516
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
507549

508550
def generate_res_url(self, req_type, userid = '0'):
509551
app_info = conf.get_app_info()

0 commit comments

Comments
 (0)