55c99301 by 周伟奇

modify ocr_1 res to list

1 parent 50017fdb
...@@ -494,68 +494,77 @@ class Command(BaseCommand, LoggerMixin): ...@@ -494,68 +494,77 @@ class Command(BaseCommand, LoggerMixin):
494 for img_path, res in ocr_1_res.items(): 494 for img_path, res in ocr_1_res.items():
495 pno, ino = self.parse_img_path(img_path) 495 pno, ino = self.parse_img_path(img_path)
496 if res.get('code') == 1: 496 if res.get('code') == 1:
497 ocr_data = res.get('data', {}) 497 ocr_data_list = res.get('data', [])
498 classify = ocr_data.get('classify') 498 if not isinstance(ocr_data_list, list):
499 if classify is None:
500 res_list.append((pno, ino, consts.RES_FAILED_1)) 499 res_list.append((pno, ino, consts.RES_FAILED_1))
501 self.cronjob_log.info('{0} [ocr_1 res error] [img={1}] [res={2}]'.format( 500 self.cronjob_log.info('{0} [ocr_1 res error] [img={1}] [res={2}]'.format(
502 self.log_base, img_path, res)) 501 self.log_base, img_path, res))
503 continue 502 else:
504 elif classify in consts.OTHER_CLASSIFY_SET: # 其他类 503 for part_idx, ocr_data in enumerate(ocr_data_list):
505 res_list.append((pno, ino, consts.RES_SUCCESS_OTHER)) 504 ino_part = '{0}-{1}'.format(ino, part_idx)
506 continue 505 classify = ocr_data.get('classify')
507 elif classify in consts.LICENSE_CLASSIFY_SET_1: # 证件1 506 if classify is None:
508 self.license1_process(ocr_data, license_summary, classify, res_list, pno, ino) 507 res_list.append((pno, ino_part, consts.RES_FAILED_1))
509 elif classify in consts.LICENSE_CLASSIFY_SET_2: # 证件2 508 self.cronjob_log.info('{0} [ocr_1 res error] [img={1}] [res={2}]'.format(
510 pid, _, _, _, _, _ = consts.LICENSE_CLASSIFY_MAPPING.get(classify) 509 self.log_base, img_path, res))
511 with open(img_path, 'rb') as f: 510 continue
512 base64_data = base64.b64encode(f.read()) 511 elif classify in consts.OTHER_CLASSIFY_SET: # 其他类
513 # 获取解码后的base64值 512 res_list.append((pno, ino_part, consts.RES_SUCCESS_OTHER))
514 file_data = base64_data.decode() 513 continue
515 json_data_2 = { 514 elif classify in consts.LICENSE_CLASSIFY_SET_1: # 证件1
516 "pid": str(pid), 515 self.license1_process(ocr_data, license_summary, classify, res_list, pno, ino_part)
517 "filedata": file_data 516 elif classify in consts.LICENSE_CLASSIFY_SET_2: # 证件2
518 } 517 pid, _, _, _, _, _ = consts.LICENSE_CLASSIFY_MAPPING.get(classify)
519 518 file_data = ocr_data.get('section_img')
520 for times in range(consts.RETRY_TIMES): 519 if file_data is None:
521 try: 520 with open(img_path, 'rb') as f:
522 start_time = time.time() 521 base64_data = base64.b64encode(f.read())
523 ocr_2_response = requests.post(self.ocr_url_2, data=json_data_2) 522 # 获取解码后的base64值
524 if ocr_2_response.status_code != 200: 523 file_data = base64_data.decode()
525 raise OCR2Exception('ocr_2 status code: {0}'.format(ocr_2_response.status_code)) 524 json_data_2 = {
526 except Exception as e: 525 "pid": str(pid),
527 self.cronjob_log.warn( 526 "filedata": file_data
528 '{0} [ocr_2 failed] [times={1}] [img_path={2}] [error={3}]'.format( 527 }
529 self.log_base, times, img_path, traceback.format_exc())) 528
530 else: 529 for times in range(consts.RETRY_TIMES):
531 ocr_2_res = json.loads(ocr_2_response.text) 530 try:
532 end_time = time.time() 531 start_time = time.time()
533 speed_time = int(end_time - start_time) 532 ocr_2_response = requests.post(self.ocr_url_2, data=json_data_2)
534 self.cronjob_log.info( 533 if ocr_2_response.status_code != 200:
535 '{0} [ocr_2 success] [img={1}] [res={2}] [speed_time={3}]'.format( 534 raise OCR2Exception('ocr_2 status code: {0}'.format(ocr_2_response.status_code))
536 self.log_base, img_path, ocr_2_res, speed_time)) 535 except Exception as e:
537 536 self.cronjob_log.warn(
538 if classify == consts.BC_CLASSIFY: 537 '{0} [ocr_2 failed] [times={1}] [img_path={2}] [error={3}]'.format(
539 name = '有' 538 self.log_base, times, img_path, traceback.format_exc()))
540 json_data_3 = { 539 else:
541 "file": file_data, 540 ocr_2_res = json.loads(ocr_2_response.text)
542 'card_res': ocr_2_res 541 end_time = time.time()
543 } 542 speed_time = int(end_time - start_time)
544 card_name_response = requests.post(self.ocr_url_3, json_data_3) 543 self.cronjob_log.info(
545 if card_name_response.status_code == 200: 544 '{0} [ocr_2 success] [img={1}] [res={2}] [speed_time={3}]'.format(
546 card_name_res = card_name_response.json() 545 self.log_base, img_path, ocr_2_res, speed_time))
547 if isinstance(card_name_res, dict) and \ 546
548 card_name_res.get('data', {}).get('is_exists_name') == 0: 547 if classify == consts.BC_CLASSIFY:
549 name = '无' 548 name = '有'
550 ocr_2_res['Name'] = name 549 json_data_3 = {
551 self.license2_process(ocr_2_res, license_summary, pid, classify, res_list, pno, ino) 550 "file": file_data,
552 break 551 'card_res': ocr_2_res
553 else: 552 }
554 res_list.append((pno, ino, consts.RES_FAILED_2)) 553 card_name_response = requests.post(self.ocr_url_3, json_data_3)
555 self.cronjob_log.warn( 554 if card_name_response.status_code == 200:
556 '{0} [ocr_2 failed] [img_path={1}]'.format(self.log_base, img_path)) 555 card_name_res = card_name_response.json()
557 else: # 流水处理 556 if isinstance(card_name_res, dict) and \
558 self.bs_process(wb, ocr_data, bs_summary, unknown_summary, classify, res_list, pno, ino) 557 card_name_res.get('data', {}).get('is_exists_name') == 0:
558 name = '无'
559 ocr_2_res['Name'] = name
560 self.license2_process(ocr_2_res, license_summary, pid, classify, res_list, pno, ino_part)
561 break
562 else:
563 res_list.append((pno, ino_part, consts.RES_FAILED_2))
564 self.cronjob_log.warn(
565 '{0} [ocr_2 failed] [img_path={1}]'.format(self.log_base, img_path))
566 else: # 流水处理
567 self.bs_process(wb, ocr_data, bs_summary, unknown_summary, classify, res_list, pno, ino_part)
559 else: 568 else:
560 res_list.append((pno, ino, consts.RES_FAILED_1)) 569 res_list.append((pno, ino, consts.RES_FAILED_1))
561 self.cronjob_log.info('{0} [ocr_1 res error] [img={1}] [res={2}]'.format( 570 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!