fix idcard
Showing
3 changed files
with
120 additions
and
22 deletions
| ... | @@ -1418,10 +1418,18 @@ ID_COMPARE_LOGIC = { | ... | @@ -1418,10 +1418,18 @@ ID_COMPARE_LOGIC = { |
| 1418 | } | 1418 | } |
| 1419 | 1419 | ||
| 1420 | PP_COMPARE_LOGIC = { | 1420 | PP_COMPARE_LOGIC = { |
| 1421 | 'customerName': ('英文姓名', 'se_name_compare', {'is_passport': True}), | 1421 | 'customerName': ('英文姓名', 'se_name_compare', {'is_passport': True}), # 1 |
| 1422 | 'idNum': ('护照号码', 'se_common_compare', {}), | 1422 | 'idNum': ('护照号码', 'se_common_compare', {}), # 2 |
| 1423 | 'dateOfBirth': ('出生日期', 'se_date_compare', {'input_replace': ''}), | 1423 | 'dateOfBirth': ('出生日期', 'se_date_compare', {'input_replace': ''}), # 2 |
| 1424 | 'idExpiryDate': ('有效期至', 'se_date_compare', {'input_replace': '', 'today': True}), | 1424 | 'idExpiryDate': ('有效期至', 'se_date_compare', {'input_replace': '', 'today': True}), # 2 |
| 1425 | } | ||
| 1426 | |||
| 1427 | PP_SLICE_MAP = { | ||
| 1428 | 'customerChineseName': '机读码1', | ||
| 1429 | 'customerName': '机读码1', | ||
| 1430 | 'idNum': '机读码2', | ||
| 1431 | 'dateOfBirth': '机读码2', | ||
| 1432 | 'idExpiryDate': '机读码2', | ||
| 1425 | } | 1433 | } |
| 1426 | 1434 | ||
| 1427 | EEP_COMPARE_LOGIC = { | 1435 | EEP_COMPARE_LOGIC = { |
| ... | @@ -1582,7 +1590,9 @@ BC_FIELD_ORDER_2 = (('BankName', '发卡行名称'), | ... | @@ -1582,7 +1590,9 @@ BC_FIELD_ORDER_2 = (('BankName', '发卡行名称'), |
| 1582 | IMG_PATH_KEY = 'uniq_img_path_key' | 1590 | IMG_PATH_KEY = 'uniq_img_path_key' |
| 1583 | IMG_PATH_KEY_2 = 'uniq_img_path_key_2' | 1591 | IMG_PATH_KEY_2 = 'uniq_img_path_key_2' |
| 1584 | SECTION_IMG_PATH_KEY = 'uniq_section_img_path_key' | 1592 | SECTION_IMG_PATH_KEY = 'uniq_section_img_path_key' |
| 1593 | SECTION_IMG_PATH_KEY_2 = 'uniq_section_img_path_key_2' | ||
| 1585 | ALL_POSITION_KEY = 'uniq_all_position_key' | 1594 | ALL_POSITION_KEY = 'uniq_all_position_key' |
| 1595 | ALL_POSITION_KEY_2 = 'uniq_all_position_key_2' | ||
| 1586 | POSITION_KEY = 'uniq_position_key' | 1596 | POSITION_KEY = 'uniq_position_key' |
| 1587 | ANGLE_KEY = 'uniq_angle_key' | 1597 | ANGLE_KEY = 'uniq_angle_key' |
| 1588 | FIELD_POSITION_KEY = 'position' | 1598 | FIELD_POSITION_KEY = 'position' | ... | ... |
| ... | @@ -305,18 +305,22 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -305,18 +305,22 @@ class Command(BaseCommand, LoggerMixin): |
| 305 | if not license_data: | 305 | if not license_data: |
| 306 | res_list.append((pno, ino, part_idx, consts.RES_SUCCESS_EMPTY)) | 306 | res_list.append((pno, ino, part_idx, consts.RES_SUCCESS_EMPTY)) |
| 307 | return | 307 | return |
| 308 | pre, suf = os.path.splitext(img_path) | 308 | if isinstance(license_data, dict): |
| 309 | base64_img = license_data.pop('base64_img', '') | 309 | pre, suf = os.path.splitext(img_path) |
| 310 | is_save = True if len(base64_img) > 0 else False | 310 | base64_img = license_data.pop('base64_img', '') |
| 311 | section_img_path = '{0}_{1}{2}'.format(pre, part_idx, suf) if is_save else img_path | 311 | is_save = True if len(base64_img) > 0 else False |
| 312 | if is_save: | 312 | section_img_path = '{0}_{1}{2}'.format(pre, part_idx, suf) if is_save else img_path |
| 313 | try: | 313 | if is_save: |
| 314 | with open(section_img_path, "wb") as fh: | 314 | try: |
| 315 | fh.write(base64.b64decode(base64_img.encode())) | 315 | with open(section_img_path, "wb") as fh: |
| 316 | except Exception as e: | 316 | fh.write(base64.b64decode(base64_img.encode())) |
| 317 | self.online_log.warn( | 317 | except Exception as e: |
| 318 | '{0} [section img save failed] [img_path={1}]' | 318 | self.online_log.warn( |
| 319 | ' [part_idx={2}]'.format(self.log_base, img_path, part_idx)) | 319 | '{0} [section img save failed] [img_path={1}]' |
| 320 | ' [part_idx={2}]'.format(self.log_base, img_path, part_idx)) | ||
| 321 | else: | ||
| 322 | is_save = False | ||
| 323 | section_img_path = img_path | ||
| 320 | 324 | ||
| 321 | # 保单 | 325 | # 保单 |
| 322 | if classify == consts.INSURANCE_CLASSIFY: | 326 | if classify == consts.INSURANCE_CLASSIFY: |
| ... | @@ -517,12 +521,17 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -517,12 +521,17 @@ class Command(BaseCommand, LoggerMixin): |
| 517 | } | 521 | } |
| 518 | } | 522 | } |
| 519 | 523 | ||
| 520 | id_card_dict[consts.ALL_POSITION_KEY] = position_dict | 524 | |
| 521 | id_card_dict[consts.SECTION_IMG_PATH_KEY] = section_img_path | ||
| 522 | if not is_info_side: | 525 | if not is_info_side: |
| 523 | id_card_dict[consts.IMG_PATH_KEY_2] = img_path | 526 | id_card_dict[consts.IMG_PATH_KEY_2] = img_path |
| 527 | id_card_dict[consts.ALL_POSITION_KEY_2] = position_dict | ||
| 528 | id_card_dict[consts.SECTION_IMG_PATH_KEY_2] = section_img_path | ||
| 529 | |||
| 524 | else: | 530 | else: |
| 531 | id_card_dict[consts.ALL_POSITION_KEY] = position_dict | ||
| 525 | id_card_dict[consts.IMG_PATH_KEY] = img_path | 532 | id_card_dict[consts.IMG_PATH_KEY] = img_path |
| 533 | id_card_dict[consts.SECTION_IMG_PATH_KEY] = section_img_path | ||
| 534 | |||
| 526 | if is_ic and is_save: | 535 | if is_ic and is_save: |
| 527 | card_type = -1 | 536 | card_type = -1 |
| 528 | json_data_4 = { | 537 | json_data_4 = { | ... | ... |
| ... | @@ -364,6 +364,7 @@ def ca_compare_license(license_en, ocr_res_dict, field_list): | ... | @@ -364,6 +364,7 @@ def ca_compare_license(license_en, ocr_res_dict, field_list): |
| 364 | ocr_field, compare_logic, special_expiry_date = consts.CA_COMPARE_FIELD[license_en] | 364 | ocr_field, compare_logic, special_expiry_date = consts.CA_COMPARE_FIELD[license_en] |
| 365 | 365 | ||
| 366 | is_find = False | 366 | is_find = False |
| 367 | special_expiry_date_slice = False | ||
| 367 | result_field_list = [] | 368 | result_field_list = [] |
| 368 | section_img_info = dict() | 369 | section_img_info = dict() |
| 369 | field_img_path_dict = dict() | 370 | field_img_path_dict = dict() |
| ... | @@ -423,8 +424,11 @@ def ca_compare_license(license_en, ocr_res_dict, field_list): | ... | @@ -423,8 +424,11 @@ def ca_compare_license(license_en, ocr_res_dict, field_list): |
| 423 | break | 424 | break |
| 424 | 425 | ||
| 425 | if result == consts.RESULT_N: | 426 | if result == consts.RESULT_N: |
| 426 | next_img_path = expiry_dates_img_path.pop() if len(expiry_dates_img_path) > 0 else empty_str | 427 | if consts.IMG_PATH_KEY_2 in ocr_res_list[res_idx]: |
| 427 | img_path = ocr_res_list[res_idx].get(consts.IMG_PATH_KEY_2, next_img_path) | 428 | img_path = ocr_res_list[res_idx].get(consts.IMG_PATH_KEY_2, '') |
| 429 | special_expiry_date_slice = True | ||
| 430 | else: | ||
| 431 | img_path = expiry_dates_img_path.pop() if len(expiry_dates_img_path) > 0 else empty_str | ||
| 428 | else: | 432 | else: |
| 429 | img_path = empty_str | 433 | img_path = empty_str |
| 430 | else: | 434 | else: |
| ... | @@ -437,12 +441,40 @@ def ca_compare_license(license_en, ocr_res_dict, field_list): | ... | @@ -437,12 +441,40 @@ def ca_compare_license(license_en, ocr_res_dict, field_list): |
| 437 | result_field_list.append((name, value, consts.RESULT_N, empty_str, empty_str, ErrorType.NF.value)) | 441 | result_field_list.append((name, value, consts.RESULT_N, empty_str, empty_str, ErrorType.NF.value)) |
| 438 | 442 | ||
| 439 | if is_find: | 443 | if is_find: |
| 444 | if special_expiry_date_slice: | ||
| 445 | special_section_img_path = section_img_info.get(consts.SECTION_IMG_PATH_KEY_2, '') | ||
| 446 | if os.path.exists(special_section_img_path): | ||
| 447 | field = 'idExpiryDate' | ||
| 448 | special_info = section_img_info.get(consts.ALL_POSITION_KEY_2, {}) | ||
| 449 | special_section_position = special_info.get(consts.POSITION_KEY, {}) | ||
| 450 | special_section_angle = special_info.get(consts.ANGLE_KEY, 0) | ||
| 451 | try: | ||
| 452 | last_img = img_process(special_section_img_path, special_section_position, special_section_angle) | ||
| 453 | except Exception as e: | ||
| 454 | field_img_path_dict[field] = special_section_img_path | ||
| 455 | else: | ||
| 456 | pre, suf = os.path.splitext(special_section_img_path) | ||
| 457 | try: | ||
| 458 | res_field = compare_logic[field][0] | ||
| 459 | is_valid, coord_tuple = field_build_coordinates(special_info.get(res_field, {})) | ||
| 460 | if is_valid: | ||
| 461 | save_path = '{0}_{1}{2}'.format(pre, field, suf) | ||
| 462 | field_img = last_img[coord_tuple[0]:coord_tuple[1], coord_tuple[2]:coord_tuple[3], :] | ||
| 463 | cv2.imwrite(save_path, field_img) | ||
| 464 | field_img_path_dict[field] = save_path | ||
| 465 | else: | ||
| 466 | field_img_path_dict[field] = special_section_img_path | ||
| 467 | except Exception as e: | ||
| 468 | field_img_path_dict[field] = special_section_img_path | ||
| 469 | |||
| 440 | section_img_path = section_img_info.get(consts.SECTION_IMG_PATH_KEY, '') | 470 | section_img_path = section_img_info.get(consts.SECTION_IMG_PATH_KEY, '') |
| 441 | if os.path.exists(section_img_path): | 471 | if os.path.exists(section_img_path): |
| 442 | failed_field = [] | 472 | failed_field = [] |
| 443 | base_img_path = empty_str | 473 | base_img_path = empty_str |
| 444 | for name, _, result, _, img_path, _ in result_field_list: | 474 | for name, _, result, _, img_path, _ in result_field_list: |
| 445 | if result == consts.RESULT_N: | 475 | if result == consts.RESULT_N: |
| 476 | if special_expiry_date_slice and name == 'idExpiryDate': | ||
| 477 | continue | ||
| 446 | failed_field.append(name) | 478 | failed_field.append(name) |
| 447 | if base_img_path == empty_str: | 479 | if base_img_path == empty_str: |
| 448 | base_img_path = img_path | 480 | base_img_path = img_path |
| ... | @@ -459,7 +491,10 @@ def ca_compare_license(license_en, ocr_res_dict, field_list): | ... | @@ -459,7 +491,10 @@ def ca_compare_license(license_en, ocr_res_dict, field_list): |
| 459 | pre, suf = os.path.splitext(section_img_path) | 491 | pre, suf = os.path.splitext(section_img_path) |
| 460 | for field in failed_field: | 492 | for field in failed_field: |
| 461 | try: | 493 | try: |
| 462 | res_field = compare_logic[field][0] | 494 | if license_en == consts.PP_EN: |
| 495 | res_field = consts.PP_SLICE_MAP[field] | ||
| 496 | else: | ||
| 497 | res_field = compare_logic[field][0] | ||
| 463 | is_valid, coord_tuple = field_build_coordinates(info.get(res_field, {})) | 498 | is_valid, coord_tuple = field_build_coordinates(info.get(res_field, {})) |
| 464 | if is_valid: | 499 | if is_valid: |
| 465 | save_path = '{0}_{1}{2}'.format(pre, field, suf) | 500 | save_path = '{0}_{1}{2}'.format(pre, field, suf) |
| ... | @@ -1386,6 +1421,7 @@ def se_compare_license(license_en, ocr_res_dict, field_list): | ... | @@ -1386,6 +1421,7 @@ def se_compare_license(license_en, ocr_res_dict, field_list): |
| 1386 | 1421 | ||
| 1387 | is_find = False | 1422 | is_find = False |
| 1388 | no_ocr_result = False | 1423 | no_ocr_result = False |
| 1424 | special_expiry_date_slice = False | ||
| 1389 | result_field_list = [] | 1425 | result_field_list = [] |
| 1390 | section_img_info = dict() | 1426 | section_img_info = dict() |
| 1391 | field_img_path_dict = dict() | 1427 | field_img_path_dict = dict() |
| ... | @@ -1397,12 +1433,15 @@ def se_compare_license(license_en, ocr_res_dict, field_list): | ... | @@ -1397,12 +1433,15 @@ def se_compare_license(license_en, ocr_res_dict, field_list): |
| 1397 | # 身份证、居住证 过期期限特殊处理 | 1433 | # 身份证、居住证 过期期限特殊处理 |
| 1398 | if special_expiry_date: | 1434 | if special_expiry_date: |
| 1399 | expiry_dates = set() | 1435 | expiry_dates = set() |
| 1436 | expiry_dates_img_path = set() | ||
| 1400 | key = compare_logic.get('idExpiryDate')[0] | 1437 | key = compare_logic.get('idExpiryDate')[0] |
| 1401 | for ocr_res in ocr_res_list: | 1438 | for ocr_res in ocr_res_list: |
| 1402 | if key in ocr_res: | 1439 | if key in ocr_res: |
| 1403 | expiry_dates.add(ocr_res[key]) | 1440 | expiry_dates.add(ocr_res[key]) |
| 1441 | expiry_dates_img_path.add(ocr_res.get(consts.IMG_PATH_KEY_2, '')) | ||
| 1404 | else: | 1442 | else: |
| 1405 | expiry_dates = set() | 1443 | expiry_dates = set() |
| 1444 | expiry_dates_img_path = set() | ||
| 1406 | 1445 | ||
| 1407 | for res_idx in range(length-1, -1, -1): | 1446 | for res_idx in range(length-1, -1, -1): |
| 1408 | if is_find: | 1447 | if is_find: |
| ... | @@ -1422,6 +1461,9 @@ def se_compare_license(license_en, ocr_res_dict, field_list): | ... | @@ -1422,6 +1461,9 @@ def se_compare_license(license_en, ocr_res_dict, field_list): |
| 1422 | is_find = True | 1461 | is_find = True |
| 1423 | section_img_info[consts.SECTION_IMG_PATH_KEY] = ocr_res_list[res_idx].get(consts.SECTION_IMG_PATH_KEY, '') | 1462 | section_img_info[consts.SECTION_IMG_PATH_KEY] = ocr_res_list[res_idx].get(consts.SECTION_IMG_PATH_KEY, '') |
| 1424 | section_img_info[consts.ALL_POSITION_KEY] = ocr_res_list[res_idx].get(consts.ALL_POSITION_KEY, {}) | 1463 | section_img_info[consts.ALL_POSITION_KEY] = ocr_res_list[res_idx].get(consts.ALL_POSITION_KEY, {}) |
| 1464 | if special_expiry_date: | ||
| 1465 | section_img_info[consts.SECTION_IMG_PATH_KEY_2] = ocr_res_list[res_idx].get( | ||
| 1466 | consts.SECTION_IMG_PATH_KEY_2, '') | ||
| 1425 | 1467 | ||
| 1426 | # 过期期限特殊处理 | 1468 | # 过期期限特殊处理 |
| 1427 | if special_expiry_date and name == 'idExpiryDate' and result == consts.RESULT_N: | 1469 | if special_expiry_date and name == 'idExpiryDate' and result == consts.RESULT_N: |
| ... | @@ -1432,7 +1474,16 @@ def se_compare_license(license_en, ocr_res_dict, field_list): | ... | @@ -1432,7 +1474,16 @@ def se_compare_license(license_en, ocr_res_dict, field_list): |
| 1432 | result = expiry_date_res | 1474 | result = expiry_date_res |
| 1433 | break | 1475 | break |
| 1434 | 1476 | ||
| 1435 | img_path = ocr_res_list[res_idx].get(consts.IMG_PATH_KEY, '') if result == consts.RESULT_N else empty_str | 1477 | if result == consts.RESULT_N: |
| 1478 | if consts.IMG_PATH_KEY_2 in ocr_res_list[res_idx]: | ||
| 1479 | img_path = ocr_res_list[res_idx].get(consts.IMG_PATH_KEY_2, '') | ||
| 1480 | special_expiry_date_slice = True | ||
| 1481 | else: | ||
| 1482 | img_path = expiry_dates_img_path.pop() if len(expiry_dates_img_path) > 0 else empty_str | ||
| 1483 | else: | ||
| 1484 | img_path = empty_str | ||
| 1485 | else: | ||
| 1486 | img_path = ocr_res_list[res_idx].get(consts.IMG_PATH_KEY, '') if result == consts.RESULT_N else empty_str | ||
| 1436 | error_type = empty_error_type if result == consts.RESULT_Y else ErrorType.OCR.value | 1487 | error_type = empty_error_type if result == consts.RESULT_Y else ErrorType.OCR.value |
| 1437 | result_field_list.append((name, value, result, ocr_str, img_path, error_type)) | 1488 | result_field_list.append((name, value, result, ocr_str, img_path, error_type)) |
| 1438 | else: | 1489 | else: |
| ... | @@ -1443,12 +1494,40 @@ def se_compare_license(license_en, ocr_res_dict, field_list): | ... | @@ -1443,12 +1494,40 @@ def se_compare_license(license_en, ocr_res_dict, field_list): |
| 1443 | result_field_list.append((name, value, consts.RESULT_N, empty_str, empty_str, ErrorType.NF.value)) | 1494 | result_field_list.append((name, value, consts.RESULT_N, empty_str, empty_str, ErrorType.NF.value)) |
| 1444 | 1495 | ||
| 1445 | if is_find: | 1496 | if is_find: |
| 1497 | if special_expiry_date_slice: | ||
| 1498 | special_section_img_path = section_img_info.get(consts.SECTION_IMG_PATH_KEY_2, '') | ||
| 1499 | if os.path.exists(special_section_img_path): | ||
| 1500 | field = 'idExpiryDate' | ||
| 1501 | special_info = section_img_info.get(consts.ALL_POSITION_KEY_2, {}) | ||
| 1502 | special_section_position = special_info.get(consts.POSITION_KEY, {}) | ||
| 1503 | special_section_angle = special_info.get(consts.ANGLE_KEY, 0) | ||
| 1504 | try: | ||
| 1505 | last_img = img_process(special_section_img_path, special_section_position, special_section_angle) | ||
| 1506 | except Exception as e: | ||
| 1507 | field_img_path_dict[field] = special_section_img_path | ||
| 1508 | else: | ||
| 1509 | pre, suf = os.path.splitext(special_section_img_path) | ||
| 1510 | try: | ||
| 1511 | res_field = compare_logic[field][0] | ||
| 1512 | is_valid, coord_tuple = field_build_coordinates(special_info.get(res_field, {})) | ||
| 1513 | if is_valid: | ||
| 1514 | save_path = '{0}_{1}{2}'.format(pre, field, suf) | ||
| 1515 | field_img = last_img[coord_tuple[0]:coord_tuple[1], coord_tuple[2]:coord_tuple[3], :] | ||
| 1516 | cv2.imwrite(save_path, field_img) | ||
| 1517 | field_img_path_dict[field] = save_path | ||
| 1518 | else: | ||
| 1519 | field_img_path_dict[field] = special_section_img_path | ||
| 1520 | except Exception as e: | ||
| 1521 | field_img_path_dict[field] = special_section_img_path | ||
| 1522 | |||
| 1446 | section_img_path = section_img_info.get(consts.SECTION_IMG_PATH_KEY, '') | 1523 | section_img_path = section_img_info.get(consts.SECTION_IMG_PATH_KEY, '') |
| 1447 | if os.path.exists(section_img_path): | 1524 | if os.path.exists(section_img_path): |
| 1448 | failed_field = [] | 1525 | failed_field = [] |
| 1449 | base_img_path = empty_str | 1526 | base_img_path = empty_str |
| 1450 | for name, _, result, _, img_path, _ in result_field_list: | 1527 | for name, _, result, _, img_path, _ in result_field_list: |
| 1451 | if result == consts.RESULT_N: | 1528 | if result == consts.RESULT_N: |
| 1529 | if special_expiry_date_slice and name == 'idExpiryDate': | ||
| 1530 | continue | ||
| 1452 | failed_field.append(name) | 1531 | failed_field.append(name) |
| 1453 | if base_img_path == empty_str: | 1532 | if base_img_path == empty_str: |
| 1454 | base_img_path = img_path | 1533 | base_img_path = img_path | ... | ... |
-
Please register or sign in to post a comment