slice part 2
Showing
3 changed files
with
223 additions
and
48 deletions
... | @@ -1542,9 +1542,12 @@ BC_FIELD_ORDER_2 = (('BankName', '发卡行名称'), | ... | @@ -1542,9 +1542,12 @@ BC_FIELD_ORDER_2 = (('BankName', '发卡行名称'), |
1542 | 1542 | ||
1543 | IMG_PATH_KEY = 'uniq_img_path_key' | 1543 | IMG_PATH_KEY = 'uniq_img_path_key' |
1544 | IMG_PATH_KEY_2 = 'uniq_img_path_key_2' | 1544 | IMG_PATH_KEY_2 = 'uniq_img_path_key_2' |
1545 | SECTION_IMG_PATH_KEY = 'uniq_section_img_path_key' | ||
1546 | ALL_POSITION_KEY = 'uniq_all_position_key' | ||
1545 | POSITION_KEY = 'uniq_position_key' | 1547 | POSITION_KEY = 'uniq_position_key' |
1546 | SECTION_KEY = 'uniq_section_key' | ||
1547 | ANGLE_KEY = 'uniq_angle_key' | 1548 | ANGLE_KEY = 'uniq_angle_key' |
1549 | FIELD_POSITION_KEY = 'position' | ||
1550 | FIELD_QUAD_KEY = 'quad' | ||
1548 | 1551 | ||
1549 | INFO_SOURCE = ['POS', 'CMS'] | 1552 | INFO_SOURCE = ['POS', 'CMS'] |
1550 | 1553 | ... | ... |
... | @@ -373,7 +373,7 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -373,7 +373,7 @@ class Command(BaseCommand, LoggerMixin): |
373 | license_summary.setdefault(classify, []).extend(license_data) | 373 | license_summary.setdefault(classify, []).extend(license_data) |
374 | res_list.append((pno, ino, part_idx, consts.RES_SUCCESS)) | 374 | res_list.append((pno, ino, part_idx, consts.RES_SUCCESS)) |
375 | 375 | ||
376 | def license2_process(self, ocr_res_2, license_summary, pid, classify, res_list, pno, ino, part_idx, img_path, do_dda, dda_id_bc_mapping, section_img_path=None): | 376 | def license2_process(self, ocr_res_2, license_summary, pid, classify, res_list, pno, ino, part_idx, img_path, do_dda, dda_id_bc_mapping, file_data): |
377 | if ocr_res_2.get('ErrorCode') in consts.SUCCESS_CODE_SET: | 377 | if ocr_res_2.get('ErrorCode') in consts.SUCCESS_CODE_SET: |
378 | res_list.append((pno, ino, part_idx, consts.RES_SUCCESS)) | 378 | res_list.append((pno, ino, part_idx, consts.RES_SUCCESS)) |
379 | if pid == consts.BC_PID: | 379 | if pid == consts.BC_PID: |
... | @@ -389,19 +389,51 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -389,19 +389,51 @@ class Command(BaseCommand, LoggerMixin): |
389 | dda_id_bc_mapping.setdefault(consts.BC_FIELD, []).append((bc_no, img_path)) | 389 | dda_id_bc_mapping.setdefault(consts.BC_FIELD, []).append((bc_no, img_path)) |
390 | else: | 390 | else: |
391 | # 营业执照等 | 391 | # 营业执照等 |
392 | for result_dict in ocr_res_2.get('ResultList', []): | 392 | pre, suf = os.path.splitext(img_path) |
393 | position = result_dict.get('position', {}) | 393 | src_section_img_path = img_path if file_data is None else '{0}_{1}{2}'.format(pre, part_idx, suf) |
394 | angle = result_dict.get('angle', 0) | 394 | |
395 | is_save = False | ||
396 | for res_idx, result_dict in enumerate(ocr_res_2.get('ResultList', [])): | ||
397 | image_data = result_dict.get('image_data', '') | ||
398 | if len(image_data) > 0: | ||
399 | position = {} | ||
400 | angle = 0 | ||
401 | section_img_path = '{0}_{1}_{2}{3}'.format(pre, part_idx, res_idx, suf) | ||
402 | try: | ||
403 | with open(section_img_path, "wb") as fh: | ||
404 | fh.write(base64.b64decode(image_data.encode())) | ||
405 | except Exception as e: | ||
406 | self.online_log.warn( | ||
407 | '{0} [section img save failed] [img_path={1}]' | ||
408 | ' [part_idx={2}] [res_idx={3}]'.format(self.log_base, img_path, part_idx, res_idx)) | ||
409 | else: | ||
410 | is_save = True | ||
411 | section_img_path = src_section_img_path | ||
412 | position = result_dict.get('position', {}) | ||
413 | angle = result_dict.get('angle', 0) | ||
395 | res_dict = {} | 414 | res_dict = {} |
396 | position_dict = {} | 415 | position_dict = {} |
397 | for field_dict in result_dict.get('FieldList', []): | 416 | for field_dict in result_dict.get('FieldList', []): |
398 | res_dict[field_dict.get('chn_key', '')] = field_dict.get('value', '') | 417 | res_dict[field_dict.get('chn_key', '')] = field_dict.get('value', '') |
399 | position_dict[field_dict.get('chn_key', '')] = field_dict.get('position', []) | 418 | position_dict[field_dict.get('chn_key', '')] = { |
400 | position_dict[consts.SECTION_KEY] = position | 419 | consts.FIELD_POSITION_KEY: field_dict.get('position', {}), |
420 | consts.FIELD_QUAD_KEY: field_dict.get('quad', []), | ||
421 | } | ||
422 | position_dict[consts.POSITION_KEY] = position | ||
401 | position_dict[consts.ANGLE_KEY] = angle | 423 | position_dict[consts.ANGLE_KEY] = angle |
402 | res_dict[consts.IMG_PATH_KEY] = section_img_path if isinstance(section_img_path, str) else img_path | 424 | res_dict[consts.IMG_PATH_KEY] = img_path |
403 | res_dict[consts.POSITION_KEY] = position_dict | 425 | res_dict[consts.SECTION_IMG_PATH_KEY] = section_img_path |
426 | res_dict[consts.ALL_POSITION_KEY] = position_dict | ||
404 | license_summary.setdefault(classify, []).append(res_dict) | 427 | license_summary.setdefault(classify, []).append(res_dict) |
428 | |||
429 | if is_save and file_data is not None: | ||
430 | try: | ||
431 | with open(src_section_img_path, "wb") as fh: | ||
432 | fh.write(base64.b64decode(file_data.encode())) | ||
433 | except Exception as e: | ||
434 | self.online_log.warn( | ||
435 | '{0} [section img save failed] [img_path={1}]' | ||
436 | ' [part_idx={2}]'.format(self.log_base, img_path, part_idx)) | ||
405 | else: | 437 | else: |
406 | res_list.append((pno, ino, part_idx, consts.RES_FAILED_2)) | 438 | res_list.append((pno, ino, part_idx, consts.RES_FAILED_2)) |
407 | 439 | ||
... | @@ -980,22 +1012,9 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -980,22 +1012,9 @@ class Command(BaseCommand, LoggerMixin): |
980 | name = '无' | 1012 | name = '无' |
981 | ocr_2_res['Name'] = name | 1013 | ocr_2_res['Name'] = name |
982 | 1014 | ||
983 | section_img_path = None | ||
984 | try: | ||
985 | if ocr_2_res.get('ErrorCode') in consts.SUCCESS_CODE_SET: | ||
986 | pre, suf = os.path.splitext(img_path) | ||
987 | section_img_path = '{0}_{1}{2}'.format(pre, part_idx, suf) | ||
988 | with open(section_img_path, "wb") as fh: | ||
989 | fh.write(base64.b64decode(file_data.encode())) | ||
990 | except Exception as e: | ||
991 | self.online_log.warn( | ||
992 | '{0} [section img save failed] [img_path={1}]' | ||
993 | ' [part_idx={2}]'.format(self.log_base, img_path, part_idx)) | ||
994 | |||
995 | self.license2_process(ocr_2_res, license_summary, pid, classify, | 1015 | self.license2_process(ocr_2_res, license_summary, pid, classify, |
996 | res_list, pno, ino, part_idx, img_path, | 1016 | res_list, pno, ino, part_idx, img_path, |
997 | do_dda, dda_id_bc_mapping, | 1017 | do_dda, dda_id_bc_mapping, file_data=ocr_data.get('section_img')) |
998 | section_img_path=section_img_path) | ||
999 | break | 1018 | break |
1000 | else: | 1019 | else: |
1001 | res_list.append((pno, ino, part_idx, consts.RES_FAILED_2)) | 1020 | res_list.append((pno, ino, part_idx, consts.RES_FAILED_2)) | ... | ... |
1 | import json | 1 | import json |
2 | import os | ||
3 | import cv2 | ||
2 | import time | 4 | import time |
3 | import logging | 5 | import logging |
4 | import traceback | 6 | import traceback |
7 | import numpy as np | ||
5 | from datetime import datetime, timedelta | 8 | from datetime import datetime, timedelta |
6 | from collections import OrderedDict | 9 | from collections import OrderedDict |
7 | from . import app | 10 | from . import app |
... | @@ -40,6 +43,79 @@ empty_error_type = 1000 | ... | @@ -40,6 +43,79 @@ empty_error_type = 1000 |
40 | des_key = conf.CMS_DES_KEY | 43 | des_key = conf.CMS_DES_KEY |
41 | 44 | ||
42 | 45 | ||
46 | def rotate_bound(image, angle): | ||
47 | # grab the dimensions of the image and then determine the | ||
48 | # center | ||
49 | (h, w) = image.shape[:2] | ||
50 | (cX, cY) = (w // 2, h // 2) | ||
51 | |||
52 | # grab the rotation matrix (applying the negative of the | ||
53 | # angle to rotate clockwise), then grab the sine and cosine | ||
54 | # (i.e., the rotation components of the matrix) | ||
55 | M = cv2.getRotationMatrix2D((cX, cY), angle, 1.0) | ||
56 | cos = np.abs(M[0, 0]) | ||
57 | sin = np.abs(M[0, 1]) | ||
58 | |||
59 | # compute the new bounding dimensions of the image | ||
60 | nW = int((h * sin) + (w * cos)) | ||
61 | nH = int((h * cos) + (w * sin)) | ||
62 | |||
63 | # adjust the rotation matrix to take into account translation | ||
64 | M[0, 2] += (nW / 2) - cX | ||
65 | M[1, 2] += (nH / 2) - cY | ||
66 | |||
67 | # perform the actual rotation and return the image | ||
68 | return cv2.warpAffine(image, M, (nW, nH)) | ||
69 | |||
70 | |||
71 | def build_coordinates(section_position_dict): | ||
72 | if isinstance(section_position_dict, dict): | ||
73 | h_min = section_position_dict.get('top', 0) | ||
74 | w_min = section_position_dict.get('left', 0) | ||
75 | h_max = h_min + section_position_dict.get('height', 0) | ||
76 | w_max = w_min + section_position_dict.get('width', 0) | ||
77 | if h_max > h_min and w_max > w_min: | ||
78 | return True, (h_min, h_max, w_min, w_max) | ||
79 | else: | ||
80 | return False, () | ||
81 | return False, () | ||
82 | |||
83 | |||
84 | def field_build_coordinates(field_position_info): | ||
85 | field_position_dict = field_position_info.get(consts.FIELD_POSITION_KEY, {}) | ||
86 | field_quad_list = field_position_info.get(consts.FIELD_QUAD_KEY, []) | ||
87 | if isinstance(field_quad_list, list) and len(field_quad_list) == 8: | ||
88 | w_list = [field_quad_list[0], field_quad_list[2], field_quad_list[4], field_quad_list[6]] | ||
89 | h_list = [field_quad_list[1], field_quad_list[3], field_quad_list[5], field_quad_list[7]] | ||
90 | h_min = min(h_list) | ||
91 | h_max = max(h_list) | ||
92 | w_min = min(w_list) | ||
93 | w_max = max(w_list) | ||
94 | if h_max > h_min and w_max > w_min: | ||
95 | return True, (h_min, h_max, w_min, w_max) | ||
96 | if isinstance(field_position_dict, dict): | ||
97 | h_min = field_position_dict.get('top', 0) | ||
98 | w_min = field_position_dict.get('left', 0) | ||
99 | h_max = h_min + field_position_dict.get('height', 0) | ||
100 | w_max = w_min + field_position_dict.get('width', 0) | ||
101 | if h_max > h_min and w_max > w_min: | ||
102 | return True, (h_min, h_max, w_min, w_max) | ||
103 | else: | ||
104 | return False, () | ||
105 | return False, () | ||
106 | |||
107 | |||
108 | def img_process(section_img_path, section_position, section_angle): | ||
109 | image = cv2.imread(section_img_path) | ||
110 | is_valid, coord_tuple = build_coordinates(section_position) | ||
111 | if is_valid: | ||
112 | image = image[coord_tuple[0]:coord_tuple[1], coord_tuple[2]:coord_tuple[3], :] | ||
113 | if isinstance(section_angle, int) or isinstance(section_angle, float): | ||
114 | if section_angle != 0: | ||
115 | return rotate_bound(image, section_angle) | ||
116 | return image | ||
117 | |||
118 | |||
43 | class FakePOS: | 119 | class FakePOS: |
44 | 120 | ||
45 | def __init__(self, | 121 | def __init__(self, |
... | @@ -289,6 +365,8 @@ def ca_compare_license(license_en, ocr_res_dict, field_list): | ... | @@ -289,6 +365,8 @@ def ca_compare_license(license_en, ocr_res_dict, field_list): |
289 | 365 | ||
290 | is_find = False | 366 | is_find = False |
291 | result_field_list = [] | 367 | result_field_list = [] |
368 | section_img_info = dict() | ||
369 | field_img_path_dict = dict() | ||
292 | ocr_res_str = ocr_res_dict.get(ocr_field) | 370 | ocr_res_str = ocr_res_dict.get(ocr_field) |
293 | if ocr_res_str is not None: | 371 | if ocr_res_str is not None: |
294 | ocr_res_list = json.loads(ocr_res_str) | 372 | ocr_res_list = json.loads(ocr_res_str) |
... | @@ -323,6 +401,8 @@ def ca_compare_license(license_en, ocr_res_dict, field_list): | ... | @@ -323,6 +401,8 @@ def ca_compare_license(license_en, ocr_res_dict, field_list): |
323 | break | 401 | break |
324 | 402 | ||
325 | is_find = True | 403 | is_find = True |
404 | section_img_info[consts.SECTION_IMG_PATH_KEY] = ocr_res_list[res_idx].get(consts.SECTION_IMG_PATH_KEY, '') | ||
405 | section_img_info[consts.ALL_POSITION_KEY] = ocr_res_list[res_idx].get(consts.ALL_POSITION_KEY, {}) | ||
326 | 406 | ||
327 | # 过期期限特殊处理 | 407 | # 过期期限特殊处理 |
328 | if special_expiry_date and name == 'idExpiryDate' and result == consts.RESULT_N: | 408 | if special_expiry_date and name == 'idExpiryDate' and result == consts.RESULT_N: |
... | @@ -341,13 +421,48 @@ def ca_compare_license(license_en, ocr_res_dict, field_list): | ... | @@ -341,13 +421,48 @@ def ca_compare_license(license_en, ocr_res_dict, field_list): |
341 | else: | 421 | else: |
342 | img_path = ocr_res_list[res_idx].get(consts.IMG_PATH_KEY, '') if result == consts.RESULT_N else empty_str | 422 | img_path = ocr_res_list[res_idx].get(consts.IMG_PATH_KEY, '') if result == consts.RESULT_N else empty_str |
343 | error_type = empty_error_type if result == consts.RESULT_Y else ErrorType.OCR.value | 423 | error_type = empty_error_type if result == consts.RESULT_Y else ErrorType.OCR.value |
344 | result_field_list.append((name, value, result, ocr_str, position_img_path, img_path, error_type)) | 424 | result_field_list.append((name, value, result, ocr_str, img_path, error_type)) |
345 | 425 | ||
346 | if not is_find: | 426 | if not is_find: |
347 | for name, value in field_list: | 427 | for name, value in field_list: |
348 | result_field_list.append((name, value, consts.RESULT_N, empty_str, empty_str, empty_str, ErrorType.NF.value)) | 428 | result_field_list.append((name, value, consts.RESULT_N, empty_str, empty_str, ErrorType.NF.value)) |
349 | 429 | ||
350 | return result_field_list | 430 | if is_find: |
431 | section_img_path = section_img_info.get(consts.SECTION_IMG_PATH_KEY, '') | ||
432 | if os.path.exists(section_img_path): | ||
433 | failed_field = [] | ||
434 | base_img_path = empty_str | ||
435 | for name, _, result, _, img_path, _ in result_field_list: | ||
436 | if result == consts.RESULT_N: | ||
437 | failed_field.append(name) | ||
438 | if base_img_path == empty_str: | ||
439 | base_img_path = img_path | ||
440 | if len(failed_field) > 0: | ||
441 | info = section_img_info.get(consts.ALL_POSITION_KEY, {}) | ||
442 | section_position = info.get(consts.POSITION_KEY, {}) | ||
443 | section_angle = info.get(consts.ANGLE_KEY, 0) | ||
444 | try: | ||
445 | last_img = img_process(section_img_path, section_position, section_angle) | ||
446 | except Exception as e: | ||
447 | for field in failed_field: | ||
448 | field_img_path_dict[field] = base_img_path | ||
449 | else: | ||
450 | pre, suf = os.path.splitext(section_img_path) | ||
451 | for field in failed_field: | ||
452 | try: | ||
453 | res_field = compare_logic[field][0] | ||
454 | is_valid, coord_tuple = field_build_coordinates(info.get(res_field, {})) | ||
455 | if is_valid: | ||
456 | save_path = '{0}_{1}{2}'.format(pre, field, suf) | ||
457 | field_img = last_img[coord_tuple[0]:coord_tuple[1], coord_tuple[2]:coord_tuple[3], :] | ||
458 | cv2.imwrite(save_path, field_img) | ||
459 | field_img_path_dict[field] = save_path | ||
460 | else: | ||
461 | field_img_path_dict[field] = base_img_path | ||
462 | except Exception as e: | ||
463 | field_img_path_dict[field] = base_img_path | ||
464 | |||
465 | return result_field_list, field_img_path_dict | ||
351 | 466 | ||
352 | 467 | ||
353 | def ca_compare_process(compare_info, ocr_res_dict): | 468 | def ca_compare_process(compare_info, ocr_res_dict): |
... | @@ -363,8 +478,8 @@ def ca_compare_process(compare_info, ocr_res_dict): | ... | @@ -363,8 +478,8 @@ def ca_compare_process(compare_info, ocr_res_dict): |
363 | for idx, license_list in info_value.items(): | 478 | for idx, license_list in info_value.items(): |
364 | for license_dict in license_list: | 479 | for license_dict in license_list: |
365 | for license_en, field_list in license_dict.items(): | 480 | for license_en, field_list in license_dict.items(): |
366 | result_field_list = ca_compare_license(license_en, ocr_res_dict, field_list) | 481 | result_field_list, field_img_path_dict = ca_compare_license(license_en, ocr_res_dict, field_list) |
367 | for name, value, result, ocr_str, position_img_path, img_path, error_type in result_field_list: | 482 | for name, value, result, ocr_str, img_path, error_type in result_field_list: |
368 | total_fields += 1 | 483 | total_fields += 1 |
369 | if result == consts.RESULT_N: | 484 | if result == consts.RESULT_N: |
370 | failed_count += 1 | 485 | failed_count += 1 |
... | @@ -377,15 +492,15 @@ def ca_compare_process(compare_info, ocr_res_dict): | ... | @@ -377,15 +492,15 @@ def ca_compare_process(compare_info, ocr_res_dict): |
377 | consts.HEAD_LIST[4]: value, | 492 | consts.HEAD_LIST[4]: value, |
378 | consts.HEAD_LIST[5]: ocr_str, | 493 | consts.HEAD_LIST[5]: ocr_str, |
379 | consts.HEAD_LIST[6]: result, | 494 | consts.HEAD_LIST[6]: result, |
380 | consts.HEAD_LIST[7]: position_img_path, | 495 | consts.HEAD_LIST[7]: field_img_path_dict.get(name, empty_str), |
381 | consts.HEAD_LIST[8]: img_path, | 496 | consts.HEAD_LIST[8]: img_path, |
382 | consts.HEAD_LIST[9]: error_type, | 497 | consts.HEAD_LIST[9]: error_type, |
383 | } | 498 | } |
384 | ) | 499 | ) |
385 | else: | 500 | else: |
386 | for license_en, field_list in info_value.items(): | 501 | for license_en, field_list in info_value.items(): |
387 | result_field_list = ca_compare_license(license_en, ocr_res_dict, field_list) | 502 | result_field_list, field_img_path_dict = ca_compare_license(license_en, ocr_res_dict, field_list) |
388 | for name, value, result, ocr_str, position_img_path, img_path, error_type in result_field_list: | 503 | for name, value, result, ocr_str, img_path, error_type in result_field_list: |
389 | total_fields += 1 | 504 | total_fields += 1 |
390 | if result == consts.RESULT_N: | 505 | if result == consts.RESULT_N: |
391 | failed_count += 1 | 506 | failed_count += 1 |
... | @@ -398,7 +513,7 @@ def ca_compare_process(compare_info, ocr_res_dict): | ... | @@ -398,7 +513,7 @@ def ca_compare_process(compare_info, ocr_res_dict): |
398 | consts.HEAD_LIST[4]: value, | 513 | consts.HEAD_LIST[4]: value, |
399 | consts.HEAD_LIST[5]: ocr_str, | 514 | consts.HEAD_LIST[5]: ocr_str, |
400 | consts.HEAD_LIST[6]: result, | 515 | consts.HEAD_LIST[6]: result, |
401 | consts.HEAD_LIST[7]: position_img_path, | 516 | consts.HEAD_LIST[7]: field_img_path_dict.get(name, empty_str), |
402 | consts.HEAD_LIST[8]: img_path, | 517 | consts.HEAD_LIST[8]: img_path, |
403 | consts.HEAD_LIST[9]: error_type, | 518 | consts.HEAD_LIST[9]: error_type, |
404 | } | 519 | } |
... | @@ -1073,6 +1188,8 @@ def se_compare_license(license_en, ocr_res_dict, field_list): | ... | @@ -1073,6 +1188,8 @@ def se_compare_license(license_en, ocr_res_dict, field_list): |
1073 | is_find = False | 1188 | is_find = False |
1074 | no_ocr_result = False | 1189 | no_ocr_result = False |
1075 | result_field_list = [] | 1190 | result_field_list = [] |
1191 | section_img_info = dict() | ||
1192 | field_img_path_dict = dict() | ||
1076 | ocr_res_str = ocr_res_dict.get(ocr_field) | 1193 | ocr_res_str = ocr_res_dict.get(ocr_field) |
1077 | if ocr_res_str is not None: | 1194 | if ocr_res_str is not None: |
1078 | ocr_res_list = json.loads(ocr_res_str) | 1195 | ocr_res_list = json.loads(ocr_res_str) |
... | @@ -1104,6 +1221,8 @@ def se_compare_license(license_en, ocr_res_dict, field_list): | ... | @@ -1104,6 +1221,8 @@ def se_compare_license(license_en, ocr_res_dict, field_list): |
1104 | break | 1221 | break |
1105 | 1222 | ||
1106 | is_find = True | 1223 | is_find = True |
1224 | section_img_info[consts.SECTION_IMG_PATH_KEY] = ocr_res_list[res_idx].get(consts.SECTION_IMG_PATH_KEY, '') | ||
1225 | section_img_info[consts.ALL_POSITION_KEY] = ocr_res_list[res_idx].get(consts.ALL_POSITION_KEY, {}) | ||
1107 | 1226 | ||
1108 | # 过期期限特殊处理 | 1227 | # 过期期限特殊处理 |
1109 | if special_expiry_date and name == 'idExpiryDate' and result == consts.RESULT_N: | 1228 | if special_expiry_date and name == 'idExpiryDate' and result == consts.RESULT_N: |
... | @@ -1116,15 +1235,50 @@ def se_compare_license(license_en, ocr_res_dict, field_list): | ... | @@ -1116,15 +1235,50 @@ def se_compare_license(license_en, ocr_res_dict, field_list): |
1116 | 1235 | ||
1117 | img_path = ocr_res_list[res_idx].get(consts.IMG_PATH_KEY, '') if result == consts.RESULT_N else empty_str | 1236 | img_path = ocr_res_list[res_idx].get(consts.IMG_PATH_KEY, '') if result == consts.RESULT_N else empty_str |
1118 | error_type = empty_error_type if result == consts.RESULT_Y else ErrorType.OCR.value | 1237 | error_type = empty_error_type if result == consts.RESULT_Y else ErrorType.OCR.value |
1119 | result_field_list.append((name, value, result, ocr_str, position_img_path, img_path, error_type)) | 1238 | result_field_list.append((name, value, result, ocr_str, img_path, error_type)) |
1120 | else: | 1239 | else: |
1121 | no_ocr_result = True | 1240 | no_ocr_result = True |
1122 | 1241 | ||
1123 | if not is_find: | 1242 | if not is_find: |
1124 | for name, value in field_list: | 1243 | for name, value in field_list: |
1125 | result_field_list.append((name, value, consts.RESULT_N, empty_str, empty_str, empty_str, ErrorType.NF.value)) | 1244 | result_field_list.append((name, value, consts.RESULT_N, empty_str, empty_str, ErrorType.NF.value)) |
1126 | 1245 | ||
1127 | return result_field_list, no_ocr_result | 1246 | if is_find: |
1247 | section_img_path = section_img_info.get(consts.SECTION_IMG_PATH_KEY, '') | ||
1248 | if os.path.exists(section_img_path): | ||
1249 | failed_field = [] | ||
1250 | base_img_path = empty_str | ||
1251 | for name, _, result, _, img_path, _ in result_field_list: | ||
1252 | if result == consts.RESULT_N: | ||
1253 | failed_field.append(name) | ||
1254 | if base_img_path == empty_str: | ||
1255 | base_img_path = img_path | ||
1256 | if len(failed_field) > 0: | ||
1257 | info = section_img_info.get(consts.ALL_POSITION_KEY, {}) | ||
1258 | section_position = info.get(consts.POSITION_KEY, {}) | ||
1259 | section_angle = info.get(consts.ANGLE_KEY, 0) | ||
1260 | try: | ||
1261 | last_img = img_process(section_img_path, section_position, section_angle) | ||
1262 | except Exception as e: | ||
1263 | for field in failed_field: | ||
1264 | field_img_path_dict[field] = base_img_path | ||
1265 | else: | ||
1266 | pre, suf = os.path.splitext(section_img_path) | ||
1267 | for field in failed_field: | ||
1268 | try: | ||
1269 | res_field = compare_logic[field][0] | ||
1270 | is_valid, coord_tuple = field_build_coordinates(info.get(res_field, {})) | ||
1271 | if is_valid: | ||
1272 | save_path = '{0}_{1}{2}'.format(pre, field, suf) | ||
1273 | field_img = last_img[coord_tuple[0]:coord_tuple[1], coord_tuple[2]:coord_tuple[3], :] | ||
1274 | cv2.imwrite(save_path, field_img) | ||
1275 | field_img_path_dict[field] = save_path | ||
1276 | else: | ||
1277 | field_img_path_dict[field] = base_img_path | ||
1278 | except Exception as e: | ||
1279 | field_img_path_dict[field] = base_img_path | ||
1280 | |||
1281 | return result_field_list, no_ocr_result, field_img_path_dict | ||
1128 | 1282 | ||
1129 | 1283 | ||
1130 | def se_mvc34_compare(license_en, ocr_res_dict, field_list): | 1284 | def se_mvc34_compare(license_en, ocr_res_dict, field_list): |
... | @@ -1184,14 +1338,13 @@ def se_mvc34_compare(license_en, ocr_res_dict, field_list): | ... | @@ -1184,14 +1338,13 @@ def se_mvc34_compare(license_en, ocr_res_dict, field_list): |
1184 | result = getattr(cp, compare_logic[name][1])(value, ocr_str, **compare_logic[name][2]) | 1338 | result = getattr(cp, compare_logic[name][1])(value, ocr_str, **compare_logic[name][2]) |
1185 | img_path = ocr_res.get(consts.IMG_PATH_KEY, '') if result == consts.RESULT_N else empty_str | 1339 | img_path = ocr_res.get(consts.IMG_PATH_KEY, '') if result == consts.RESULT_N else empty_str |
1186 | error_type = empty_error_type if result == consts.RESULT_Y else ErrorType.OCR.value | 1340 | error_type = empty_error_type if result == consts.RESULT_Y else ErrorType.OCR.value |
1187 | position_img_path = empty_str | 1341 | result_field_list.append((name, value, result, ocr_str, img_path, error_type)) |
1188 | result_field_list.append((name, value, result, ocr_str, position_img_path, img_path, error_type)) | ||
1189 | 1342 | ||
1190 | if not is_find: | 1343 | if not is_find: |
1191 | for name, value in field_list: | 1344 | for name, value in field_list: |
1192 | result_field_list.append((name, value, consts.RESULT_N, empty_str, empty_str, empty_str, ErrorType.NF.value)) | 1345 | result_field_list.append((name, value, consts.RESULT_N, empty_str, empty_str, ErrorType.NF.value)) |
1193 | 1346 | ||
1194 | return result_field_list | 1347 | return result_field_list, dict() |
1195 | 1348 | ||
1196 | 1349 | ||
1197 | def se_compare_process(compare_info, ocr_res_dict): | 1350 | def se_compare_process(compare_info, ocr_res_dict): |
... | @@ -1211,8 +1364,8 @@ def se_compare_process(compare_info, ocr_res_dict): | ... | @@ -1211,8 +1364,8 @@ def se_compare_process(compare_info, ocr_res_dict): |
1211 | for license_dict in license_list: | 1364 | for license_dict in license_list: |
1212 | for license_en, field_list in license_dict.items(): | 1365 | for license_en, field_list in license_dict.items(): |
1213 | failure_field = [] | 1366 | failure_field = [] |
1214 | result_field_list, no_ocr_result = se_compare_license(license_en, ocr_res_dict, field_list) | 1367 | result_field_list, no_ocr_result, field_img_path_dict = se_compare_license(license_en, ocr_res_dict, field_list) |
1215 | for name, value, result, ocr_str, position_img_path, img_path, error_type in result_field_list: | 1368 | for name, value, result, ocr_str, img_path, error_type in result_field_list: |
1216 | if license_en not in consts.SKIP_CARD or not no_ocr_result: | 1369 | if license_en not in consts.SKIP_CARD or not no_ocr_result: |
1217 | total_fields += 1 | 1370 | total_fields += 1 |
1218 | if result == consts.RESULT_N: | 1371 | if result == consts.RESULT_N: |
... | @@ -1228,7 +1381,7 @@ def se_compare_process(compare_info, ocr_res_dict): | ... | @@ -1228,7 +1381,7 @@ def se_compare_process(compare_info, ocr_res_dict): |
1228 | consts.HEAD_LIST[4]: value, | 1381 | consts.HEAD_LIST[4]: value, |
1229 | consts.HEAD_LIST[5]: ocr_str, | 1382 | consts.HEAD_LIST[5]: ocr_str, |
1230 | consts.HEAD_LIST[6]: result, | 1383 | consts.HEAD_LIST[6]: result, |
1231 | consts.HEAD_LIST[7]: position_img_path, | 1384 | consts.HEAD_LIST[7]: field_img_path_dict.get(name, empty_str), |
1232 | consts.HEAD_LIST[8]: img_path, | 1385 | consts.HEAD_LIST[8]: img_path, |
1233 | consts.HEAD_LIST[9]: error_type, | 1386 | consts.HEAD_LIST[9]: error_type, |
1234 | } | 1387 | } |
... | @@ -1239,10 +1392,10 @@ def se_compare_process(compare_info, ocr_res_dict): | ... | @@ -1239,10 +1392,10 @@ def se_compare_process(compare_info, ocr_res_dict): |
1239 | for license_en, field_list in info_value.items(): | 1392 | for license_en, field_list in info_value.items(): |
1240 | failure_field = [] | 1393 | failure_field = [] |
1241 | if license_en == consts.MVC34_EN: | 1394 | if license_en == consts.MVC34_EN: |
1242 | result_field_list = se_mvc34_compare(license_en, ocr_res_dict, field_list) | 1395 | result_field_list, field_img_path_dict = se_mvc34_compare(license_en, ocr_res_dict, field_list) |
1243 | else: | 1396 | else: |
1244 | result_field_list, _ = se_compare_license(license_en, ocr_res_dict, field_list) | 1397 | result_field_list, _, field_img_path_dict = se_compare_license(license_en, ocr_res_dict, field_list) |
1245 | for name, value, result, ocr_str, position_img_path, img_path, error_type in result_field_list: | 1398 | for name, value, result, ocr_str, img_path, error_type in result_field_list: |
1246 | total_fields += 1 | 1399 | total_fields += 1 |
1247 | if result == consts.RESULT_N: | 1400 | if result == consts.RESULT_N: |
1248 | failed_count += 1 | 1401 | failed_count += 1 |
... | @@ -1257,7 +1410,7 @@ def se_compare_process(compare_info, ocr_res_dict): | ... | @@ -1257,7 +1410,7 @@ def se_compare_process(compare_info, ocr_res_dict): |
1257 | consts.HEAD_LIST[4]: value, | 1410 | consts.HEAD_LIST[4]: value, |
1258 | consts.HEAD_LIST[5]: ocr_str, | 1411 | consts.HEAD_LIST[5]: ocr_str, |
1259 | consts.HEAD_LIST[6]: result, | 1412 | consts.HEAD_LIST[6]: result, |
1260 | consts.HEAD_LIST[7]: position_img_path, | 1413 | consts.HEAD_LIST[7]: field_img_path_dict.get(name, empty_str), |
1261 | consts.HEAD_LIST[8]: img_path, | 1414 | consts.HEAD_LIST[8]: img_path, |
1262 | consts.HEAD_LIST[9]: error_type, | 1415 | consts.HEAD_LIST[9]: error_type, |
1263 | } | 1416 | } | ... | ... |
-
Please register or sign in to post a comment