55c99301 by 周伟奇

modify ocr_1 res to list

1 parent 50017fdb
......@@ -494,68 +494,77 @@ class Command(BaseCommand, LoggerMixin):
for img_path, res in ocr_1_res.items():
pno, ino = self.parse_img_path(img_path)
if res.get('code') == 1:
ocr_data = res.get('data', {})
classify = ocr_data.get('classify')
if classify is None:
ocr_data_list = res.get('data', [])
if not isinstance(ocr_data_list, list):
res_list.append((pno, ino, consts.RES_FAILED_1))
self.cronjob_log.info('{0} [ocr_1 res error] [img={1}] [res={2}]'.format(
self.log_base, img_path, res))
continue
elif classify in consts.OTHER_CLASSIFY_SET: # 其他类
res_list.append((pno, ino, consts.RES_SUCCESS_OTHER))
continue
elif classify in consts.LICENSE_CLASSIFY_SET_1: # 证件1
self.license1_process(ocr_data, license_summary, classify, res_list, pno, ino)
elif classify in consts.LICENSE_CLASSIFY_SET_2: # 证件2
pid, _, _, _, _, _ = consts.LICENSE_CLASSIFY_MAPPING.get(classify)
with open(img_path, 'rb') as f:
base64_data = base64.b64encode(f.read())
# 获取解码后的base64值
file_data = base64_data.decode()
json_data_2 = {
"pid": str(pid),
"filedata": file_data
}
for times in range(consts.RETRY_TIMES):
try:
start_time = time.time()
ocr_2_response = requests.post(self.ocr_url_2, data=json_data_2)
if ocr_2_response.status_code != 200:
raise OCR2Exception('ocr_2 status code: {0}'.format(ocr_2_response.status_code))
except Exception as e:
self.cronjob_log.warn(
'{0} [ocr_2 failed] [times={1}] [img_path={2}] [error={3}]'.format(
self.log_base, times, img_path, traceback.format_exc()))
else:
ocr_2_res = json.loads(ocr_2_response.text)
end_time = time.time()
speed_time = int(end_time - start_time)
self.cronjob_log.info(
'{0} [ocr_2 success] [img={1}] [res={2}] [speed_time={3}]'.format(
self.log_base, img_path, ocr_2_res, speed_time))
if classify == consts.BC_CLASSIFY:
name = '有'
json_data_3 = {
"file": file_data,
'card_res': ocr_2_res
}
card_name_response = requests.post(self.ocr_url_3, json_data_3)
if card_name_response.status_code == 200:
card_name_res = card_name_response.json()
if isinstance(card_name_res, dict) and \
card_name_res.get('data', {}).get('is_exists_name') == 0:
name = '无'
ocr_2_res['Name'] = name
self.license2_process(ocr_2_res, license_summary, pid, classify, res_list, pno, ino)
break
else:
res_list.append((pno, ino, consts.RES_FAILED_2))
self.cronjob_log.warn(
'{0} [ocr_2 failed] [img_path={1}]'.format(self.log_base, img_path))
else: # 流水处理
self.bs_process(wb, ocr_data, bs_summary, unknown_summary, classify, res_list, pno, ino)
else:
for part_idx, ocr_data in enumerate(ocr_data_list):
ino_part = '{0}-{1}'.format(ino, part_idx)
classify = ocr_data.get('classify')
if classify is None:
res_list.append((pno, ino_part, consts.RES_FAILED_1))
self.cronjob_log.info('{0} [ocr_1 res error] [img={1}] [res={2}]'.format(
self.log_base, img_path, res))
continue
elif classify in consts.OTHER_CLASSIFY_SET: # 其他类
res_list.append((pno, ino_part, consts.RES_SUCCESS_OTHER))
continue
elif classify in consts.LICENSE_CLASSIFY_SET_1: # 证件1
self.license1_process(ocr_data, license_summary, classify, res_list, pno, ino_part)
elif classify in consts.LICENSE_CLASSIFY_SET_2: # 证件2
pid, _, _, _, _, _ = consts.LICENSE_CLASSIFY_MAPPING.get(classify)
file_data = ocr_data.get('section_img')
if file_data is None:
with open(img_path, 'rb') as f:
base64_data = base64.b64encode(f.read())
# 获取解码后的base64值
file_data = base64_data.decode()
json_data_2 = {
"pid": str(pid),
"filedata": file_data
}
for times in range(consts.RETRY_TIMES):
try:
start_time = time.time()
ocr_2_response = requests.post(self.ocr_url_2, data=json_data_2)
if ocr_2_response.status_code != 200:
raise OCR2Exception('ocr_2 status code: {0}'.format(ocr_2_response.status_code))
except Exception as e:
self.cronjob_log.warn(
'{0} [ocr_2 failed] [times={1}] [img_path={2}] [error={3}]'.format(
self.log_base, times, img_path, traceback.format_exc()))
else:
ocr_2_res = json.loads(ocr_2_response.text)
end_time = time.time()
speed_time = int(end_time - start_time)
self.cronjob_log.info(
'{0} [ocr_2 success] [img={1}] [res={2}] [speed_time={3}]'.format(
self.log_base, img_path, ocr_2_res, speed_time))
if classify == consts.BC_CLASSIFY:
name = '有'
json_data_3 = {
"file": file_data,
'card_res': ocr_2_res
}
card_name_response = requests.post(self.ocr_url_3, json_data_3)
if card_name_response.status_code == 200:
card_name_res = card_name_response.json()
if isinstance(card_name_res, dict) and \
card_name_res.get('data', {}).get('is_exists_name') == 0:
name = '无'
ocr_2_res['Name'] = name
self.license2_process(ocr_2_res, license_summary, pid, classify, res_list, pno, ino_part)
break
else:
res_list.append((pno, ino_part, consts.RES_FAILED_2))
self.cronjob_log.warn(
'{0} [ocr_2 failed] [img_path={1}]'.format(self.log_base, img_path))
else: # 流水处理
self.bs_process(wb, ocr_data, bs_summary, unknown_summary, classify, res_list, pno, ino_part)
else:
res_list.append((pno, ino, consts.RES_FAILED_1))
self.cronjob_log.info('{0} [ocr_1 res error] [img={1}] [res={2}]'.format(
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!