id card distinguish
Showing
1 changed file
with
50 additions
and
3 deletions
... | @@ -14,7 +14,7 @@ from settings import conf | ... | @@ -14,7 +14,7 @@ from settings import conf |
14 | from common.mixins import LoggerMixin | 14 | from common.mixins import LoggerMixin |
15 | from common.tools.pdf_to_img import PDFHandler | 15 | from common.tools.pdf_to_img import PDFHandler |
16 | from apps.doc import consts | 16 | from apps.doc import consts |
17 | from apps.doc.exceptions import OCR1Exception | 17 | from apps.doc.exceptions import OCR1Exception, OCR4Exception |
18 | from apps.doc.ocr.wb import BSWorkbook | 18 | from apps.doc.ocr.wb import BSWorkbook |
19 | 19 | ||
20 | 20 | ||
... | @@ -37,13 +37,59 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -37,13 +37,59 @@ class Command(BaseCommand, LoggerMixin): |
37 | def signal_handler(self, sig, frame): | 37 | def signal_handler(self, sig, frame): |
38 | self.switch = False # 停止处理文件 | 38 | self.switch = False # 停止处理文件 |
39 | 39 | ||
40 | def license1_process(self, ocr_data, license_summary, classify, res_list, pno, ino, part_idx): | 40 | def license1_process(self, ocr_data, license_summary, classify, res_list, pno, ino, part_idx, img_path): |
41 | # 类别:'0'身份证, '1'居住证 | 41 | # 类别:'0'身份证, '1'居住证 |
42 | license_data = ocr_data.get('data', []) | 42 | license_data = ocr_data.get('data', []) |
43 | if not license_data: | 43 | if not license_data: |
44 | res_list.append((pno, ino, part_idx, consts.RES_SUCCESS_EMPTY)) | 44 | res_list.append((pno, ino, part_idx, consts.RES_SUCCESS_EMPTY)) |
45 | return | 45 | return |
46 | res_list.append((pno, ino, part_idx, consts.RES_SUCCESS)) | 46 | res_list.append((pno, ino, part_idx, consts.RES_SUCCESS)) |
47 | if classify == consts.IC_CLASSIFY: | ||
48 | for id_card_dict in license_data: | ||
49 | try: | ||
50 | base64_img = id_card_dict.pop('base64_img') | ||
51 | except Exception as e: | ||
52 | continue | ||
53 | else: | ||
54 | card_type = -1 | ||
55 | json_data_4 = { | ||
56 | 'mode': 1, | ||
57 | 'user_info': { | ||
58 | 'image_content': base64_img, | ||
59 | }, | ||
60 | 'options': { | ||
61 | 'distinguish_type': 1, | ||
62 | 'auto_rotate': True, | ||
63 | }, | ||
64 | } | ||
65 | for times in range(consts.RETRY_TIMES): | ||
66 | try: | ||
67 | start_time = time.time() | ||
68 | ocr_4_response = requests.post(self.ocr_url_4, json=json_data_4) | ||
69 | if ocr_4_response.status_code != 200: | ||
70 | raise OCR4Exception('ocr_4 status code: {0}'.format(ocr_4_response.status_code)) | ||
71 | except Exception as e: | ||
72 | self.cronjob_log.warn( | ||
73 | '{0} [ocr_4 failed] [times={1}] [img_path={2}] [error={3}]'.format( | ||
74 | self.log_base, times, img_path, traceback.format_exc())) | ||
75 | else: | ||
76 | ocr_4_res = ocr_4_response.json() | ||
77 | end_time = time.time() | ||
78 | speed_time = int(end_time - start_time) | ||
79 | |||
80 | if ocr_4_res.get('code') == 0 and ocr_4_res.get('result', {}).get('rtn') == 0: | ||
81 | card_type = ocr_4_res.get('result', {}).get( | ||
82 | 'idcard_distinguish_result', {}).get('result', -1) | ||
83 | |||
84 | self.cronjob_log.info( | ||
85 | '{0} [ocr_4 success] [img_path={1}] [speed_time={2}]'.format( | ||
86 | self.log_base, img_path, speed_time)) | ||
87 | break | ||
88 | else: | ||
89 | self.cronjob_log.warn( | ||
90 | '{0} [ocr_4 failed] [img_path={1}]'.format(self.log_base, img_path)) | ||
91 | |||
92 | id_card_dict[consts.IC_TURE_OR_FALSE] = consts.IC_RES_MAPPING.get(card_type) | ||
47 | license_summary.setdefault(classify, []).extend(license_data) | 93 | license_summary.setdefault(classify, []).extend(license_data) |
48 | 94 | ||
49 | @staticmethod | 95 | @staticmethod |
... | @@ -84,7 +130,8 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -84,7 +130,8 @@ class Command(BaseCommand, LoggerMixin): |
84 | if isinstance(data_list, list): | 130 | if isinstance(data_list, list): |
85 | for part_idx, ocr_data in enumerate(data_list): | 131 | for part_idx, ocr_data in enumerate(data_list): |
86 | part_idx = part_idx + 1 | 132 | part_idx = part_idx + 1 |
87 | self.license1_process(ocr_data, license_summary, classify, res_list, pno, ino, part_idx) | 133 | self.license1_process(ocr_data, license_summary, classify, |
134 | res_list, pno, ino, part_idx, img_path) | ||
88 | else: | 135 | else: |
89 | res_list.append((pno, ino, part_idx, consts.RES_FAILED_3)) | 136 | res_list.append((pno, ino, part_idx, consts.RES_FAILED_3)) |
90 | else: | 137 | else: | ... | ... |
-
Please register or sign in to post a comment