pos_compare.py
58.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
from common.tools.comparison import cp
import os
from pos import pos_consts
from apps.doc import consts
import json
from apps.doc.named_enum import RequestTeam, RequestTrigger, ProcessName, ErrorType
import time
empty_str = ''
empty_error_type = 1000
class SECompare:
@staticmethod
def se_mvc34_compare(license_en, ocr_res_dict, field_list):
ocr_field, compare_logic, _ = pos_consts.SE_COMPARE_FIELD[license_en]
ocr_res_str = ocr_res_dict.get(ocr_field)
is_find = False
result_field_list = []
field_img_path_dict = dict()
if ocr_res_str is not None:
ocr_res_list = json.loads(ocr_res_str)
length = len(ocr_res_list)
page34_date_dict = dict()
first_res = None
for res_idx in range(length - 1, -1, -1):
if consts.TRANSFER_DATE in ocr_res_list[res_idx]:
img_path = ocr_res_list[res_idx].get(consts.IMG_PATH_KEY_2, '')
section_img_path = ocr_res_list[res_idx].get(consts.SECTION_IMG_PATH_KEY_2, '')
for idx, transfer_date in enumerate(ocr_res_list[res_idx].get(consts.TRANSFER_DATE, [])):
try:
transfer_name = ocr_res_list[res_idx].get(consts.TRANSFER_NAME, [])[idx]
except Exception as e:
transfer_name = empty_str
try:
transfer_num = ocr_res_list[res_idx].get(consts.TRANSFER_NUM, [])[idx]
except Exception as e:
transfer_num = empty_str
try:
position_info_date = ocr_res_list[res_idx].get(consts.ALL_POSITION_KEY_2, dict()).get(
consts.TRANSFER_DATE, [])[idx]
except Exception as e:
position_info_date = {}
try:
position_info_name = ocr_res_list[res_idx].get(consts.ALL_POSITION_KEY_2, dict()).get(
consts.TRANSFER_NAME, [])[idx]
except Exception as e:
position_info_name = {}
try:
position_info_num = ocr_res_list[res_idx].get(consts.ALL_POSITION_KEY_2, dict()).get(
consts.TRANSFER_NUM, [])[idx]
except Exception as e:
position_info_num = {}
core_info = {
consts.TRANSFER_NAME: transfer_name,
consts.TRANSFER_NUM: transfer_num,
consts.TRANSFER_DATE: transfer_date,
consts.IMG_PATH_KEY_2: img_path,
consts.SECTION_IMG_PATH_KEY_2: section_img_path,
consts.ALL_POSITION_KEY: {
consts.TRANSFER_NAME: position_info_name,
consts.TRANSFER_NUM: position_info_num,
consts.TRANSFER_DATE: position_info_date,
},
}
page34_date_dict.setdefault(transfer_date, []).append(core_info)
if first_res is None:
first_res = core_info
max_date = None
for date_tmp in page34_date_dict.keys():
try:
max_date_part = time.strptime(date_tmp, "%Y-%m-%d")
except Exception as e:
pass
else:
if max_date is None or max_date_part > max_date:
max_date = max_date_part
if max_date is not None or first_res is not None:
is_find = True
ocr_res = first_res if max_date is None else page34_date_dict[time.strftime('%Y-%m-%d', max_date)][0]
failed_field = []
base_img_path = ocr_res.get(consts.IMG_PATH_KEY_2, '')
for name, value in field_list:
ocr_str = ocr_res.get(compare_logic[name][0])
if not isinstance(ocr_str, str):
result = consts.RESULT_N
ocr_str = empty_str
else:
result = getattr(cp, compare_logic[name][1])(value, ocr_str, **compare_logic[name][2])
img_path = base_img_path if result == consts.RESULT_N else empty_str
error_type = empty_error_type if result == consts.RESULT_Y else ErrorType.OCR.value
result_field_list.append(
(name, value, result, ocr_str, img_path, error_type, compare_logic[name][3]))
if result == consts.RESULT_N:
failed_field.append(name)
# section_img_path = ocr_res.get(consts.SECTION_IMG_PATH_KEY_2, '')
# if len(failed_field) > 0 and os.path.exists(section_img_path):
# info = ocr_res.get(consts.ALL_POSITION_KEY, {})
# try:
# last_img = img_process(section_img_path, {}, 0)
# except Exception as e:
# for field in failed_field:
# field_img_path_dict[field] = base_img_path
# else:
# pre, suf = os.path.splitext(section_img_path)
# for field in failed_field:
# try:
# res_field = compare_logic[field][0]
# is_valid, coord_tuple = field_build_coordinates(info.get(res_field, {}))
# if is_valid:
# save_path = '{0}_{1}{2}'.format(pre, field, suf)
# field_img = last_img[coord_tuple[0]:coord_tuple[1], coord_tuple[2]:coord_tuple[3],
# :]
# cv2.imwrite(save_path, field_img)
# field_img_path_dict[field] = save_path
# else:
# field_img_path_dict[field] = base_img_path
# except Exception as e:
# field_img_path_dict[field] = base_img_path
if not is_find:
for name, value in field_list:
result_field_list.append((name, value, consts.RESULT_N, empty_str, empty_str, ErrorType.NF.value,
'{0}未找到'.format(license_en)))
return result_field_list, field_img_path_dict
@staticmethod
def se_contract_compare(license_en, ocr_res_dict, strip_list, is_gsyh):
ocr_field, compare_logic, _ = pos_consts.SE_COMPARE_FIELD[license_en]
ocr_res_str = ocr_res_dict.get(ocr_field)
result_field_list = []
field_img_path_dict = dict()
ocr_res = dict()
if ocr_res_str is not None:
ocr_res_list = json.loads(ocr_res_str)
ocr_res = ocr_res_list.pop()
for name, value in strip_list:
# 购置税校验
# if name == consts.SE_AFC_CON_FIELD[21]:
# if len(value) == 3:
# reason = []
# gzs_verify = value[1] >= value[2]
# if gzs_verify:
# if value[0] == consts.GZS_STATUS[0]:
# reason.append(consts.GZS_REASON_1)
# result = consts.RESULT_N
# else:
# result = consts.RESULT_Y
# else:
# if value[0] == consts.GZS_STATUS[0]:
# reason.append(consts.GZS_REASON_1)
# result = consts.RESULT_N
# reason.append(consts.GZS_REASON_2)
# else:
# result = consts.RESULT_N
# reason = consts.GZS_REASON_1
# ocr_str = empty_str
# else:
if name == consts.SE_HIL_CON_1_FIELD[9] or name == consts.SE_HIL_CON_1_FIELD[15] or \
name == consts.SE_AFC_CON_FIELD[21] or name == consts.SE_AFC_CON_FIELD[24]:
ocr_str_or_list = ''
else:
ocr_str_or_list = ocr_res.get(compare_logic[name][0])
# 招商银行特殊
# if ocr_str_or_list is None and license_en == consts.AFC_CONTRACT_EN \
# and is_gsyh is True and name in consts.CON_BANK_FIELD:
# result = consts.RESULT_Y
# ocr_str = empty_str
# reason = compare_logic[name][3]
# 见证人日期
if name == consts.SE_AFC_CON_FIELD[19]:
if not isinstance(ocr_str_or_list, str) or len(ocr_str_or_list) == 0:
result = consts.RESULT_N
ocr_str = empty_str
else:
is_find_date = False
all_date_list = [ocr_str_or_list]
for date_name in consts.AFC_HT_DATE_FIELDS:
all_date_list.append(ocr_res.get(compare_logic[date_name][0], ''))
if not is_find_date and ocr_str_or_list == ocr_res.get(compare_logic[date_name][0], ''):
is_find_date = True
result = consts.RESULT_Y if is_find_date else consts.RESULT_N
ocr_str = json.dumps(all_date_list, ensure_ascii=False)
reason = compare_logic[name][3]
elif isinstance(ocr_str_or_list, str) or isinstance(ocr_str_or_list, list):
# if is_gsyh is True and name in consts.CON_BANK_FIELD:
# update_args = {'is_gsyh': is_gsyh}
# for k, v in compare_logic[name][2].items():
# update_args[k] = v
# else:
# update_args = compare_logic[name][2]
if isinstance(ocr_str_or_list, list):
# no-asp 合同编号-每页(no-asp)
if name == consts.SE_AFC_CON_FIELD[23]:
ocr_str_or_list.pop()
ocr_str = json.dumps(ocr_str_or_list, ensure_ascii=False)
else:
ocr_str_or_list = ocr_str_or_list.strip()
ocr_str = ocr_str_or_list
result = getattr(cp, compare_logic[name][1])(value, ocr_str_or_list, **compare_logic[name][2])
reason = compare_logic[name][3]
else:
result = consts.RESULT_N
ocr_str = empty_str
reason = compare_logic[name][3]
# img_path = empty_str
if name not in compare_logic:
img_path = empty_str
else:
img_path = ocr_res.get(consts.IMG_PATH_KEY, {}).get(compare_logic[name][0],
empty_str) if result == consts.RESULT_N else empty_str
error_type = empty_error_type if result == consts.RESULT_Y else ErrorType.OCR.value
if isinstance(value, list):
value = json.dumps(value, ensure_ascii=False)
result_field_list.append((name, value, result, ocr_str, img_path, error_type, reason))
else:
for name, value in strip_list:
if isinstance(value, list):
value = json.dumps(value, ensure_ascii=False)
result_field_list.append((name, value, consts.RESULT_N, empty_str, empty_str, ErrorType.NF.value,
'{0}未找到'.format(license_en)))
# if ocr_res_str is not None:
# img_map = {}
# for name, _, result, _, img_path, _, _ in result_field_list:
# if result == consts.RESULT_N:
# img_map.setdefault(img_path, []).append(name)
# for path, field_list in img_map.items():
# if os.path.exists(path):
# pre, suf = os.path.splitext(path)
# last_img = cv2.imread(path)
# for field_idx, field in enumerate(field_list):
# try:
# save_path = '{0}_{1}{2}'.format(pre, str(field_idx), suf)
# section_position_list = ocr_res.get(consts.ALL_POSITION_KEY, {}).get(field, [])
# if isinstance(section_position_list, list) and len(section_position_list) == 4:
# field_img = last_img[section_position_list[1]: section_position_list[3],
# section_position_list[0]: section_position_list[2], :]
# cv2.imwrite(save_path, field_img)
# field_img_path_dict[field] = save_path
# else:
# field_img_path_dict[field] = path
# except Exception as e:
# field_img_path_dict[field] = path
return result_field_list, field_img_path_dict
@staticmethod
def se_bs_compare(license_en, ocr_res_dict, strip_list, is_auto):
# 主共借至少提供一个
# 有担保人,担保人必须提供。主共借没有时,修改comment:人工查看担保人亲属关系
if is_auto:
ocr_field, compare_logic, _ = consts.SE_COMPARE_FIELD_AUTO[license_en]
else:
ocr_field, compare_logic, _ = pos_consts.SE_COMPARE_FIELD[license_en]
ocr_res_str = ocr_res_dict.get(ocr_field)
result_field_list = []
field_img_path_dict = dict()
if ocr_res_str is not None:
pre_field_list = strip_list[:3]
dbr1_field_list = strip_list[3:6]
dbr2_field_list = strip_list[6:]
ocr_res_list = json.loads(ocr_res_str)
# length = len(ocr_res_list)
# 主共借人
pre_best_res = {}
max_correct_count = 0
verify_list = []
verify_false_idx_list = []
for tmp_idx, ocr_res in enumerate(ocr_res_list):
correct_count = 0
pre_tmp_res_part = {}
verify_bool = ocr_res.get('verify', True)
verify_list.append(verify_bool)
if not verify_bool:
verify_false_idx_list.append(str(tmp_idx + 1))
for idx, (name, value) in enumerate(pre_field_list):
ocr_str_or_list = ocr_res.get(compare_logic[name][0])
if isinstance(ocr_str_or_list, str) or isinstance(ocr_str_or_list, list) \
or isinstance(ocr_str_or_list, int):
result = getattr(cp, compare_logic[name][1])(value, ocr_str_or_list, **compare_logic[name][2])
if isinstance(ocr_str_or_list, list):
ocr_str = json.dumps(ocr_str_or_list, ensure_ascii=False)
else:
ocr_str = ocr_str_or_list
reason = compare_logic[name][3]
else:
result = consts.RESULT_N
ocr_str = empty_str
reason = compare_logic[name][3]
if idx == 0 and result == consts.RESULT_N:
break
if result == consts.RESULT_Y:
correct_count += 1
pre_tmp_res_part[name] = (result, ocr_str, reason)
if correct_count > 0 and correct_count >= max_correct_count:
max_correct_count = correct_count
pre_best_res = pre_tmp_res_part
# 真伪
if not is_auto:
name = '真伪'
result = consts.RESULT_Y if all(verify_list) else consts.RESULT_N
reason = '第{0}份银行流水疑似造假,需人工核查'.format('、'.join(verify_false_idx_list))
result_field_list.append((name, empty_str, result, json.dumps(verify_list, ensure_ascii=False),
empty_str, empty_error_type, reason))
# 担保人1
dbr1_best_res = {}
if len(dbr1_field_list) > 0:
max_correct_count = 0
for ocr_res in ocr_res_list:
correct_count = 0
dbr1_tmp_res_part = {}
for idx, (name, value) in enumerate(dbr1_field_list):
ocr_str_or_list = ocr_res.get(compare_logic[name][0])
if isinstance(ocr_str_or_list, str) or isinstance(ocr_str_or_list, list) or isinstance(
ocr_str_or_list, int):
result = getattr(cp, compare_logic[name][1])(value, ocr_str_or_list,
**compare_logic[name][2])
if isinstance(ocr_str_or_list, list):
ocr_str = json.dumps(ocr_str_or_list, ensure_ascii=False)
else:
ocr_str = ocr_str_or_list
reason = compare_logic[name][3]
else:
result = consts.RESULT_N
ocr_str = empty_str
reason = compare_logic[name][3]
if idx == 0 and result == consts.RESULT_N:
break
if result == consts.RESULT_Y:
correct_count += 1
dbr1_tmp_res_part[name] = (result, ocr_str, reason)
if correct_count > 0 and correct_count >= max_correct_count:
max_correct_count = correct_count
dbr1_best_res = dbr1_tmp_res_part
# 担保人2
dbr2_best_res = {}
if len(dbr1_field_list) > 0:
max_correct_count = 0
for ocr_res in ocr_res_list:
correct_count = 0
dbr2_tmp_res_part = {}
for idx, (name, value) in enumerate(dbr2_field_list):
ocr_str_or_list = ocr_res.get(compare_logic[name][0])
if isinstance(ocr_str_or_list, str) or isinstance(ocr_str_or_list, list) or isinstance(
ocr_str_or_list, int):
result = getattr(cp, compare_logic[name][1])(value, ocr_str_or_list,
**compare_logic[name][2])
if isinstance(ocr_str_or_list, list):
ocr_str = json.dumps(ocr_str_or_list, ensure_ascii=False)
else:
ocr_str = ocr_str_or_list
reason = compare_logic[name][3]
else:
result = consts.RESULT_N
ocr_str = empty_str
reason = compare_logic[name][3]
if idx == 0 and result == consts.RESULT_N:
break
if result == consts.RESULT_Y:
correct_count += 1
dbr2_tmp_res_part[name] = (result, ocr_str, reason)
if correct_count > 0 and correct_count >= max_correct_count:
max_correct_count = correct_count
dbr2_best_res = dbr2_tmp_res_part
dbr_ok = False
# 有担保人
if len(dbr1_field_list) > 0:
# 有担保人12
if len(dbr2_field_list) > 0:
if len(dbr1_best_res) > 0 and len(dbr2_best_res) > 0:
dbr_ok = True
# 有担保人1
else:
if len(dbr1_best_res) > 0:
dbr_ok = True
# 无担保人
# else:
# pass
best_res_empty = False
if len(pre_best_res) == 0 and len(dbr1_best_res) == 0 and len(dbr2_best_res) == 0:
best_res_empty = True
for name, value in pre_field_list:
if len(pre_best_res) > 0:
result, ocr_str, reason = pre_best_res[name]
else:
result = consts.RESULT_N
ocr_str = empty_str
if best_res_empty:
reason = consts.SPECIAL_REASON_3
elif dbr_ok: # 有担保人且担保人都提供了流水
reason = consts.SPECIAL_REASON
else:
reason = compare_logic[pre_field_list[0][0]][3]
img_path = empty_str
error_type = empty_error_type if result == consts.RESULT_Y else ErrorType.OCR.value
if isinstance(value, list):
value = json.dumps(value, ensure_ascii=False)
result_field_list.append((name, value, result, ocr_str, img_path, error_type, reason))
if len(dbr1_field_list) > 0:
for name, value in dbr1_field_list:
if len(dbr1_best_res) > 0:
result, ocr_str, reason = dbr1_best_res[name]
else:
result = consts.RESULT_N
ocr_str = empty_str
if best_res_empty:
reason = consts.SPECIAL_REASON_3
elif len(pre_best_res) > 0:
reason = consts.SPECIAL_REASON_2
else:
reason = compare_logic[dbr1_field_list[0][0]][3]
img_path = empty_str
error_type = empty_error_type if result == consts.RESULT_Y else ErrorType.OCR.value
if isinstance(value, list):
value = json.dumps(value, ensure_ascii=False)
result_field_list.append((name, value, result, ocr_str, img_path, error_type, reason))
if len(dbr2_field_list) > 0:
for name, value in dbr2_field_list:
if len(dbr2_best_res) > 0:
result, ocr_str, reason = dbr2_best_res[name]
else:
result = consts.RESULT_N
ocr_str = empty_str
if best_res_empty:
reason = consts.SPECIAL_REASON_3
elif len(pre_best_res) > 0:
reason = consts.SPECIAL_REASON_2
else:
reason = compare_logic[dbr2_field_list[0][0]][3]
img_path = empty_str
error_type = empty_error_type if result == consts.RESULT_Y else ErrorType.OCR.value
if isinstance(value, list):
value = json.dumps(value, ensure_ascii=False)
result_field_list.append((name, value, result, ocr_str, img_path, error_type, reason))
else:
for name, value in strip_list:
if isinstance(value, list):
value = json.dumps(value, ensure_ascii=False)
result_field_list.append((name, value, consts.RESULT_N, empty_str, empty_str, ErrorType.NF.value,
consts.SPECIAL_REASON_3))
return result_field_list, field_img_path_dict
@staticmethod
def se_compare_license_id(license_en, id_res_list, field_list):
ocr_field, compare_logic, special_expiry_date = pos_consts.SE_COMPARE_FIELD[license_en]
is_find = False
no_ocr_result = True
special_expiry_date_slice = False
result_field_list = []
section_img_info = dict()
field_img_path_dict = dict()
# ocr_res_str = ocr_res_dict.get(ocr_field)
for ocr_res_str in id_res_list:
if is_find:
break
if ocr_res_str is not None:
no_ocr_result = False
ocr_res_list = json.loads(ocr_res_str)
# 3/4页去除
# if ocr_field == consts.MVC_OCR_FIELD:
# tmp_list = []
# for res in ocr_res_list:
# if compare_logic['vinNo'][0] in res:
# tmp_list.append(res)
# ocr_res_list = tmp_list
length = len(ocr_res_list)
# 身份证、居住证 过期期限特殊处理
if special_expiry_date:
expiry_dates = dict()
key = compare_logic.get('idExpiryDate')[0]
for date_tmp_idx, ocr_res in enumerate(ocr_res_list):
if key in ocr_res:
expiry_dates[ocr_res[key]] = (ocr_res.get(consts.IMG_PATH_KEY_2, ''), date_tmp_idx)
else:
expiry_dates = dict()
for res_idx in range(length - 1, -1, -1):
if is_find:
break
for idx, (name, value) in enumerate(field_list):
# if ocr_field == consts.MVI_OCR_FIELD and name == consts.SE_NEW_ADD_FIELD[9]:
# ocr_str = getattr(cp, consts.ZW_METHOD)(
# ocr_res_list[res_idx].get(consts.LOWER_AMOUNT_FIELD, ''),
# ocr_res_list[res_idx].get(consts.UPPER_AMOUNT_FIELD, ''),
# )
# else:
ocr_str = ocr_res_list[res_idx].get(compare_logic[name][0])
if not isinstance(ocr_str, str):
result = consts.RESULT_N
ocr_str = empty_str
no_key = True
else:
result = getattr(cp, compare_logic[name][1])(value, ocr_str, **compare_logic[name][2])
no_key = False
if idx == 0 and result == consts.RESULT_N and length > 1:
break
is_find = True
section_img_info[consts.SECTION_IMG_PATH_KEY] = ocr_res_list[res_idx].get(
consts.SECTION_IMG_PATH_KEY, '')
section_img_info[consts.ALL_POSITION_KEY] = ocr_res_list[res_idx].get(consts.ALL_POSITION_KEY,
{})
if special_expiry_date:
section_img_info[consts.SECTION_IMG_PATH_KEY_2] = ocr_res_list[res_idx].get(
consts.SECTION_IMG_PATH_KEY_2, '')
section_img_info[consts.ALL_POSITION_KEY_2] = ocr_res_list[res_idx].get(
consts.ALL_POSITION_KEY_2, {})
# 过期期限特殊处理
if special_expiry_date and name == 'idExpiryDate' and result == consts.RESULT_N:
if no_key:
if len(expiry_dates) == 0:
ocr_str = empty_str
result = consts.RESULT_N
img_path = empty_str
else:
for expiry_date, (date_img_path, date_res_idx) in expiry_dates.items():
expiry_date_res = getattr(cp, compare_logic[name][1])(value, expiry_date,
**compare_logic[name][2])
if expiry_date_res == consts.RESULT_N:
ocr_str = expiry_date
img_path = date_img_path
special_expiry_date_slice = True
section_img_info[consts.SECTION_IMG_PATH_KEY_2] = ocr_res_list[
date_res_idx].get(
consts.SECTION_IMG_PATH_KEY_2, '')
section_img_info[consts.ALL_POSITION_KEY_2] = ocr_res_list[
date_res_idx].get(
consts.ALL_POSITION_KEY_2, {})
break
else:
ocr_str = empty_str
result = consts.RESULT_Y
img_path = empty_str
else:
img_path = ocr_res_list[res_idx].get(consts.IMG_PATH_KEY_2, '')
special_expiry_date_slice = True
else:
img_path = ocr_res_list[res_idx].get(consts.IMG_PATH_KEY,
'') if result == consts.RESULT_N else empty_str
if isinstance(value, list):
value = json.dumps(value, ensure_ascii=False)
error_type = empty_error_type if result == consts.RESULT_Y else ErrorType.OCR.value
result_field_list.append(
(name, value, result, ocr_str, img_path, error_type, compare_logic[name][3]))
if not is_find:
for name, value in field_list:
if isinstance(value, list):
value = json.dumps(value, ensure_ascii=False)
no_find_str = consts.DDA_NO_FIND if license_en == consts.DDA_EN else '{0}未找到'.format(license_en)
result_field_list.append(
(name, value, consts.RESULT_N, empty_str, empty_str, ErrorType.NF.value, no_find_str))
# if is_find:
# if special_expiry_date_slice:
# special_section_img_path = section_img_info.get(consts.SECTION_IMG_PATH_KEY_2, '')
# if os.path.exists(special_section_img_path):
# field = 'idExpiryDate'
# special_info = section_img_info.get(consts.ALL_POSITION_KEY_2, {})
# special_section_position = special_info.get(consts.POSITION_KEY, {})
# special_section_angle = special_info.get(consts.ANGLE_KEY, 0)
# try:
# last_img = img_process(special_section_img_path, special_section_position,
# special_section_angle)
# except Exception as e:
# field_img_path_dict[field] = special_section_img_path
# else:
# pre, suf = os.path.splitext(special_section_img_path)
# try:
# res_field = compare_logic[field][0]
# is_valid, coord_tuple = field_build_coordinates(special_info.get(res_field, {}))
# if is_valid:
# save_path = '{0}_{1}{2}'.format(pre, field, suf)
# field_img = last_img[coord_tuple[0]:coord_tuple[1], coord_tuple[2]:coord_tuple[3], :]
# cv2.imwrite(save_path, field_img)
# field_img_path_dict[field] = save_path
# else:
# field_img_path_dict[field] = special_section_img_path
# except Exception as e:
# field_img_path_dict[field] = special_section_img_path
#
# section_img_path = section_img_info.get(consts.SECTION_IMG_PATH_KEY, '')
# if os.path.exists(section_img_path):
# failed_field = []
# base_img_path = empty_str
# for name, _, result, _, img_path, _, _ in result_field_list:
# if result == consts.RESULT_N:
# if special_expiry_date_slice and name == 'idExpiryDate':
# continue
# failed_field.append(name)
# if base_img_path == empty_str:
# base_img_path = img_path
# if len(failed_field) > 0:
# info = section_img_info.get(consts.ALL_POSITION_KEY, {})
# section_position = info.get(consts.POSITION_KEY, {})
# section_angle = info.get(consts.ANGLE_KEY, 0)
# try:
# last_img = img_process(section_img_path, section_position, section_angle)
# except Exception as e:
# for field in failed_field:
# field_img_path_dict[field] = base_img_path
# else:
# pre, suf = os.path.splitext(section_img_path)
# for field in failed_field:
# try:
# res_field = compare_logic[field][0]
# is_valid, coord_tuple = field_build_coordinates(info.get(res_field, {}))
# if is_valid:
# save_path = '{0}_{1}{2}'.format(pre, field, suf)
# field_img = last_img[coord_tuple[0]:coord_tuple[1], coord_tuple[2]:coord_tuple[3],
# :]
# cv2.imwrite(save_path, field_img)
# field_img_path_dict[field] = save_path
# else:
# field_img_path_dict[field] = base_img_path
# except Exception as e:
# field_img_path_dict[field] = base_img_path
return result_field_list, no_ocr_result, field_img_path_dict
@staticmethod
def se_compare_license(license_en, ocr_res_dict, field_list):
ocr_field, compare_logic, special_expiry_date = pos_consts.SE_COMPARE_FIELD[license_en]
is_find = False
no_ocr_result = False
special_expiry_date_slice = False
result_field_list = []
section_img_info = dict()
field_img_path_dict = dict()
ocr_res_str = ocr_res_dict.get(ocr_field)
if ocr_res_str is not None:
ocr_res_list = json.loads(ocr_res_str)
# 3/4页去除
if ocr_field == consts.MVC_OCR_FIELD:
tmp_list = []
for res in ocr_res_list:
if compare_logic['vinNo'][0] in res:
tmp_list.append(res)
ocr_res_list = tmp_list
length = len(ocr_res_list)
# 身份证、居住证 过期期限特殊处理
if special_expiry_date:
expiry_dates = dict()
key = compare_logic.get('idExpiryDate')[0]
for date_tmp_idx, ocr_res in enumerate(ocr_res_list):
if key in ocr_res:
expiry_dates[ocr_res[key]] = (ocr_res.get(consts.IMG_PATH_KEY_2, ''), date_tmp_idx)
else:
expiry_dates = dict()
for res_idx in range(length - 1, -1, -1):
if is_find:
break
new_invoice_result = None
for idx, (name, value) in enumerate(field_list):
# 二手车交易凭证 日期
if ocr_field == consts.JYPZ_OCR_FIELD and name == consts.SE_GB_USED_FIELD[2]:
date_1 = ocr_res_list[res_idx].get(consts.JYPZ_DATE_FIELD_1, '')
if len(date_1) > 0:
date_2 = date_3 = ''
else:
date_1 = ocr_res_list[res_idx].get(consts.JYPZ_DATE_FIELD_2, '')
date_2 = ocr_res_list[res_idx].get(consts.JYPZ_DATE_FIELD_3, '')
date_3 = ocr_res_list[res_idx].get(consts.JYPZ_DATE_FIELD_4, '')
ocr_str = [date_1, date_2, date_3]
# 购车发票 价税合计大小写检验
elif ocr_field == consts.MVI_OCR_FIELD and name == consts.SE_NEW_ADD_FIELD[9]:
ocr_str = getattr(cp, consts.ZW_METHOD)(
ocr_res_list[res_idx].get(consts.LOWER_AMOUNT_FIELD, ''),
ocr_res_list[res_idx].get(consts.UPPER_AMOUNT_FIELD, ''),
)
else:
ocr_str = ocr_res_list[res_idx].get(compare_logic[name][0])
if isinstance(ocr_str, str):
result = getattr(cp, compare_logic[name][1])(value, ocr_str, **compare_logic[name][2])
no_key = False
# 二手车交易凭证 日期
elif ocr_field == consts.JYPZ_OCR_FIELD and name == consts.SE_GB_USED_FIELD[2]:
result = getattr(cp, compare_logic[name][1])(value, ocr_str, **compare_logic[name][2])
no_key = False
else:
result = consts.RESULT_N
ocr_str = empty_str
no_key = True
if name == '发票联':
new_invoice_result = result
if name == 'productGroupName' and (name.find('官方认证二手车') > -1 or name.find('官方认证二手车') > -1):
# 发票联必须
if new_invoice_result and new_invoice_result == consts.RESULT_Y:
result = consts.RESULT_Y
else:
result = consts.RESULT_N
if idx == 0 and result == consts.RESULT_N and length > 1:
break
is_find = True
# section_img_info[consts.SECTION_IMG_PATH_KEY] = ocr_res_list[res_idx].get(
# consts.SECTION_IMG_PATH_KEY, '')
# section_img_info[consts.ALL_POSITION_KEY] = ocr_res_list[res_idx].get(consts.ALL_POSITION_KEY, {})
# if special_expiry_date:
# section_img_info[consts.SECTION_IMG_PATH_KEY_2] = ocr_res_list[res_idx].get(
# consts.SECTION_IMG_PATH_KEY_2, '')
# section_img_info[consts.ALL_POSITION_KEY_2] = ocr_res_list[res_idx].get(
# consts.ALL_POSITION_KEY_2, {})
# 过期期限特殊处理
if special_expiry_date and name == 'idExpiryDate' and result == consts.RESULT_N:
if no_key:
if len(expiry_dates) == 0:
ocr_str = empty_str
result = consts.RESULT_N
img_path = empty_str
else:
for expiry_date, (date_img_path, date_res_idx) in expiry_dates.items():
expiry_date_res = getattr(cp, compare_logic[name][1])(value, expiry_date,
**compare_logic[name][2])
if expiry_date_res == consts.RESULT_N:
ocr_str = expiry_date
img_path = date_img_path
special_expiry_date_slice = True
section_img_info[consts.SECTION_IMG_PATH_KEY_2] = ocr_res_list[
date_res_idx].get(
consts.SECTION_IMG_PATH_KEY_2, '')
section_img_info[consts.ALL_POSITION_KEY_2] = ocr_res_list[date_res_idx].get(
consts.ALL_POSITION_KEY_2, {})
break
else:
ocr_str = empty_str
result = consts.RESULT_Y
img_path = empty_str
else:
img_path = ocr_res_list[res_idx].get(consts.IMG_PATH_KEY_2, '')
special_expiry_date_slice = True
else:
img_path = ocr_res_list[res_idx].get(consts.IMG_PATH_KEY,
'') if result == consts.RESULT_N else empty_str
if isinstance(value, list):
value = json.dumps(value, ensure_ascii=False)
error_type = empty_error_type if result == consts.RESULT_Y else ErrorType.OCR.value
if ocr_field == pos_consts.IC_OCR_FIELD and name == 'idExpiryDate':
result_field_list.append(
(name, value, result, ocr_str, img_path, error_type, compare_logic[name][-1][result]))
else:
result_field_list.append((name, value, result, ocr_str, img_path, error_type, compare_logic[name][-1]))
else:
no_ocr_result = True
# 针对license文档类型输出
if no_ocr_result:
# 保险
if license_en == pos_consts.BD_EN:
# 整体文档相关
pos_field, value = field_list[0]
if pos_field == 'insuranceType' and value == 'Comprehensive Insurance':
if no_ocr_result:
result_field_list.append((pos_field, value, consts.RESULT_N, ocr_str, img_path, error_type,
compare_logic['文档'][-1]))
elif license_en == pos_consts.HMH_EN: # 抵押登记豁免函
pos_field, value = field_list[0]
if pos_field == 'mortgageType' and value == 'MOTGF':
if no_ocr_result:
result_field_list.append((pos_field, value, consts.RESULT_N, ocr_str, img_path, error_type,
compare_logic['文档'][-1]))
elif license_en == pos_consts.MVI_EN: # 新车发票
pos_field, value = field_list[0]
if pos_field == 'productGroupName' and no_ocr_result:
result_field_list.append(
(pos_field, value, consts.RESULT_N, ocr_str, img_path, error_type,
compare_logic['文档'][-1]))
elif license_en == pos_consts.BC_EN: # 银行卡
pos_field, value = field_list[0]
if pos_field == 'bankVerificationStatus' and (value == 'N/A' or value == 'FAIL'):
if no_ocr_result:
result_field_list.append(
(pos_field, value, consts.RESULT_N, ocr_str, img_path, error_type,
compare_logic['文档'][-1]))
elif license_en == pos_consts.AFC_CONTRACT_EN: # AFC 车辆抵押贷款合同(E-Contract-Non ASP/ASP)
pos_field, value = field_list[0]
if pos_field == 'applicationEntity' and value == 'AFC':
if no_ocr_result:
result_field_list.append((pos_field, value, consts.RESULT_N, ocr_str, img_path, error_type,
compare_logic['文档'][-1]))
elif license_en == pos_consts.HIL_CONTRACT_1_EN: # 售后回租合同(E-Sign-SLB / OC)
pos_field, value = field_list[0]
if pos_field == 'applicationEntity' and value == 'HIL':
if no_ocr_result:
result_field_list.append((pos_field, value, consts.RESULT_N, ocr_str, img_path, error_type,
compare_logic['文档'][-1]))
elif license_en == pos_consts.HIL_CONTRACT_2_EN: # 车辆租赁抵押合同(E-Sign-SLB / OC)
pos_field, value = field_list[0]
if pos_field == 'applicationEntity' and value == 'HIL':
if no_ocr_result:
result_field_list.append(
(pos_field, value, consts.RESULT_N, ocr_str, img_path, error_type,
compare_logic['文档'][-1]))
if not is_find:
for name, value in field_list:
if isinstance(value, list):
value = json.dumps(value, ensure_ascii=False)
no_find_str = consts.DDA_NO_FIND if license_en == consts.DDA_EN else '{0}未找到'.format(license_en)
result_field_list.append(
(name, value, consts.RESULT_N, empty_str, empty_str, ErrorType.NF.value, no_find_str))
# if is_find:
# if special_expiry_date_slice:
# special_section_img_path = section_img_info.get(consts.SECTION_IMG_PATH_KEY_2, '')
# if os.path.exists(special_section_img_path):
# field = 'idExpiryDate'
# special_info = section_img_info.get(consts.ALL_POSITION_KEY_2, {})
# special_section_position = special_info.get(consts.POSITION_KEY, {})
# special_section_angle = special_info.get(consts.ANGLE_KEY, 0)
# try:
# last_img = img_process(special_section_img_path, special_section_position,
# special_section_angle)
# except Exception as e:
# field_img_path_dict[field] = special_section_img_path
# else:
# pre, suf = os.path.splitext(special_section_img_path)
# try:
# res_field = compare_logic[field][0]
# is_valid, coord_tuple = field_build_coordinates(special_info.get(res_field, {}))
# if is_valid:
# save_path = '{0}_{1}{2}'.format(pre, field, suf)
# field_img = last_img[coord_tuple[0]:coord_tuple[1], coord_tuple[2]:coord_tuple[3], :]
# cv2.imwrite(save_path, field_img)
# field_img_path_dict[field] = save_path
# else:
# field_img_path_dict[field] = special_section_img_path
# except Exception as e:
# field_img_path_dict[field] = special_section_img_path
#
# section_img_path = section_img_info.get(consts.SECTION_IMG_PATH_KEY, '')
# if os.path.exists(section_img_path):
# failed_field = []
# base_img_path = empty_str
# for name, _, result, _, img_path, _, _ in result_field_list:
# if result == consts.RESULT_N:
# if special_expiry_date_slice and name == 'idExpiryDate':
# continue
# failed_field.append(name)
# if base_img_path == empty_str:
# base_img_path = img_path
# if len(failed_field) > 0:
# info = section_img_info.get(consts.ALL_POSITION_KEY, {})
# section_position = info.get(consts.POSITION_KEY, {})
# section_angle = info.get(consts.ANGLE_KEY, 0)
# try:
# last_img = img_process(section_img_path, section_position, section_angle)
# except Exception as e:
# for field in failed_field:
# field_img_path_dict[field] = base_img_path
# else:
# pre, suf = os.path.splitext(section_img_path)
# for field in failed_field:
# try:
# res_field = compare_logic[field][0]
# is_valid, coord_tuple = field_build_coordinates(info.get(res_field, {}))
# if is_valid:
# save_path = '{0}_{1}{2}'.format(pre, field, suf)
# field_img = last_img[coord_tuple[0]:coord_tuple[1], coord_tuple[2]:coord_tuple[3],
# :]
# cv2.imwrite(save_path, field_img)
# field_img_path_dict[field] = save_path
# else:
# field_img_path_dict[field] = base_img_path
# except Exception as e:
# field_img_path_dict[field] = base_img_path
return result_field_list, no_ocr_result, field_img_path_dict
class CACompare:
@staticmethod
def ca_compare_license(license_en, ocr_res_dict, field_list):
ocr_field, compare_logic, special_expiry_date = consts.CA_COMPARE_FIELD[license_en]
is_find = False
special_expiry_date_slice = False
result_field_list = []
section_img_info = dict()
field_img_path_dict = dict()
ocr_res_str = ocr_res_dict.get(ocr_field)
if ocr_res_str is not None:
ocr_res_list = json.loads(ocr_res_str)
# 副页去除 3/4页去除
if ocr_field == consts.DL_OCR_FIELD or ocr_field == consts.MVC_OCR_FIELD:
tmp_list = []
for res in ocr_res_list:
if compare_logic['vinNo'][0] in res:
tmp_list.append(res)
ocr_res_list = tmp_list
length = len(ocr_res_list)
# 身份证、居住证 过期期限特殊处理
if special_expiry_date:
expiry_dates = set()
expiry_dates_img_path = set()
key = compare_logic.get('idExpiryDate')[0]
for ocr_res in ocr_res_list:
if key in ocr_res:
expiry_dates.add(ocr_res[key])
expiry_dates_img_path.add(ocr_res.get(consts.IMG_PATH_KEY_2, ''))
else:
expiry_dates = set()
expiry_dates_img_path = set()
for res_idx in range(length - 1, -1, -1):
if is_find:
break
for idx, (name, value) in enumerate(field_list):
ocr_str = ocr_res_list[res_idx].get(compare_logic[name][0])
if not isinstance(ocr_str, str):
result = consts.RESULT_N
ocr_str = empty_str
else:
result = getattr(cp, compare_logic[name][1])(value, ocr_str, **compare_logic[name][2])
if idx == 0 and result == consts.RESULT_N and length > 1:
break
is_find = True
# section_img_info[consts.SECTION_IMG_PATH_KEY] = ocr_res_list[res_idx].get(
# consts.SECTION_IMG_PATH_KEY, '')
# section_img_info[consts.ALL_POSITION_KEY] = ocr_res_list[res_idx].get(consts.ALL_POSITION_KEY, {})
# if special_expiry_date:
# section_img_info[consts.SECTION_IMG_PATH_KEY_2] = ocr_res_list[res_idx].get(
# consts.SECTION_IMG_PATH_KEY_2, '')
# section_img_info[consts.ALL_POSITION_KEY_2] = ocr_res_list[res_idx].get(
# consts.ALL_POSITION_KEY_2, {})
# 过期期限特殊处理
if special_expiry_date and name == 'idExpiryDate' and result == consts.RESULT_N:
for expiry_date in expiry_dates:
expiry_date_res = getattr(cp, compare_logic[name][1])(value, expiry_date,
**compare_logic[name][2])
if expiry_date_res == consts.RESULT_Y:
ocr_str = expiry_date
result = expiry_date_res
break
if result == consts.RESULT_N:
if consts.IMG_PATH_KEY_2 in ocr_res_list[res_idx]:
img_path = ocr_res_list[res_idx].get(consts.IMG_PATH_KEY_2, '')
special_expiry_date_slice = True
else:
img_path = expiry_dates_img_path.pop() if len(expiry_dates_img_path) > 0 else empty_str
else:
img_path = empty_str
else:
img_path = ocr_res_list[res_idx].get(consts.IMG_PATH_KEY,
'') if result == consts.RESULT_N else empty_str
error_type = empty_error_type if result == consts.RESULT_Y else ErrorType.OCR.value
result_field_list.append((name, value, result, ocr_str, img_path, error_type, compare_logic[name][-1]))
if not is_find:
for name, value in field_list:
result_field_list.append((name, value, consts.RESULT_N, empty_str, empty_str, ErrorType.NF.value, compare_logic[name][-1]))
# if is_find:
# if special_expiry_date_slice:
# special_section_img_path = section_img_info.get(consts.SECTION_IMG_PATH_KEY_2, '')
# if os.path.exists(special_section_img_path):
# field = 'idExpiryDate'
# special_info = section_img_info.get(consts.ALL_POSITION_KEY_2, {})
# special_section_position = special_info.get(consts.POSITION_KEY, {})
# special_section_angle = special_info.get(consts.ANGLE_KEY, 0)
# try:
# last_img = img_process(special_section_img_path, special_section_position,
# special_section_angle)
# except Exception as e:
# field_img_path_dict[field] = special_section_img_path
# else:
# pre, suf = os.path.splitext(special_section_img_path)
# try:
# res_field = compare_logic[field][0]
# is_valid, coord_tuple = field_build_coordinates(special_info.get(res_field, {}))
# if is_valid:
# save_path = '{0}_{1}{2}'.format(pre, field, suf)
# field_img = last_img[coord_tuple[0]:coord_tuple[1], coord_tuple[2]:coord_tuple[3], :]
# cv2.imwrite(save_path, field_img)
# field_img_path_dict[field] = save_path
# else:
# field_img_path_dict[field] = special_section_img_path
# except Exception as e:
# field_img_path_dict[field] = special_section_img_path
#
# section_img_path = section_img_info.get(consts.SECTION_IMG_PATH_KEY, '')
# if os.path.exists(section_img_path):
# failed_field = []
# base_img_path = empty_str
# for name, _, result, _, img_path, _ in result_field_list:
# if result == consts.RESULT_N:
# if special_expiry_date_slice and name == 'idExpiryDate':
# continue
# failed_field.append(name)
# if base_img_path == empty_str:
# base_img_path = img_path
# if len(failed_field) > 0:
# info = section_img_info.get(consts.ALL_POSITION_KEY, {})
# section_position = info.get(consts.POSITION_KEY, {})
# section_angle = info.get(consts.ANGLE_KEY, 0)
# try:
# last_img = img_process(section_img_path, section_position, section_angle)
# except Exception as e:
# for field in failed_field:
# field_img_path_dict[field] = base_img_path
# else:
# pre, suf = os.path.splitext(section_img_path)
# for field in failed_field:
# try:
# if license_en == consts.PP_EN:
# res_field = consts.PP_SLICE_MAP[field]
# else:
# res_field = compare_logic[field][0]
# is_valid, coord_tuple = field_build_coordinates(info.get(res_field, {}))
# if is_valid:
# save_path = '{0}_{1}{2}'.format(pre, field, suf)
# field_img = last_img[coord_tuple[0]:coord_tuple[1], coord_tuple[2]:coord_tuple[3],
# :]
# cv2.imwrite(save_path, field_img)
# field_img_path_dict[field] = save_path
# else:
# field_img_path_dict[field] = base_img_path
# except Exception as e:
# field_img_path_dict[field] = base_img_path
return result_field_list, field_img_path_dict