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
3a0e24cf
authored
2024-10-29 15:01:16 +0800
by
chenyao
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
ocr_process添加try-except处理在后半部分
1 parent
98e8884c
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
65 additions
and
0 deletions
src/apps/doc/management/commands/ocr_process.py
src/apps/doc/management/commands/ocr_process.py
View file @
3a0e24c
...
...
@@ -910,13 +910,20 @@ class Command(BaseCommand, LoggerMixin):
return
date_res
def
get_validate_date
(
self
,
date_list
):
# 添加 try-except 处理
try
:
for
date_str
in
date_list
:
for
format_str
in
consts
.
DATE_FORMAT
:
date_res
=
self
.
date_format
(
date_str
,
format_str
)
if
isinstance
(
date_res
,
date
):
return
date_res
except
Exception
as
e
:
self
.
online_log
.
error
(
'{0} [get_validate_date error] [error={1}]'
.
format
(
self
.
log_base
,
traceback
.
format_exc
()))
return
None
def
merge_card
(
self
,
bs_summary
):
# 添加 try-except 处理
try
:
classify_info
=
{}
merged_bs_summary
=
{}
sorted_card
=
sorted
(
bs_summary
.
keys
(),
key
=
lambda
x
:
bs_summary
[
x
][
'count'
],
reverse
=
True
)
...
...
@@ -948,15 +955,25 @@ class Command(BaseCommand, LoggerMixin):
merged_bs_summary
[
main_card
][
'role'
]
=
self
.
get_most
(
merged_bs_summary
[
main_card
][
'role'
])
del
bs_summary
return
merged_bs_summary
,
classify_info
except
Exception
as
e
:
self
.
online_log
.
error
(
'{0} [merge_card error] [error={1}]'
.
format
(
self
.
log_base
,
traceback
.
format_exc
()))
return
{},
{}
def
prune_bs_summary
(
self
,
bs_summary
):
# 添加 try-except 处理
try
:
for
summary
in
bs_summary
.
values
():
del
summary
[
'count'
]
summary
[
'classify'
]
=
self
.
get_most
(
summary
[
'classify'
])
summary
[
'role'
]
=
self
.
get_most
(
summary
[
'role'
])
return
bs_summary
except
Exception
as
e
:
self
.
online_log
.
error
(
'{0} [prune_bs_summary error] [error={1}]'
.
format
(
self
.
log_base
,
traceback
.
format_exc
()))
return
bs_summary
def
rebuild_bs
(
self
,
bs_summary
):
# 添加 try-except 处理
try
:
# bs_summary = {
# '卡号': {
# 'classify': 0,
...
...
@@ -989,8 +1006,13 @@ class Command(BaseCommand, LoggerMixin):
}
)
return
res
except
Exception
as
e
:
self
.
online_log
.
error
(
'{0} [rebuild_bs error] [error={1}]'
.
format
(
self
.
log_base
,
traceback
.
format_exc
()))
return
[]
def
rebuild_contract
(
self
,
license_summary
,
contract_result_compare
):
# 添加 try-except 处理
try
:
for
classify
,
page_info_dict
in
contract_result_compare
.
items
():
if
classify
==
consts
.
CONTRACT_CLASSIFY
:
res
=
{}
...
...
@@ -1090,8 +1112,12 @@ class Command(BaseCommand, LoggerMixin):
res
[
key
]
=
page_info_dict
.
get
(
str
(
pno1
),
{})
.
get
(
key1
)
res
.
setdefault
(
consts
.
IMG_PATH_KEY
,
dict
())[
key
]
=
page_info_dict
.
get
(
str
(
pno1
),
{})
.
get
(
consts
.
IMG_PATH_KEY
,
''
)
license_summary
[
classify
]
=
[
res
]
except
Exception
as
e
:
self
.
online_log
.
error
(
'{0} [rebuild_contract error] [error={1}]'
.
format
(
self
.
log_base
,
traceback
.
format_exc
()))
def
rebuild_bs_summary
(
self
,
bs_summary
,
unknown_summary
):
# 添加 try-except 处理
try
:
# bs_summary = {
# '卡号': {
# 'count': 100,
...
...
@@ -1195,8 +1221,13 @@ class Command(BaseCommand, LoggerMixin):
summary
[
'end_date'
]
=
self
.
get_validate_date
(
summary
[
'end_date'
])
# summary['confidence'] = max(summary['confidence'])
return
merged_bs_summary
except
Exception
as
e
:
self
.
online_log
.
error
(
'{0} [rebuild_bs_summary error] [error={1}]'
.
format
(
self
.
log_base
,
traceback
.
format_exc
()))
return
{}
def
zip_2_pdfs
(
self
,
zip_task_queue
,
error_list
):
# 添加 try-except 处理
try
:
while
len
(
error_list
)
==
0
:
# 1. 从redis队列中读取任务: AFC_111_0
task_str
=
rh
.
dequeue_zip
()
...
...
@@ -1324,8 +1355,14 @@ class Command(BaseCommand, LoggerMixin):
except
Exception
as
e
:
self
.
online_log
.
error
(
'{0} [zip_2_pdfs] [process error (db save)] [task={1}] [error={2}]'
.
format
(
self
.
log_base
,
task_str
,
traceback
.
format_exc
()))
except
Exception
as
e
:
self
.
online_log
.
error
(
'{0} [process error (zip_2_pdfs out)] [error={1}]'
.
format
(
self
.
log_base
,
traceback
.
format_exc
()))
# 本应该在 error_list 中添加内容, 但是这里没有添加, 使得进程可以一直运行
# error_list.append(1)
def
pdf_2_img_2_queue
(
self
,
img_queue
,
todo_count_dict
,
lock
,
error_list
,
res_dict
,
finish_queue
,
zip_task_queue
):
# 添加 try-except 处理
try
:
while
self
.
switch
:
try
:
task_str
=
zip_task_queue
.
get
(
block
=
False
)
...
...
@@ -1653,8 +1690,14 @@ class Command(BaseCommand, LoggerMixin):
self
.
e_log_base
,
traceback
.
format_exc
()))
error_list
.
append
(
1
)
return
except
Exception
as
e
:
self
.
online_log
.
error
(
'{0} [process error (pdf_2_img_2_queue out)] [error={1}]'
.
format
(
self
.
log_base
,
traceback
.
format_exc
()))
# 本应该在 error_list 中添加内容, 但是这里没有添加, 使得进程可以一直运行
# error_list.append(1)
def
img_2_ocr_1
(
self
,
img_queue
,
todo_count_dict
,
res_dict
,
finish_queue
,
lock
,
url
,
error_list
):
# 添加 try-except 处理
try
:
while
len
(
error_list
)
==
0
or
not
img_queue
.
empty
():
try
:
channel
,
img_path
,
text_list
=
img_queue
.
get
(
block
=
False
)
...
...
@@ -1730,8 +1773,14 @@ class Command(BaseCommand, LoggerMixin):
except
Exception
as
e
:
self
.
online_log
.
error
(
'{0} [process error (store ocr res)] [img_path={1}] [error={2}]'
.
format
(
self
.
log_base
,
img_path
,
traceback
.
format_exc
()))
except
Exception
as
e
:
self
.
online_log
.
error
(
'{0} [process error (img_2_ocr_1 out)] [error={1}]'
.
format
(
self
.
log_base
,
traceback
.
format_exc
()))
# 本应该在 error_list 中添加内容, 但是这里没有添加, 使得进程可以一直运行
# error_list.append(1
def
res_2_wb
(
self
,
res_dict
,
img_queue
,
finish_queue
,
error_list
):
# 添加 try-except 处理
try
:
self
.
online_log
.
info
(
'{0} [res_2_wb] [get task] [queue running] [finish_queue_size={1}]'
.
format
(
self
.
log_base
,
finish_queue
.
qsize
()))
while
len
(
error_list
)
==
0
or
not
img_queue
.
empty
()
or
not
finish_queue
.
empty
():
try
:
...
...
@@ -2501,8 +2550,14 @@ class Command(BaseCommand, LoggerMixin):
self
.
online_log
.
error
(
'{0} [process error (pdf & img remove)] [task={1}] [error={2}]'
.
format
(
self
.
log_base
,
task_str
,
traceback
.
format_exc
()))
self
.
online_log
.
info
(
'{0} [res_2_wb after while] [len(error_list)={1}] [img_queue={2}] [finish_queue={3}]'
.
format
(
self
.
log_base
,
len
(
error_list
),
img_queue
.
empty
(),
finish_queue
.
empty
()))
except
Exception
as
e
:
self
.
online_log
.
error
(
'{0} [res_2_wb failed] [error={1}]'
.
format
(
self
.
log_base
,
traceback
.
format_exc
()))
# 本应该在 error_list 中添加内容, 但是这里没有添加, 使得进程可以一直运行
# error_list.append(1)
def
handle
(
self
,
*
args
,
**
kwargs
):
# 添加 try-except 处理
try
:
db
.
close_old_connections
()
lock
=
Lock
()
with
Manager
()
as
manager
:
...
...
@@ -2537,9 +2592,13 @@ class Command(BaseCommand, LoggerMixin):
p
.
join
()
self
.
online_log
.
info
(
'{0} [stop safely]'
.
format
(
self
.
log_base
))
except
Exception
as
e
:
self
.
online_log
.
error
(
'{0} [stop failed] [error={1}]'
.
format
(
self
.
log_base
,
traceback
.
format_exc
()))
@transaction.atomic
def
atomicSaveDBAFC
(
self
,
result_class
,
doc
,
license_summary
,
ic_merge
,
rp_merge
,
task_str
,
financial_statement_dict
,
financial_explanation_dict
):
# 添加 try-except 处理
try
:
with
transaction
.
atomic
(
'afc'
):
res_obj
=
result_class
.
objects
.
using
(
'afc'
)
.
select_for_update
()
.
filter
(
application_id
=
doc
.
application_id
)
.
first
()
self
.
online_log
.
info
(
'{0} [sql lock AFC application_id={1} doc_id={2}]'
.
format
(
self
.
log_base
,
doc
.
application_id
,
doc
.
id
))
...
...
@@ -2578,9 +2637,13 @@ def atomicSaveDBAFC(self,result_class,doc,license_summary,ic_merge,rp_merge,task
res_obj
.
save
()
self
.
online_log
.
info
(
'{0} [sql lock release application_id={1} doc_id={2}]'
.
format
(
self
.
log_base
,
doc
.
application_id
,
doc
.
id
))
return
res_obj
except
Exception
as
e
:
self
.
online_log
.
error
(
'{0} [process error (afc db save)] [task={1}] [error={2}]'
.
format
(
self
.
log_base
,
task_str
,
traceback
.
format_exc
()))
@transaction.atomic
def
atomicSaveDBHIL
(
self
,
result_class
,
doc
,
license_summary
,
ic_merge
,
rp_merge
,
task_str
,
financial_statement_dict
,
financial_explanation_dict
):
# 添加 try-except 处理
try
:
with
transaction
.
atomic
(
'default'
):
res_obj
=
result_class
.
objects
.
using
(
'default'
)
.
select_for_update
()
.
filter
(
application_id
=
doc
.
application_id
)
.
first
()
self
.
online_log
.
info
(
'{0} [sql lock HIL application_id={1} doc_id={2}]'
.
format
(
self
.
log_base
,
doc
.
application_id
,
doc
.
id
))
...
...
@@ -2619,3 +2682,5 @@ def atomicSaveDBHIL(self,result_class,doc,license_summary,ic_merge,rp_merge, tas
res_obj
.
save
()
self
.
online_log
.
info
(
'{0} [sql lock release application_id={1} doc_id={2}]'
.
format
(
self
.
log_base
,
doc
.
application_id
,
doc
.
id
))
return
res_obj
except
Exception
as
e
:
self
.
online_log
.
error
(
'{0} [process error (hil db save)] [task={1}] [error={2}]'
.
format
(
self
.
log_base
,
task_str
,
traceback
.
format_exc
()))
...
...
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