def get_person_data_callback(): data = request.get_json() if not verify_data(data, "id"): resp = { "status_code": status_codes["insufficient_data"], "message": response_messages["insufficient_data"] } return jsonify(resp) data_dir = os.path.join(PERSON_DATA_PATH, data["id"]+".json") if not os.path.exists(data_dir): resp = { "status_code": status_codes["person_not_found"], "message": response_messages["person_not_found"] } return jsonify(resp) d_json = get_person_data(data["id"]) temp = get_person_data_from_database(data["id"]) if temp is not None: d_json["star"] = temp["star"] img_dir = os.path.join(FACE_IMAGE_PATH, d_json["image"]) d_json["image"] = encode_to_base64(img_dir) resp = { "status_code": status_codes["success"], "message": response_messages["success"], "data": d_json } return jsonify(resp)
def download_file_from_service(uniq_file_key): ''' downloads file data from remote service in string base64 coding ''' try: data_raw = obj_saver.download(get_download_file_link(uniq_file_key)) except WrongLinkValueError: return None, 'wrong link to file' except DoesNotExist: return None, 'wrong key' except FieldDoesNotExist: return None, 'needs name "key"' return encode_to_base64(data_raw), None
def get_starred_persons_callback(): persons = get_starred_persons() result = [] for mid, star in persons: json_d = get_person_data(mid) json_d["star"] = star img_dir = os.path.join(FACE_IMAGE_PATH, json_d["image"]) json_d["image"] = encode_to_base64(img_dir) result.append(json_d) resp = { "status_code": status_codes["success"], "message": response_messages["success"], "result": result } return jsonify(resp)
def image_anti_porn(filename, token, proxy_dict=None): ''' process the image tagging :param image_base64_str: :return: ''' image_base64_str = utils.encode_to_base64(filename) _payload = {"image": image_base64_str} _url = "%s/v1.0/moderation/image/anti-p**n" % ais.ENDPOINT _headers = {'Content-Type': 'application/json', 'X-Auth-Token': token} if proxy_dict is None: r = requests.post(_url, json=_payload, headers=_headers) else: r = requests.post(_url, json=_payload, headers=_headers, proxies=proxy_dict) return r.json()
def image_anti_porn(filename, token, proxy_dict=None): ''' process the image tagging :param image_base64_str: :return: ''' image_base64_str = utils.encode_to_base64(filename) _payload = {"image": image_base64_str} _url = "%s/v1.0/moderation/image/anti-p**n" % ais.ENDPOINT _headers = {'Content-Type': 'application/json', 'X-Auth-Token': token} if proxy_dict is None: r = requests.post(_url, json=_payload, headers=_headers) else: r = requests.post(_url, json=_payload, headers=_headers, proxies=proxy_dict) return r.json()
def recature_detect(filename, token, proxy_dict=None): ''' process the recature detect :param image_base64_str: :return: ''' image_base64_str = utils.encode_to_base64(filename) _payload = {"image": image_base64_str, "threshold": 0.99, "scene": ["recapture"] } _url = "%s/v1.0/image/recapture-detect" % ais.ENDPOINT _headers = {'Content-Type': 'application/json', 'X-Auth-Token': token} if proxy_dict is None: r = requests.post(_url, json=_payload, headers=_headers) else: r = requests.post(_url, json=_payload, headers=_headers, proxies=proxy_dict) return r.json()
def tagging(filename, token, proxy_dict=None): ''' process the image tagging :param image_base64_str: :return: ''' image_base64_str = utils.encode_to_base64(filename) _payload = {"image": image_base64_str, "url": "", "threshold": "30", "language": "zh" } _url = "%s/v1.0/image/tagging" % ais.ENDPOINT _headers = {'Content-Type': 'application/json', 'X-Auth-Token': token} if proxy_dict is None: r = requests.post(_url, json=_payload, headers=_headers) else: r = requests.post(_url, json=_payload, headers=_headers, proxies=proxy_dict) return r.json()
def get_face_image_callback(): data = request.get_json() if not verify_data(data, "image_name"): resp = { "status_code": status_codes["insufficient_data"], "message": response_messages["insufficient_data"] } return jsonify(resp) img_name = data["image_name"] img_dir = os.path.join(FACE_IMAGE_PATH, img_name) if not os.path.exists(img_dir): resp = { "status_code": status_codes["image_not_found"], "message": response_messages["image_not_found"] } return jsonify(resp) resp = { "status_code": status_codes["success"], "message": response_messages["success"] } resp["image_data"] = encode_to_base64(img_dir) return jsonify(resp)
def search_person_callback(): data = request.get_json() if not verify_data(data, "name"): resp = { "status_code": status_codes["insufficient_data"], "message": response_messages["insufficient_data"] } return jsonify(resp) persons = search_person_by_name(data["name"], limit=100) result = [] for mid, star in persons: json_d = get_person_data(mid) json_d["star"] = star img_dir = os.path.join(FACE_IMAGE_PATH, json_d["image"]) json_d["image"] = encode_to_base64(img_dir) result.append(json_d) resp = { "status_code": status_codes["success"], "message": response_messages["success"], "result": result } return jsonify(resp)
def recature_detect(filename, token, proxy_dict=None): ''' process the recature detect :param image_base64_str: :return: ''' image_base64_str = utils.encode_to_base64(filename) _payload = { "image": image_base64_str, "threshold": 0.99, "scene": ["recapture"] } _url = "%s/v1.0/image/recapture-detect" % ais.ENDPOINT _headers = {'Content-Type': 'application/json', 'X-Auth-Token': token} if proxy_dict is None: r = requests.post(_url, json=_payload, headers=_headers) else: r = requests.post(_url, json=_payload, headers=_headers, proxies=proxy_dict) return r.json()
# # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests, # there is no this problem. # except HTTPError, e: resp = e.read() status_code = e.code except URLError, e: resp = e.read() status_code = e.code else: status_code = r.code resp = r.read() return resp if __name__ == '__main__': user_name = '******' password = '******' account_name = '******' # the same as user_name in commonly use demo_data_url = 'https://ais-sample-data.obs.myhwclouds.com/bank.jpg' token = get_token(user_name, password, account_name) # call interface use the url result = ocr_bank_card(token, '', demo_data_url) print result # call interface use the data result = ocr_bank_card(token, encode_to_base64('data/bank.jpg'), '', True) print result
# # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests, # there is no this problem. # except HTTPError, e: resp = e.read() status_code = e.code except URLError, e: resp = e.read() status_code = e.code else: status_code = r.code resp = r.read() return resp if __name__ == '__main__': user_name = '******' password = '******' account_name = '******' # the same as user_name in commonly use demo_data_url = 'https://ais-sample-data.obs.myhwclouds.com/tagging-normal.jpg' token = get_token(user_name, password, account_name) # call interface use the url result = image_antiporn(token, '', demo_data_url) print result # call interface use the file result = image_antiporn(token, encode_to_base64('data/tagging-normal.jpg')) print result
# 500 error in the single urlopen function. # # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests, # there is no this problem. # except HTTPError, e: resp = e.read() status_code = e.code except URLError, e: resp = e.read() status_code = e.code else: status_code = r.code resp = r.read() return resp if __name__ == '__main__': user_name = '******' password = '******' account_name = '******' # the same as user_name in commonly use demo_data_url = 'https://ais-sample-data.obs.myhwclouds.com/tagging-normal.jpg' token = get_token(user_name, password, account_name) # call interface use the url result = image_antiporn(token, '', demo_data_url ) print result # call interface use the file result = image_antiporn(token, encode_to_base64('data/tagging-normal.jpg')) print result
# # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests, # there is no this problem. # except HTTPError, e: resp = e.read() status_code = e.code except URLError, e: resp = e.read() status_code = e.code else: status_code = r.code resp = r.read() return resp if __name__ == '__main__': user_name = '******' password = '******' account_name = '******' # the same as user_name in commonly use demo_data_url = 'https://ais-sample-data.obs.myhwclouds.com/qr-code.jpg' token = get_token(user_name, password, account_name) # call interface use the url result = ocr_qr_code(token, '', demo_data_url) print result # call interface use the file result = ocr_qr_code(token, encode_to_base64('data/qr-code.jpg')) print result
# # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests, # there is no this problem. # except HTTPError, e: resp = e.read() status_code = e.code except URLError, e: resp = e.read() status_code = e.code else: status_code = r.code resp = r.read() return resp if __name__ == '__main__': user_name = '******' password = '******' account_name = '******' # the same as user_name in commonly use demo_data_url = 'https://ais-sample-data.obs.myhwclouds.com/bank.jpg' token = get_token(user_name, password, account_name) # call interface use the url result = ocr_bank_card(token, '', demo_data_url) print result # call interface use the data result = ocr_bank_card(token, encode_to_base64('data/bank.jpg'), '', True) print result
# # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests, # there is no this problem. # except HTTPError, e: resp = e.read() status_code = e.code except URLError, e: resp = e.read() status_code = e.code else: status_code = r.code resp = r.read() return resp if __name__ == '__main__': user_name = '******' password = '******' account_name = '******' # the same as user_name in commonly use demo_data_url = 'https://ais-sample-data.obs.myhwclouds.com/asr-sentence.wav' token = get_token(user_name, password, account_name) # call interface use the url result = asr_sentence(token, '', demo_data_url, 'wav', '16k') print result # call interface use the file result = asr_sentence(token, encode_to_base64('data/asr-sentence.wav'), '', 'wav', '16k') print result
# # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests, # there is no this problem. # except HTTPError, e: resp = e.read() status_code = e.code except URLError, e: resp = e.read() status_code = e.code else: status_code = r.code resp = r.read() return resp if __name__ == '__main__': user_name = '******' password = '******' account_name = '******' demo_data_url = 'https://ais-sample-data.obs.myhwclouds.com/barcode.jpg' token = get_token(user_name, password, account_name) # call interface use the url result = ocr_barcode(token, '', demo_data_url) print result # call interface use the data result = ocr_barcode(token, encode_to_base64('data/barcode.jpg')) print result
# # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests, # there is no this problem. # except HTTPError, e: resp = e.read() status_code = e.code except URLError, e: resp = e.read() status_code = e.code else: status_code = r.code resp = r.read() return resp if __name__ == '__main__': user_name = '******' password = '******' account_name = '******' demo_data_url = 'https://ais-sample-data.obs.myhwclouds.com/barcode.jpg' token = get_token(user_name, password, account_name) # call interface use the url result = ocr_barcode(token, '', demo_data_url) print result # call interface use the data result = ocr_barcode(token, encode_to_base64('data/barcode.jpg')) print result
# If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests, # there is no this problem. # except HTTPError, e: resp = e.read() status_code = e.code except URLError, e: resp = e.read() status_code = e.code else: status_code = r.code resp = r.read() return resp if __name__ == '__main__': user_name = '******' password = '******' account_name = '******' # the same as user_name in commonly use demo_data_url = 'https://ais-sample-data.obs.myhwclouds.com/qr-code.jpg' token = get_token(user_name, password, account_name) # call interface use the url result = ocr_qr_code(token, '', demo_data_url) print result # call interface use the file result = ocr_qr_code(token, encode_to_base64('data/qr-code.jpg')) print result