Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
周伟奇
/
bmw-ocr
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
e2dacd53
authored
2021-11-02 17:26:16 +0800
by
周伟奇
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
fix idcard
1 parent
77676d5c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
120 additions
and
22 deletions
src/apps/doc/consts.py
src/apps/doc/management/commands/ocr_process.py
src/celery_compare/tasks.py
src/apps/doc/consts.py
View file @
e2dacd5
...
...
@@ -1418,10 +1418,18 @@ ID_COMPARE_LOGIC = {
}
PP_COMPARE_LOGIC
=
{
'customerName'
:
(
'英文姓名'
,
'se_name_compare'
,
{
'is_passport'
:
True
}),
'idNum'
:
(
'护照号码'
,
'se_common_compare'
,
{}),
'dateOfBirth'
:
(
'出生日期'
,
'se_date_compare'
,
{
'input_replace'
:
''
}),
'idExpiryDate'
:
(
'有效期至'
,
'se_date_compare'
,
{
'input_replace'
:
''
,
'today'
:
True
}),
'customerName'
:
(
'英文姓名'
,
'se_name_compare'
,
{
'is_passport'
:
True
}),
# 1
'idNum'
:
(
'护照号码'
,
'se_common_compare'
,
{}),
# 2
'dateOfBirth'
:
(
'出生日期'
,
'se_date_compare'
,
{
'input_replace'
:
''
}),
# 2
'idExpiryDate'
:
(
'有效期至'
,
'se_date_compare'
,
{
'input_replace'
:
''
,
'today'
:
True
}),
# 2
}
PP_SLICE_MAP
=
{
'customerChineseName'
:
'机读码1'
,
'customerName'
:
'机读码1'
,
'idNum'
:
'机读码2'
,
'dateOfBirth'
:
'机读码2'
,
'idExpiryDate'
:
'机读码2'
,
}
EEP_COMPARE_LOGIC
=
{
...
...
@@ -1582,7 +1590,9 @@ BC_FIELD_ORDER_2 = (('BankName', '发卡行名称'),
IMG_PATH_KEY
=
'uniq_img_path_key'
IMG_PATH_KEY_2
=
'uniq_img_path_key_2'
SECTION_IMG_PATH_KEY
=
'uniq_section_img_path_key'
SECTION_IMG_PATH_KEY_2
=
'uniq_section_img_path_key_2'
ALL_POSITION_KEY
=
'uniq_all_position_key'
ALL_POSITION_KEY_2
=
'uniq_all_position_key_2'
POSITION_KEY
=
'uniq_position_key'
ANGLE_KEY
=
'uniq_angle_key'
FIELD_POSITION_KEY
=
'position'
...
...
src/apps/doc/management/commands/ocr_process.py
View file @
e2dacd5
...
...
@@ -305,18 +305,22 @@ class Command(BaseCommand, LoggerMixin):
if
not
license_data
:
res_list
.
append
((
pno
,
ino
,
part_idx
,
consts
.
RES_SUCCESS_EMPTY
))
return
pre
,
suf
=
os
.
path
.
splitext
(
img_path
)
base64_img
=
license_data
.
pop
(
'base64_img'
,
''
)
is_save
=
True
if
len
(
base64_img
)
>
0
else
False
section_img_path
=
'{0}_{1}{2}'
.
format
(
pre
,
part_idx
,
suf
)
if
is_save
else
img_path
if
is_save
:
try
:
with
open
(
section_img_path
,
"wb"
)
as
fh
:
fh
.
write
(
base64
.
b64decode
(
base64_img
.
encode
()))
except
Exception
as
e
:
self
.
online_log
.
warn
(
'{0} [section img save failed] [img_path={1}]'
' [part_idx={2}]'
.
format
(
self
.
log_base
,
img_path
,
part_idx
))
if
isinstance
(
license_data
,
dict
):
pre
,
suf
=
os
.
path
.
splitext
(
img_path
)
base64_img
=
license_data
.
pop
(
'base64_img'
,
''
)
is_save
=
True
if
len
(
base64_img
)
>
0
else
False
section_img_path
=
'{0}_{1}{2}'
.
format
(
pre
,
part_idx
,
suf
)
if
is_save
else
img_path
if
is_save
:
try
:
with
open
(
section_img_path
,
"wb"
)
as
fh
:
fh
.
write
(
base64
.
b64decode
(
base64_img
.
encode
()))
except
Exception
as
e
:
self
.
online_log
.
warn
(
'{0} [section img save failed] [img_path={1}]'
' [part_idx={2}]'
.
format
(
self
.
log_base
,
img_path
,
part_idx
))
else
:
is_save
=
False
section_img_path
=
img_path
# 保单
if
classify
==
consts
.
INSURANCE_CLASSIFY
:
...
...
@@ -517,12 +521,17 @@ class Command(BaseCommand, LoggerMixin):
}
}
id_card_dict
[
consts
.
ALL_POSITION_KEY
]
=
position_dict
id_card_dict
[
consts
.
SECTION_IMG_PATH_KEY
]
=
section_img_path
if
not
is_info_side
:
id_card_dict
[
consts
.
IMG_PATH_KEY_2
]
=
img_path
id_card_dict
[
consts
.
ALL_POSITION_KEY_2
]
=
position_dict
id_card_dict
[
consts
.
SECTION_IMG_PATH_KEY_2
]
=
section_img_path
else
:
id_card_dict
[
consts
.
ALL_POSITION_KEY
]
=
position_dict
id_card_dict
[
consts
.
IMG_PATH_KEY
]
=
img_path
id_card_dict
[
consts
.
SECTION_IMG_PATH_KEY
]
=
section_img_path
if
is_ic
and
is_save
:
card_type
=
-
1
json_data_4
=
{
...
...
src/celery_compare/tasks.py
View file @
e2dacd5
...
...
@@ -364,6 +364,7 @@ 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
()
...
...
@@ -423,8 +424,11 @@ def ca_compare_license(license_en, ocr_res_dict, field_list):
break
if
result
==
consts
.
RESULT_N
:
next_img_path
=
expiry_dates_img_path
.
pop
()
if
len
(
expiry_dates_img_path
)
>
0
else
empty_str
img_path
=
ocr_res_list
[
res_idx
]
.
get
(
consts
.
IMG_PATH_KEY_2
,
next_img_path
)
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
:
...
...
@@ -437,12 +441,40 @@ def ca_compare_license(license_en, ocr_res_dict, field_list):
result_field_list
.
append
((
name
,
value
,
consts
.
RESULT_N
,
empty_str
,
empty_str
,
ErrorType
.
NF
.
value
))
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
...
...
@@ -459,7 +491,10 @@ def ca_compare_license(license_en, ocr_res_dict, field_list):
pre
,
suf
=
os
.
path
.
splitext
(
section_img_path
)
for
field
in
failed_field
:
try
:
res_field
=
compare_logic
[
field
][
0
]
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
)
...
...
@@ -1386,6 +1421,7 @@ def se_compare_license(license_en, ocr_res_dict, field_list):
is_find
=
False
no_ocr_result
=
False
special_expiry_date_slice
=
False
result_field_list
=
[]
section_img_info
=
dict
()
field_img_path_dict
=
dict
()
...
...
@@ -1397,12 +1433,15 @@ def se_compare_license(license_en, ocr_res_dict, field_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
:
...
...
@@ -1422,6 +1461,9 @@ def se_compare_license(license_en, ocr_res_dict, field_list):
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
,
''
)
# 过期期限特殊处理
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):
result
=
expiry_date_res
break
img_path
=
ocr_res_list
[
res_idx
]
.
get
(
consts
.
IMG_PATH_KEY
,
''
)
if
result
==
consts
.
RESULT_N
else
empty_str
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
))
else
:
...
...
@@ -1443,12 +1494,40 @@ def se_compare_license(license_en, ocr_res_dict, field_list):
result_field_list
.
append
((
name
,
value
,
consts
.
RESULT_N
,
empty_str
,
empty_str
,
ErrorType
.
NF
.
value
))
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
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment