add idCard special
Showing
1 changed file
with
201 additions
and
8 deletions
| ... | @@ -2333,6 +2333,183 @@ def se_compare_license(license_en, ocr_res_dict, field_list): | ... | @@ -2333,6 +2333,183 @@ def se_compare_license(license_en, ocr_res_dict, field_list): |
| 2333 | return result_field_list, no_ocr_result, field_img_path_dict | 2333 | return result_field_list, no_ocr_result, field_img_path_dict |
| 2334 | 2334 | ||
| 2335 | 2335 | ||
| 2336 | def se_compare_license_id(license_en, id_res_list, field_list): | ||
| 2337 | ocr_field, compare_logic, special_expiry_date = consts.SE_COMPARE_FIELD[license_en] | ||
| 2338 | |||
| 2339 | is_find = False | ||
| 2340 | no_ocr_result = True | ||
| 2341 | special_expiry_date_slice = False | ||
| 2342 | result_field_list = [] | ||
| 2343 | section_img_info = dict() | ||
| 2344 | field_img_path_dict = dict() | ||
| 2345 | |||
| 2346 | # ocr_res_str = ocr_res_dict.get(ocr_field) | ||
| 2347 | for ocr_res_str in id_res_list: | ||
| 2348 | if is_find: | ||
| 2349 | break | ||
| 2350 | |||
| 2351 | if ocr_res_str is not None: | ||
| 2352 | no_ocr_result = False | ||
| 2353 | |||
| 2354 | ocr_res_list = json.loads(ocr_res_str) | ||
| 2355 | |||
| 2356 | # 3/4页去除 | ||
| 2357 | # if ocr_field == consts.MVC_OCR_FIELD: | ||
| 2358 | # tmp_list = [] | ||
| 2359 | # for res in ocr_res_list: | ||
| 2360 | # if compare_logic['vinNo'][0] in res: | ||
| 2361 | # tmp_list.append(res) | ||
| 2362 | # ocr_res_list = tmp_list | ||
| 2363 | |||
| 2364 | length = len(ocr_res_list) | ||
| 2365 | |||
| 2366 | # 身份证、居住证 过期期限特殊处理 | ||
| 2367 | if special_expiry_date: | ||
| 2368 | expiry_dates = dict() | ||
| 2369 | key = compare_logic.get('idExpiryDate')[0] | ||
| 2370 | for date_tmp_idx, ocr_res in enumerate(ocr_res_list): | ||
| 2371 | if key in ocr_res: | ||
| 2372 | expiry_dates[ocr_res[key]] = (ocr_res.get(consts.IMG_PATH_KEY_2, ''), date_tmp_idx) | ||
| 2373 | else: | ||
| 2374 | expiry_dates = dict() | ||
| 2375 | |||
| 2376 | for res_idx in range(length-1, -1, -1): | ||
| 2377 | if is_find: | ||
| 2378 | break | ||
| 2379 | |||
| 2380 | for idx, (name, value) in enumerate(field_list): | ||
| 2381 | # if ocr_field == consts.MVI_OCR_FIELD and name == consts.SE_NEW_ADD_FIELD[9]: | ||
| 2382 | # ocr_str = getattr(cp, consts.ZW_METHOD)( | ||
| 2383 | # ocr_res_list[res_idx].get(consts.LOWER_AMOUNT_FIELD, ''), | ||
| 2384 | # ocr_res_list[res_idx].get(consts.UPPER_AMOUNT_FIELD, ''), | ||
| 2385 | # ) | ||
| 2386 | # else: | ||
| 2387 | ocr_str = ocr_res_list[res_idx].get(compare_logic[name][0]) | ||
| 2388 | if not isinstance(ocr_str, str): | ||
| 2389 | result = consts.RESULT_N | ||
| 2390 | ocr_str = empty_str | ||
| 2391 | no_key = True | ||
| 2392 | else: | ||
| 2393 | result = getattr(cp, compare_logic[name][1])(value, ocr_str, **compare_logic[name][2]) | ||
| 2394 | no_key = False | ||
| 2395 | |||
| 2396 | if idx == 0 and result == consts.RESULT_N and length > 1: | ||
| 2397 | break | ||
| 2398 | |||
| 2399 | is_find = True | ||
| 2400 | section_img_info[consts.SECTION_IMG_PATH_KEY] = ocr_res_list[res_idx].get(consts.SECTION_IMG_PATH_KEY, '') | ||
| 2401 | section_img_info[consts.ALL_POSITION_KEY] = ocr_res_list[res_idx].get(consts.ALL_POSITION_KEY, {}) | ||
| 2402 | if special_expiry_date: | ||
| 2403 | section_img_info[consts.SECTION_IMG_PATH_KEY_2] = ocr_res_list[res_idx].get( | ||
| 2404 | consts.SECTION_IMG_PATH_KEY_2, '') | ||
| 2405 | section_img_info[consts.ALL_POSITION_KEY_2] = ocr_res_list[res_idx].get(consts.ALL_POSITION_KEY_2, {}) | ||
| 2406 | |||
| 2407 | # 过期期限特殊处理 | ||
| 2408 | if special_expiry_date and name == 'idExpiryDate' and result == consts.RESULT_N: | ||
| 2409 | if no_key: | ||
| 2410 | if len(expiry_dates) == 0: | ||
| 2411 | ocr_str = empty_str | ||
| 2412 | result = consts.RESULT_N | ||
| 2413 | img_path = empty_str | ||
| 2414 | else: | ||
| 2415 | for expiry_date, (date_img_path, date_res_idx) in expiry_dates.items(): | ||
| 2416 | expiry_date_res = getattr(cp, compare_logic[name][1])(value, expiry_date, **compare_logic[name][2]) | ||
| 2417 | if expiry_date_res == consts.RESULT_N: | ||
| 2418 | ocr_str = expiry_date | ||
| 2419 | img_path = date_img_path | ||
| 2420 | special_expiry_date_slice = True | ||
| 2421 | section_img_info[consts.SECTION_IMG_PATH_KEY_2] = ocr_res_list[date_res_idx].get( | ||
| 2422 | consts.SECTION_IMG_PATH_KEY_2, '') | ||
| 2423 | section_img_info[consts.ALL_POSITION_KEY_2] = ocr_res_list[date_res_idx].get( | ||
| 2424 | consts.ALL_POSITION_KEY_2, {}) | ||
| 2425 | break | ||
| 2426 | else: | ||
| 2427 | ocr_str = empty_str | ||
| 2428 | result = consts.RESULT_Y | ||
| 2429 | img_path = empty_str | ||
| 2430 | else: | ||
| 2431 | img_path = ocr_res_list[res_idx].get(consts.IMG_PATH_KEY_2, '') | ||
| 2432 | special_expiry_date_slice = True | ||
| 2433 | else: | ||
| 2434 | img_path = ocr_res_list[res_idx].get(consts.IMG_PATH_KEY, '') if result == consts.RESULT_N else empty_str | ||
| 2435 | if isinstance(value, list): | ||
| 2436 | value = json.dumps(value, ensure_ascii=False) | ||
| 2437 | error_type = empty_error_type if result == consts.RESULT_Y else ErrorType.OCR.value | ||
| 2438 | result_field_list.append((name, value, result, ocr_str, img_path, error_type, compare_logic[name][3])) | ||
| 2439 | |||
| 2440 | if not is_find: | ||
| 2441 | for name, value in field_list: | ||
| 2442 | if isinstance(value, list): | ||
| 2443 | value = json.dumps(value, ensure_ascii=False) | ||
| 2444 | no_find_str = consts.DDA_NO_FIND if license_en == consts.DDA_EN else '{0}未找到'.format(license_en) | ||
| 2445 | result_field_list.append((name, value, consts.RESULT_N, empty_str, empty_str, ErrorType.NF.value, no_find_str)) | ||
| 2446 | |||
| 2447 | if is_find: | ||
| 2448 | if special_expiry_date_slice: | ||
| 2449 | special_section_img_path = section_img_info.get(consts.SECTION_IMG_PATH_KEY_2, '') | ||
| 2450 | if os.path.exists(special_section_img_path): | ||
| 2451 | field = 'idExpiryDate' | ||
| 2452 | special_info = section_img_info.get(consts.ALL_POSITION_KEY_2, {}) | ||
| 2453 | special_section_position = special_info.get(consts.POSITION_KEY, {}) | ||
| 2454 | special_section_angle = special_info.get(consts.ANGLE_KEY, 0) | ||
| 2455 | try: | ||
| 2456 | last_img = img_process(special_section_img_path, special_section_position, special_section_angle) | ||
| 2457 | except Exception as e: | ||
| 2458 | field_img_path_dict[field] = special_section_img_path | ||
| 2459 | else: | ||
| 2460 | pre, suf = os.path.splitext(special_section_img_path) | ||
| 2461 | try: | ||
| 2462 | res_field = compare_logic[field][0] | ||
| 2463 | is_valid, coord_tuple = field_build_coordinates(special_info.get(res_field, {})) | ||
| 2464 | if is_valid: | ||
| 2465 | save_path = '{0}_{1}{2}'.format(pre, field, suf) | ||
| 2466 | field_img = last_img[coord_tuple[0]:coord_tuple[1], coord_tuple[2]:coord_tuple[3], :] | ||
| 2467 | cv2.imwrite(save_path, field_img) | ||
| 2468 | field_img_path_dict[field] = save_path | ||
| 2469 | else: | ||
| 2470 | field_img_path_dict[field] = special_section_img_path | ||
| 2471 | except Exception as e: | ||
| 2472 | field_img_path_dict[field] = special_section_img_path | ||
| 2473 | |||
| 2474 | section_img_path = section_img_info.get(consts.SECTION_IMG_PATH_KEY, '') | ||
| 2475 | if os.path.exists(section_img_path): | ||
| 2476 | failed_field = [] | ||
| 2477 | base_img_path = empty_str | ||
| 2478 | for name, _, result, _, img_path, _, _ in result_field_list: | ||
| 2479 | if result == consts.RESULT_N: | ||
| 2480 | if special_expiry_date_slice and name == 'idExpiryDate': | ||
| 2481 | continue | ||
| 2482 | failed_field.append(name) | ||
| 2483 | if base_img_path == empty_str: | ||
| 2484 | base_img_path = img_path | ||
| 2485 | if len(failed_field) > 0: | ||
| 2486 | info = section_img_info.get(consts.ALL_POSITION_KEY, {}) | ||
| 2487 | section_position = info.get(consts.POSITION_KEY, {}) | ||
| 2488 | section_angle = info.get(consts.ANGLE_KEY, 0) | ||
| 2489 | try: | ||
| 2490 | last_img = img_process(section_img_path, section_position, section_angle) | ||
| 2491 | except Exception as e: | ||
| 2492 | for field in failed_field: | ||
| 2493 | field_img_path_dict[field] = base_img_path | ||
| 2494 | else: | ||
| 2495 | pre, suf = os.path.splitext(section_img_path) | ||
| 2496 | for field in failed_field: | ||
| 2497 | try: | ||
| 2498 | res_field = compare_logic[field][0] | ||
| 2499 | is_valid, coord_tuple = field_build_coordinates(info.get(res_field, {})) | ||
| 2500 | if is_valid: | ||
| 2501 | save_path = '{0}_{1}{2}'.format(pre, field, suf) | ||
| 2502 | field_img = last_img[coord_tuple[0]:coord_tuple[1], coord_tuple[2]:coord_tuple[3], :] | ||
| 2503 | cv2.imwrite(save_path, field_img) | ||
| 2504 | field_img_path_dict[field] = save_path | ||
| 2505 | else: | ||
| 2506 | field_img_path_dict[field] = base_img_path | ||
| 2507 | except Exception as e: | ||
| 2508 | field_img_path_dict[field] = base_img_path | ||
| 2509 | |||
| 2510 | return result_field_list, no_ocr_result, field_img_path_dict | ||
| 2511 | |||
| 2512 | |||
| 2336 | def se_contract_compare(license_en, ocr_res_dict, strip_list, is_gsyh): | 2513 | def se_contract_compare(license_en, ocr_res_dict, strip_list, is_gsyh): |
| 2337 | ocr_field, compare_logic, _ = consts.SE_COMPARE_FIELD[license_en] | 2514 | ocr_field, compare_logic, _ = consts.SE_COMPARE_FIELD[license_en] |
| 2338 | ocr_res_str = ocr_res_dict.get(ocr_field) | 2515 | ocr_res_str = ocr_res_dict.get(ocr_field) |
| ... | @@ -2557,7 +2734,7 @@ def se_mvc34_compare(license_en, ocr_res_dict, field_list): | ... | @@ -2557,7 +2734,7 @@ def se_mvc34_compare(license_en, ocr_res_dict, field_list): |
| 2557 | return result_field_list, field_img_path_dict | 2734 | return result_field_list, field_img_path_dict |
| 2558 | 2735 | ||
| 2559 | 2736 | ||
| 2560 | def se_compare_process(compare_info, ocr_res_dict, is_gsyh, is_auto): | 2737 | def se_compare_process(compare_info, ocr_res_dict, is_gsyh, is_auto, id_res_list): |
| 2561 | # individualCusInfo | 2738 | # individualCusInfo |
| 2562 | # corporateCusInfo | 2739 | # corporateCusInfo |
| 2563 | # vehicleInfo | 2740 | # vehicleInfo |
| ... | @@ -2590,7 +2767,13 @@ def se_compare_process(compare_info, ocr_res_dict, is_gsyh, is_auto): | ... | @@ -2590,7 +2767,13 @@ def se_compare_process(compare_info, ocr_res_dict, is_gsyh, is_auto): |
| 2590 | else: | 2767 | else: |
| 2591 | strip_list.append((a, b)) | 2768 | strip_list.append((a, b)) |
| 2592 | failure_field = [] | 2769 | failure_field = [] |
| 2593 | result_field_list, no_ocr_result, field_img_path_dict = se_compare_license(license_en, ocr_res_dict, strip_list) | 2770 | # 身份证先SE正反面,后CA正反面 |
| 2771 | if license_en == consts.ID_EN: | ||
| 2772 | result_field_list, no_ocr_result, field_img_path_dict = se_compare_license_id( | ||
| 2773 | license_en, id_res_list, strip_list) | ||
| 2774 | else: | ||
| 2775 | result_field_list, no_ocr_result, field_img_path_dict = se_compare_license( | ||
| 2776 | license_en, ocr_res_dict, strip_list) | ||
| 2594 | for name, value, result, ocr_str, img_path, error_type, cn_reason in result_field_list: | 2777 | for name, value, result, ocr_str, img_path, error_type, cn_reason in result_field_list: |
| 2595 | if license_en not in consts.SKIP_CARD or not no_ocr_result: | 2778 | if license_en not in consts.SKIP_CARD or not no_ocr_result: |
| 2596 | total_fields += 1 | 2779 | total_fields += 1 |
| ... | @@ -2709,13 +2892,15 @@ def se_result_detect(ocr_res_dict): | ... | @@ -2709,13 +2892,15 @@ def se_result_detect(ocr_res_dict): |
| 2709 | return detect_list | 2892 | return detect_list |
| 2710 | 2893 | ||
| 2711 | 2894 | ||
| 2712 | def se_compare_auto(application_id, application_entity, ocr_res_id, last_obj, ocr_res_dict, auto_obj, ignore_bank): | 2895 | def se_compare_auto(application_id, application_entity, ocr_res_id, last_obj, ocr_res_dict, auto_obj, ignore_bank, id_res_list): |
| 2713 | try: | 2896 | try: |
| 2714 | # 比对逻辑 | 2897 | # 比对逻辑 |
| 2715 | # detect_list = se_result_detect(ocr_res_dict) | 2898 | # detect_list = se_result_detect(ocr_res_dict) |
| 2716 | compare_info, aa_type, is_gsyh = get_se_cms_compare_info_auto( | 2899 | compare_info, aa_type, is_gsyh = get_se_cms_compare_info_auto( |
| 2717 | last_obj, application_entity, ignore_bank=ignore_bank) | 2900 | last_obj, application_entity, ignore_bank=ignore_bank) |
| 2718 | compare_result, total_fields, failed_count, successful_at_this_level, failure_reason_str, cn_failure_reason_str, bs_failure_reason_str, _ = se_compare_process(compare_info, ocr_res_dict, is_gsyh, True) | 2901 | compare_result, total_fields, failed_count, successful_at_this_level, failure_reason_str, \ |
| 2902 | cn_failure_reason_str, bs_failure_reason_str, _ = se_compare_process( | ||
| 2903 | compare_info, ocr_res_dict, is_gsyh, True, id_res_list) | ||
| 2719 | compare_log.info('{0} [Auto SE] [compare success] [entity={1}] [id={2}] [ocr_res_id={3}] [result={4}]'.format( | 2904 | compare_log.info('{0} [Auto SE] [compare success] [entity={1}] [id={2}] [ocr_res_id={3}] [result={4}]'.format( |
| 2720 | log_base, application_entity, application_id, ocr_res_id, compare_result)) | 2905 | log_base, application_entity, application_id, ocr_res_id, compare_result)) |
| 2721 | except Exception as e: | 2906 | except Exception as e: |
| ... | @@ -2753,14 +2938,17 @@ def se_compare_auto(application_id, application_entity, ocr_res_id, last_obj, oc | ... | @@ -2753,14 +2938,17 @@ def se_compare_auto(application_id, application_entity, ocr_res_id, last_obj, oc |
| 2753 | return successful_at_this_level | 2938 | return successful_at_this_level |
| 2754 | 2939 | ||
| 2755 | 2940 | ||
| 2756 | def se_compare(application_id, application_entity, ocr_res_id, last_obj, ocr_res_dict, is_cms, auto_result, ignore_bank): | 2941 | def se_compare(application_id, application_entity, ocr_res_id, last_obj, ocr_res_dict, is_cms, |
| 2942 | auto_result, ignore_bank, id_res_list): | ||
| 2757 | try: | 2943 | try: |
| 2758 | # 比对逻辑 | 2944 | # 比对逻辑 |
| 2759 | start_time = datetime.now() | 2945 | start_time = datetime.now() |
| 2760 | detect_list = se_result_detect(ocr_res_dict) | 2946 | detect_list = se_result_detect(ocr_res_dict) |
| 2761 | compare_info, application_version, is_gsyh = get_se_cms_compare_info( | 2947 | compare_info, application_version, is_gsyh = get_se_cms_compare_info( |
| 2762 | last_obj, application_entity, detect_list, ignore_bank=ignore_bank) | 2948 | last_obj, application_entity, detect_list, ignore_bank=ignore_bank) |
| 2763 | compare_result, total_fields, failed_count, successful_at_this_level, failure_reason_str, cn_failure_reason_str, bs_failure_reason_str, rpa_failure_reason = se_compare_process(compare_info, ocr_res_dict, is_gsyh, False) | 2949 | compare_result, total_fields, failed_count, successful_at_this_level, failure_reason_str, \ |
| 2950 | cn_failure_reason_str, bs_failure_reason_str, rpa_failure_reason = se_compare_process( | ||
| 2951 | compare_info, ocr_res_dict, is_gsyh, False, id_res_list) | ||
| 2764 | compare_log.info('{0} [SE] [compare success] [entity={1}] [id={2}] [ocr_res_id={3}] [result={4}]'.format( | 2952 | compare_log.info('{0} [SE] [compare success] [entity={1}] [id={2}] [ocr_res_id={3}] [result={4}]'.format( |
| 2765 | log_base, application_entity, application_id, ocr_res_id, compare_result)) | 2953 | log_base, application_entity, application_id, ocr_res_id, compare_result)) |
| 2766 | except Exception as e: | 2954 | except Exception as e: |
| ... | @@ -2902,7 +3090,12 @@ def compare(application_id, application_entity, uniq_seq, ocr_res_id, is_ca=True | ... | @@ -2902,7 +3090,12 @@ def compare(application_id, application_entity, uniq_seq, ocr_res_id, is_ca=True |
| 2902 | if is_ca: | 3090 | if is_ca: |
| 2903 | ca_compare(application_id, application_entity, ocr_res_id, last_obj, ocr_res_dict) | 3091 | ca_compare(application_id, application_entity, ocr_res_id, last_obj, ocr_res_dict) |
| 2904 | else: | 3092 | else: |
| 3093 | id_res_list = [] | ||
| 2905 | for field_name in consts.CA_ADD_COMPARE_FIELDS: | 3094 | for field_name in consts.CA_ADD_COMPARE_FIELDS: |
| 3095 | if field_name == consts.IC_OCR_FIELD: | ||
| 3096 | id_res_list.append(ocr_res_dict.get(field_name)) | ||
| 3097 | id_res_list.append(ca_ocr_res_dict.get(field_name) if isinstance(ca_ocr_res_dict, dict) else None) | ||
| 3098 | |||
| 2906 | if isinstance(ca_ocr_res_dict, dict) and isinstance(ca_ocr_res_dict.get(field_name), str): | 3099 | if isinstance(ca_ocr_res_dict, dict) and isinstance(ca_ocr_res_dict.get(field_name), str): |
| 2907 | tmp_ca_result = json.loads(ca_ocr_res_dict.get(field_name)) | 3100 | tmp_ca_result = json.loads(ca_ocr_res_dict.get(field_name)) |
| 2908 | if isinstance(ocr_res_dict.get(field_name), str): | 3101 | if isinstance(ocr_res_dict.get(field_name), str): |
| ... | @@ -2915,11 +3108,11 @@ def compare(application_id, application_entity, uniq_seq, ocr_res_id, is_ca=True | ... | @@ -2915,11 +3108,11 @@ def compare(application_id, application_entity, uniq_seq, ocr_res_id, is_ca=True |
| 2915 | bank_class = HILbankVerification if application_entity == consts.HIL_PREFIX else AFCbankVerification | 3108 | bank_class = HILbankVerification if application_entity == consts.HIL_PREFIX else AFCbankVerification |
| 2916 | ignore_bank = bank_class.objects.filter(application_id=application_id, on_off=True).exists() | 3109 | ignore_bank = bank_class.objects.filter(application_id=application_id, on_off=True).exists() |
| 2917 | if auto_obj is not None: | 3110 | if auto_obj is not None: |
| 2918 | auto_result = se_compare_auto(application_id, application_entity, ocr_res_id, last_obj, ocr_res_dict, auto_obj, ignore_bank) | 3111 | auto_result = se_compare_auto(application_id, application_entity, ocr_res_id, last_obj, ocr_res_dict, auto_obj, ignore_bank, id_res_list) |
| 2919 | else: | 3112 | else: |
| 2920 | auto_result = None | 3113 | auto_result = None |
| 2921 | 3114 | ||
| 2922 | full_result = se_compare(application_id, application_entity, ocr_res_id, last_obj, ocr_res_dict, is_cms, auto_result, ignore_bank) | 3115 | full_result = se_compare(application_id, application_entity, ocr_res_id, last_obj, ocr_res_dict, is_cms, auto_result, ignore_bank, id_res_list) |
| 2923 | 3116 | ||
| 2924 | if auto_obj is not None: | 3117 | if auto_obj is not None: |
| 2925 | try: | 3118 | try: | ... | ... |
-
Please register or sign in to post a comment