def check_message(self, user_id): """ check if user has new message which is unread yet. Args: user_id: Returns """ message_mapping = [ 'non update', 'user\'s report missing person status has updated', 'location push message has updated', 'both missing person status and location message has updated' ] result = ReturnStruct(message_mapping) # 1. check reporter user message. has_update = self.user_model.check_update(user_id) if has_update: result.code = 1 # 2. check location push message. message_queue = self.message_model.get_user_message_queue(user_id, 1) if message_queue != []: # if has_update = 0, code = 2, if has_update = 1, code = 3 result.code = result.code + 2 # 3. set return. result.data['has_update'] = has_update result.data['message_queue'] = message_queue logging.info("result of check_message: %s" % result.data) return result
def identify_check(self, telephone, password): """check the telephone and password in mysql databases. Args: telephone passowrd Returns: result: UserInfo object or None """ message_mapping = [ 'login success', # 0 "no such user", # 1 "telephone format error", # 2 ] result = ReturnStruct(message_mapping) if re.match(regex_dict['telephone'], telephone): user_info = self.user_model.identify_check(telephone, password) if user_info is None: result.code = 1 else: result.code = 0 result.data['user_id'] = user_info.user_id else: result.code = 2 return result
def register_new_user(self, telephone, password, real_name, nick_name, id_number): """register new user to sql database Args && example: "telephone":"15195861110", "password":"******", "real_name":"chenxionghui", "nick_name":"burningbear", "id_number":"350623199503100053" Returns: ReturnStruct, look message_mapping for detail """ message_mapping = [ "success", # 0 "real_name format error", # 1 "nick_name format error", # 2 "id_number format error", # 3 "insert conflit: " # 4 ] result = ReturnStruct(message_mapping) if not re.match(regex_dict['real_name'], real_name): result.code = 1 if re.match(regex_dict['nick_name'], nick_name): result.code = 2 if re.match(regex_dict['id_number'], id_number): result.code = 3 # type check finished. has_success, message = self.user_model.insert_record_to_sql( telephone, password, real_name, nick_name, id_number) if has_success: result.code = 0 else: result.code = 4 result.message_mapping[4] = message_mapping[4] + message return result
def post(self): message_mapping = [ 'success in oss', 'success in resource server', 'failed , not exist' ] result = ReturnStruct(message_mapping) key = self.get_argument('key') exist = self._picture_model.check_exist(key) if exist: url = self._picture_model.get_url(key) result.data = {'url': url} else: file_dir = self.temp_file_dir + str(key) if os.path.exists(file_dir): result.code = 1 url = 'http://' + self.application.host + ':' + self.application.port + self.application.prefix + '/project/redirect?key=' + str( key) result.data = {'url': url} else: result.code = 2 self.return_to_client(result) self.finish()
def unique_check(self, telephone): """Check if this telephone has exist in database. Args: telephone: register telephone. Retruns: result: 0: success. 1: telephone format error 2: telephone has been registered. """ message_mapping = [ "has not been registered before", # 0 "telephone format error", # 1 "telephone has been registed" # 2 ] result = ReturnStruct(message_mapping) if re.match(regex_dict['telephone'], telephone): if self.user_model.is_telephone_exist(telephone): result.code = 2 else: result.code = 1 return result
def post(self): logging.info("in post upload") """ user_id: pro_id: file: """ message_mapping = ['success', 'failed'] result = ReturnStruct(message_mapping) for field_name, files in self.request.files.items(): for meta in files: filename, content_type = meta['filename'], meta['content_type'] body = meta['body'] logging.info('POST "%s" "%s" %d m', filename, content_type, len(body) / 1024.0 / 1024.0) user_id = self.get_argument('user_id') pro_id = self.get_argument('pro_id') logging.info("get user id %s" % user_id) # file = self.get_argument('file') # name = self.get_argument('filename') # try: # binary_picture = base64.b64decode(body) # del file # except TypeError as e: # raise ArgumentTypeError('upload project picture') logging.info("in uploading resource...") key = self._gen_key(user_id, pro_id, filename) file_dir = self.temp_file_dir + str(key) with open(file_dir, 'wb') as f: f.write(body) f.close() result.code = 0 result.data = {'key': key} self.return_to_client(result) self.finish() up_result = yield self.background_task( self._picture_model.upload_resource, key, body) if up_result: del body logging.info("after upload file key:%s. result code is %s " % (key, result.code)) os.remove(file_dir)
def add_new_person(self, person_id, person_name, picture_list): """Add face tokens to faceset; Args: face_tokens list. Returns: { "faceset_token": "42fb0d5bf81c5ac57c52344dddc3e7c9", "time_used": 479, "face_count": 1, "face_added": 1, "request_id": "1470293555,78637cd1-f773-47c6-8ba4-5af7153e4e00", "outer_id": "uabREDWZvshpHISwVsav", "failure_detail": [] } { u'added': 1, u'errormsg': u'OK', u'face_ids': [u'1989390683606093797', u'1989390686576709605', u'1989390689447710693'], u'session_id': u'', u'errorcode': 0, u'ret_codes': [-1312, 0, -1312] } { u'group_ids': [], u'suc_face': 0, u'errormsg': u'SDK_IMAGE_FACEDETECT_FAILED', u'session_id': u'', u'errorcode': -1101, u'suc_group': 0, u'person_id': u'3' } """ mapping = [ 'insert success', 'has low qualitity picture', 'insert failed', ] if type(person_id) == ObjectId: person_id = str(person_id) to_return = ReturnStruct(mapping) picture = picture_list[0] result = self.youtu.NewPerson(person_id, picture, [self.group_id], person_name=person_name, tag='', data_type=2) logging.info("new person result is %s" % result) # string_token='' # for item in face_tokens: # string_token = string_token+item+',' # string_token = string_token[0:-1] if result['errorcode'] == 0: # first picture detect success. del picture_list[0] if picture_list != []: result2 = self.youtu.AddFace(person_id, picture_list, tag='', data_type=2) logging.info("add face result is %s" % result2) if result2['errorcode'] == 0: if result2['added'] != len(picture_list): # some picture detect failed def find_all_index(arr, item): return [i for i, a in enumerate(arr) if a != item] invalid_index = find_all_index(result2['ret_codes'], 0) def find_all_index_item(arr, arr_index): return [arr[a] for a in arr_index] invalid_msg = find_all_index_item( result2['ret_codes'], invalid_index) to_return.code = 1 to_return.data = { 'invalid_msg': invalid_msg, 'invalid_index': [x + 1 for x in invalid_index] } else: to_return.code = 0 else: # insert failed to_return.code = 2 to_return.data = { 'errorcode': result2['errorcode'], 'errormsg': result2['errormsg'] } else: # only one picture. to_return.code = 0 else: to_return.code = 2 to_return.data = { 'errorcode': result['errorcode'], 'errormsg': result['errormsg'] } if to_return.code != 0: # delete add new person. self.youtu.DelPerson(person_id) return to_return
def detect_img_list(self, binary_picture_list, only): """detect face of a image list through face++, get face_token list as a result. Args: binary_picture_list only: if only, detect the higest quality picture. Returns: ReturnStruct. if code == 1: detect error, return low quality picture number in data['count'] if code == 0: return the faces list in data. example: [ { u'attributes': { u'facequality': 20.67800000000001 }, u'face_token': u'44f2a168abdb7770203ae924f3bfaa6c', u'face_rectangle': { u'width': 180, u'top': 88, u'height': 180, u'left': 99 } }, { u'attributes': { u'facequality': 12.599000000000004 }, u'face_token': u'6eb1852aceb54bb1f69cd66863e0718a', u'face_rectangle': { u'width': 138, u'top': 176, u'height': 138, u'left': 252 } } ] """ message_mapping = [ 'detect all pictures successful', 'has low quality picture' ] to_return = ReturnStruct(message_mapping) detect_result_list = [] count = 0 for binary_picture in binary_picture_list: # detect. detect_result = self.face_model.detect_faces(binary_picture) if detect_result == []: # the quality of pictures is too to detect any faces to_return.code = 1 to_return.data = {'failed_detect_count': count} break else: if only: max_index = 0 max_quality = 0 item_count = 0 # get the highest quality face. for item in detect_result: if item['attributes']['facequality'] > max_quality: max_index = item_count max_quality = item['attributes']['facequality'] item_count += 1 detect_result = detect_result[max_index] # appends. detect_result_list.append(detect_result) else: detect_result_list.extend(detect_result) count += 1 if to_return.code != 1: to_return.data = {'detect_result_list': detect_result_list} # logging.info("[detect result list] detect img list function : %s"%detect_result_list) return to_return
def post(self): """User can upload a picture for a specific person_id. System will compare it and tell user if this is a high confidence face. Besides, if it is a high confidence face, we should store this new message and pugh this message though LBS and pilice man. Args: person_id: picture: coordinate: user_id Returns: """ try: person_id = self.get_argument('person_id') coordinate = eval(self.get_argument('coordinate')) picture = self.get_argument('picture') description = self.get_argument('description') user_id = int(self.get_secure_cookie('user_id')) pic_type = self.get_argument('pic_type') except tornado.web.MissingArgumentError as e: raise MyMissingArgumentError(e.arg_name) try: binary_picture = base64.b64decode(picture) except TypeError as e: raise ArgumentTypeError('picture') message_mapping = [ 'find high confidence person', 'the person maybe not the missing one or you upload a low quality picture' ] result = ReturnStruct(message_mapping) event_happen_date = self.get_even_happen_data() # 1. get person's std picture. personid--> -->face_token # std_face_token = self.person_model.get_person_std_pic(person_id) # # 2. detect picture --> face_token2 # result_detect_struct = yield self.background_task(self.face_model.detect_img_list, [binary_picture], True) # result.merge_info(result_detect_struct) # # 3. compare face_token. # if result_detect_struct.code == 0: # # the result just one element # detect_result = result_detect_struct.data['detect_result_list'] # -----[base64]不需要检测人脸,只要比较person_id和base64;不需要std_face_token;不需要detect_result confidence = yield self.background_task(self.face_model.compare_face, person_id, picture) result.data = confidence logging.info("[compare]the confidence is %s" % confidence) # [change] if confidence['errorcode'] == 0 and confidence[ 'ismatch'] and confidence['level'] >= self.confirm_level: # 4. update info result.code = 0 #[change] 这里也不需要detect_result pic_key_list = yield self.background_task( self.picture_model.store_pictures, [binary_picture], "user" + str(user_id), pic_type) # 4. update track and person information shooter_info = {'user_id': user_id, 'description': description} event_info = { 'coordinate': coordinate, 'confidence': confidence['confidence'], 'pic_key': pic_key_list[0], 'person_id': person_id, 'date': event_happen_date } self.person_model.update_person_status(self.person_model.PERSON, event_info, shooter_info) # 5. send message. message_data = { 'spot': coordinate, 'date': event_happen_date, 'person_id': person_id, 'upload_user_id': user_id, 'confidence': confidence['confidence'], 'pic_key': pic_key_list[0] } self.message_model.send_message_factory(self.message_model.COMPARE, message_data) else: result.code = 1 result.data = confidence self.return_to_client(result) self.finish()