Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
eshiled-new
/
my_flask_web
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit fe907e4c
authored
Feb 23, 2024
by
周杭
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
ocr功能
1 parent
c657c2e9
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
1180 additions
and
417 deletions
Identify_sn/main04.py
Identify_vinandsn/main04.py
config_dev.py
config_test.py
flaskr/controller/ocr_controller.py
kafka_consumer/orc_consumer.py
Identify_sn/logger.py → logger.py
manage.py
Identify_sn/main04.py
View file @
fe907e4
...
@@ -179,30 +179,56 @@ def extract_vin(string):
...
@@ -179,30 +179,56 @@ def extract_vin(string):
else
:
else
:
return
None
return
None
def
flip_image
_45
(
imag
e
):
def
flip_image
(
image
,
angl
e
):
# 逆时针旋转45度
# 逆时针旋转45度
log
.
info
(
"逆时针旋转
45
度"
)
log
.
info
(
"逆时针旋转
"
+
angle
+
"
度"
)
# rotated_image = cv2.transpose(image)
# rotated_image = cv2.transpose(image)
# rotated_image = cv2.flip(rotated_image, 0)
# rotated_image = cv2.flip(rotated_image, 0)
rotated_image
=
image
.
rotate
(
45
)
rotated_image
=
image
.
rotate
(
angle
)
return
rotated_image
return
rotated_image
def
flip_image_90
(
image
):
def
handler_sn
(
dst
,
flag
,
angle
,
path
):
# 逆时针旋转90度
# 旋转角度
log
.
info
(
"逆时针旋转90度"
)
if
angle
>
0
:
# rotated_image = cv2.transpose(image)
dst
=
flip_image
(
dst
,
angle
)
# rotated_image = cv2.flip(rotated_image, 0)
try
:
rotated_image
=
image
.
rotate
(
90
)
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
return
rotated_image
except
Exception
as
msg
:
log
.
error
(
msg
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
log
.
info
(
line
)
# log.info(line[0][1][0])
def
flip_image_180
(
image
):
result_list
=
flatten_list
(
result
)
# 旋转180度
log
.
info
(
"result_list
%
s"
,
result_list
)
log
.
info
(
"逆时针旋转180度"
)
with
open
(
"./log01.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
rotated_image
=
image
.
rotate
(
180
)
if
result_list
is
not
None
:
return
rotated_image
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
# 使用正则表达式进行过滤 获取车架号
for
item
in
result_list
:
log
.
info
(
item
)
sn_check
=
extract_sn
(
item
)
if
sn_check
is
not
None
:
flag
=
False
sn_str
=
sn_check
if
sn_str
:
log
.
info
(
str
(
row
))
log
.
info
(
sn_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_vin_check_ocr_sn_01 (order_id,file_id,db_sn,ocr_sn,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}','{sn_str}',sysdate())
"""
# 执行SQL语句
log
.
info
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
return
flag
# 下载图片数据
# 下载图片数据
for
index
,
row
in
enumerate
(
result
):
for
index
,
row
in
enumerate
(
result
):
...
@@ -245,165 +271,16 @@ for index, row in enumerate(result):
...
@@ -245,165 +271,16 @@ for index, row in enumerate(result):
(
original_image
.
width
*
scale_factor
,
original_image
.
height
*
scale_factor
)
(
original_image
.
width
*
scale_factor
,
original_image
.
height
*
scale_factor
)
)
)
try
:
flag
=
True
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
except
Exception
as
msg
:
log
.
error
(
msg
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
log
.
info
(
line
)
# log.info(line[0][1][0])
result_list
=
flatten_list
(
result
)
flag
=
handler_sn
(
dst
,
flag
,
0
,
path
,
row
)
log
.
info
(
"result_list
%
s"
,
result_list
)
with
open
(
"./log01.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
# 使用正则表达式进行过滤 获取车架号
sn_str
=
""
flag
=
True
for
item
in
result_list
:
log
.
info
(
item
)
sn_check
=
extract_sn
(
item
)
if
sn_check
is
not
None
:
flag
=
False
sn_str
=
sn_check
if
sn_str
:
log
.
info
(
str
(
row
))
log
.
info
(
sn_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_vin_check_ocr_sn_01 (order_id,file_id,db_sn,ocr_sn,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}','{sn_str}',sysdate())
"""
# 执行SQL语句
log
.
info
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
if
flag
:
if
flag
:
# 旋转45
flag
=
handler_sn
(
dst
,
flag
,
45
,
path
,
row
)
dst
=
flip_image_45
(
dst
)
try
:
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
except
Exception
as
msg
:
log
.
error
(
msg
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
log
.
info
(
line
)
# log.info(line[0][1][0])
result_list
=
flatten_list
(
result
)
log
.
info
(
"result_list
%
s"
,
result_list
)
with
open
(
"./log01.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
# 使用正则表达式进行过滤 获取车架号
sn_str
=
""
for
item
in
result_list
:
log
.
info
(
item
)
sn_check
=
extract_sn
(
item
)
if
sn_check
is
not
None
:
flag
=
False
sn_str
=
sn_check
if
sn_str
:
log
.
info
(
str
(
row
))
log
.
info
(
sn_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_vin_check_ocr_sn_01 (order_id,file_id,db_sn,ocr_sn,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}','{sn_str}',sysdate())
"""
# 执行SQL语句
log
.
info
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
if
flag
:
if
flag
:
# 旋转90
flag
=
handler_sn
(
dst
,
flag
,
45
,
path
,
row
)
dst
=
flip_image_45
(
dst
)
try
:
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
except
Exception
as
msg
:
log
.
error
(
msg
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
log
.
info
(
line
)
# log.info(line[0][1][0])
result_list
=
flatten_list
(
result
)
log
.
info
(
"result_list
%
s"
,
result_list
)
with
open
(
"./log01.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
# 使用正则表达式进行过滤 获取车架号
sn_str
=
""
for
item
in
result_list
:
log
.
info
(
item
)
sn_check
=
extract_sn
(
item
)
if
sn_check
is
not
None
:
flag
=
False
sn_str
=
sn_check
if
sn_str
:
log
.
info
(
str
(
row
))
log
.
info
(
sn_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_vin_check_ocr_sn_01 (order_id,file_id,db_sn,ocr_sn,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}','{sn_str}',sysdate())
"""
# 执行SQL语句
log
.
info
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
if
flag
:
if
flag
:
# 旋转180
flag
=
handler_sn
(
dst
,
flag
,
90
,
path
,
row
)
dst
=
flip_image_90
(
dst
)
try
:
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
except
Exception
as
msg
:
log
.
error
(
msg
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
log
.
info
(
line
)
# log.info(line[0][1][0])
result_list
=
flatten_list
(
result
)
log
.
info
(
"result_list
%
s"
,
result_list
)
with
open
(
"./log01.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
# 使用正则表达式进行过滤 获取车架号
sn_str
=
""
for
item
in
result_list
:
log
.
info
(
item
)
sn_check
=
extract_sn
(
item
)
if
sn_check
is
not
None
:
flag
=
False
sn_str
=
sn_check
if
sn_str
:
log
.
info
(
str
(
row
))
log
.
info
(
sn_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_vin_check_ocr_sn_01 (order_id,file_id,db_sn,ocr_sn,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}','{sn_str}',sysdate())
"""
# 执行SQL语句
log
.
info
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
if
flag
:
if
flag
:
scale_factor
=
5
scale_factor
=
5
dst
=
original_image
.
resize
(
dst
=
original_image
.
resize
(
...
@@ -412,45 +289,8 @@ for index, row in enumerate(result):
...
@@ -412,45 +289,8 @@ for index, row in enumerate(result):
original_image
.
height
*
scale_factor
,
original_image
.
height
*
scale_factor
,
)
)
)
)
flag
=
handler_sn
(
dst
,
flag
,
0
,
path
,
row
)
try
:
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
except
Exception
as
msg
:
log
.
error
(
msg
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
log
.
info
(
line
)
# log.info(line[0][1][0])
result_list
=
flatten_list
(
result
)
log
.
info
(
"result_list
%
s"
,
result_list
)
with
open
(
"./log01.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
# 使用正则表达式进行过滤 获取车架号
sn_str
=
""
for
item
in
result_list
:
log
.
info
(
item
)
sn_check
=
extract_sn
(
item
)
if
sn_check
is
not
None
:
flag
=
False
sn_str
=
sn_check
if
sn_str
:
log
.
info
(
str
(
row
))
log
.
info
(
sn_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_vin_check_ocr_sn_01 (order_id,file_id,db_sn,ocr_sn,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}','{sn_str}',sysdate())
"""
# 执行SQL语句
log
.
info
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
if
flag
:
if
flag
:
# 定义要执行的SQL语句
# 定义要执行的SQL语句
SQL02
=
f
"""
SQL02
=
f
"""
...
...
Identify_vinandsn/main04.py
View file @
fe907e4
...
@@ -14,7 +14,7 @@ from paddleocr import PaddleOCR, draw_ocr
...
@@ -14,7 +14,7 @@ from paddleocr import PaddleOCR, draw_ocr
from
PIL
import
Image
from
PIL
import
Image
from
PIL
import
ImageEnhance
from
PIL
import
ImageEnhance
# from pyzbar.pyzbar import decode
# from pyzbar.pyzbar import decode
from
logger
import
log
# 连接database
# 连接database
conn_dev
=
pymysql
.
connect
(
conn_dev
=
pymysql
.
connect
(
host
=
"192.168.3.227"
,
host
=
"192.168.3.227"
,
...
@@ -157,29 +157,60 @@ def extract_vin(string):
...
@@ -157,29 +157,60 @@ def extract_vin(string):
return
None
return
None
def
flip_image
_45
(
imag
e
):
def
flip_image
(
image
,
angl
e
):
# 逆时针旋转45度
# 逆时针旋转45度
print
(
"逆时针旋转45
度"
)
log
.
info
(
"逆时针旋转"
+
angle
+
"
度"
)
# rotated_image = cv2.transpose(image)
# rotated_image = cv2.transpose(image)
# rotated_image = cv2.flip(rotated_image, 0)
# rotated_image = cv2.flip(rotated_image, 0)
rotated_image
=
image
.
rotate
(
45
)
rotated_image
=
image
.
rotate
(
angle
)
return
rotated_image
return
rotated_image
def
flip_image_90
(
image
):
def
handler_vin
(
dst
,
flag
,
angle
,
path
):
# 逆时针旋转90度
if
angle
>
0
:
print
(
"逆时针旋转90度"
)
dst
=
flip_image
(
dst
,
angle
)
# rotated_image = cv2.transpose(image)
# rotated_image = cv2.flip(rotated_image, 0)
try
:
rotated_image
=
image
.
rotate
(
90
)
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
return
rotated_image
except
Exception
as
msg
:
log
.
error
(
msg
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
print
(
line
)
# print(line[0][1][0])
def
flip_image_180
(
image
):
result_list
=
flatten_list
(
result
)
# 旋转180度
print
(
"result_list"
,
result_list
)
rotated_image
=
cv2
.
flip
(
image
,
-
1
)
with
open
(
"./log10.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
return
rotated_image
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
# 使用正则表达式进行过滤 获取车架号
vin_str
=
""
for
item
in
result_list
:
print
(
item
)
vin_check
=
extract_vin
(
item
)
if
vin_check
is
not
None
:
vin_str
=
vin_check
flag
=
False
break
if
vin_str
:
print
(
str
(
row
))
print
(
vin_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_vin_check_ocr_11 (order_id,file_id,db_vin,ocr_vin,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}','{vin_str}',sysdate())
"""
# 执行SQL语句
print
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
return
flag
# 下载图片数据
# 下载图片数据
for
index
,
row
in
enumerate
(
result
):
for
index
,
row
in
enumerate
(
result
):
...
@@ -225,196 +256,38 @@ for index, row in enumerate(result):
...
@@ -225,196 +256,38 @@ for index, row in enumerate(result):
dst
=
original_image
.
resize
(
dst
=
original_image
.
resize
(
(
original_image
.
width
*
scale_factor
,
original_image
.
height
*
scale_factor
)
(
original_image
.
width
*
scale_factor
,
original_image
.
height
*
scale_factor
)
)
)
flag
=
True
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
flag
=
handler_vin
(
dst
,
flag
,
0
,
path
,
row
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
if
flag
:
for
line
in
result
:
# 旋转45度宠重新识别
print
(
line
)
flag
=
handler_vin
(
dst
,
flag
,
45
,
path
,
row
)
# print(line[0][1][0])
if
flag
:
# 旋转90度宠重新识别
result_list
=
flatten_list
(
result
)
flag
=
handler_vin
(
dst
,
flag
,
45
,
path
,
row
)
print
(
"result_list"
,
result_list
)
if
flag
:
with
open
(
"./log10.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
# 旋转180度宠重新识别
if
result_list
is
not
None
:
flag
=
handler_vin
(
dst
,
flag
,
90
,
path
,
row
)
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
if
flag
:
else
:
# 如果识别不了 直接再放大图片
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
scale_factor
=
5
dst
=
original_image
.
resize
(
# 使用正则表达式进行过滤 获取车架号
(
vin_str
=
""
original_image
.
width
*
scale_factor
,
for
item
in
result_list
:
original_image
.
height
*
scale_factor
,
print
(
item
)
)
vin_check
=
extract_vin
(
item
)
)
if
vin_check
is
not
None
:
flag
=
handler_vin
(
dst
,
flag
,
0
,
path
,
row
)
vin_str
=
vin_check
if
flag
:
break
if
vin_str
:
print
(
str
(
row
))
print
(
vin_str
)
# 定义要执行的SQL语句
# 定义要执行的SQL语句
SQL0
1
=
f
"""
SQL0
2
=
f
"""
INSERT INTO sys_vin_check_ocr_11 (order_id,file_id,db_vin,ocr_vin,create_time)
INSERT INTO sys_vin_check_ocr_11 (order_id,file_id,db_vin,ocr_vin,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}',
'{vin_str}'
,sysdate())
VALUES ({row[1]},{row[0]},'{row[9]}',
null
,sysdate())
"""
"""
# 执行SQL语句
# 执行SQL语句
print
(
SQL0
1
)
print
(
SQL0
2
)
cursor_dev
.
execute
(
SQL0
1
)
cursor_dev
.
execute
(
SQL0
2
)
conn_dev
.
commit
()
conn_dev
.
commit
()
else
:
# 旋转90度宠重新识别
dst
=
flip_image_45
(
dst
)
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
print
(
line
)
# print(line[0][1][0])
result_list
=
flatten_list
(
result
)
print
(
"result_list"
,
result_list
)
with
open
(
"./log10.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
vin_str
=
""
for
item
in
result_list
:
print
(
item
)
vin_check
=
extract_vin
(
item
)
if
vin_check
is
not
None
:
vin_str
=
vin_check
break
if
vin_str
:
print
(
str
(
row
))
print
(
vin_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_vin_check_ocr_11 (order_id,file_id,db_vin,ocr_vin,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}','{vin_str}',sysdate())
"""
# 执行SQL语句
print
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
else
:
dst
=
flip_image_45
(
dst
)
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
print
(
line
)
# print(line[0][1][0])
result_list
=
flatten_list
(
result
)
print
(
"result_list"
,
result_list
)
with
open
(
"./log10.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
vin_str
=
""
for
item
in
result_list
:
print
(
item
)
vin_check
=
extract_vin
(
item
)
if
vin_check
is
not
None
:
vin_str
=
vin_check
break
if
vin_str
:
print
(
str
(
row
))
print
(
vin_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_vin_check_ocr_11 (order_id,file_id,db_vin,ocr_vin,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}','{vin_str}',sysdate())
"""
# 执行SQL语句
print
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
else
:
dst
=
flip_image_90
(
dst
)
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
print
(
line
)
# print(line[0][1][0])
result_list
=
flatten_list
(
result
)
print
(
"result_list"
,
result_list
)
with
open
(
"./log10.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
vin_str
=
""
for
item
in
result_list
:
print
(
item
)
vin_check
=
extract_vin
(
item
)
if
vin_check
is
not
None
:
vin_str
=
vin_check
break
if
vin_str
:
print
(
str
(
row
))
print
(
vin_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_vin_check_ocr_11 (order_id,file_id,db_vin,ocr_vin,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}','{vin_str}',sysdate())
"""
# 执行SQL语句
print
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
else
:
# 如果识别不了 直接再放大图片
scale_factor
=
5
dst
=
original_image
.
resize
(
(
original_image
.
width
*
scale_factor
,
original_image
.
height
*
scale_factor
,
)
)
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
print
(
line
)
# print(line[0][1][0])
result_list
=
flatten_list
(
result
)
print
(
"result_list"
,
result_list
)
with
open
(
"./log10.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
vin_str
=
""
for
item
in
result_list
:
print
(
item
)
vin_check
=
extract_vin
(
item
)
if
vin_check
is
not
None
:
vin_str
=
vin_check
break
if
vin_str
:
print
(
str
(
row
))
print
(
vin_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_vin_check_ocr_11 (order_id,file_id,db_vin,ocr_vin,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}','{vin_str}',sysdate())
"""
# 执行SQL语句
print
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
else
:
# 定义要执行的SQL语句
SQL02
=
f
"""
INSERT INTO sys_vin_check_ocr_11 (order_id,file_id,db_vin,ocr_vin,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}',null,sysdate())
"""
# 执行SQL语句
print
(
SQL02
)
cursor_dev
.
execute
(
SQL02
)
conn_dev
.
commit
()
# # 关闭光标对象
# # 关闭光标对象
...
...
config_dev.py
View file @
fe907e4
...
@@ -8,9 +8,9 @@ class Config:
...
@@ -8,9 +8,9 @@ class Config:
password
=
'df234fl'
password
=
'df234fl'
database
=
'glsx_car_service'
database
=
'glsx_car_service'
# Kafka consumer配置
# Kafka consumer配置
kafka_consumer_bootstrap_servers
=
'
localhost
:9092'
kafka_consumer_bootstrap_servers
=
'
192.168.3.131
:9092'
kafka_consumer_group_id
=
'
my_consumer_group
'
kafka_consumer_group_id
=
'
ocr_consumer_dev
'
kafka_consumer_auto_offset_reset
=
'earliest'
kafka_consumer_auto_offset_reset
=
'earliest'
kafka_consumer_subscribe
=
[
'
my
_topic'
]
kafka_consumer_subscribe
=
[
'
ocr
_topic'
]
charset
=
'utf8'
charset
=
'utf8'
#
#
config_test.py
View file @
fe907e4
...
@@ -8,9 +8,9 @@ class Config:
...
@@ -8,9 +8,9 @@ class Config:
password
=
'Msd^*$@online'
password
=
'Msd^*$@online'
database
=
'glsx_car_service'
database
=
'glsx_car_service'
# Kafka consumer配置
# Kafka consumer配置
kafka_consumer_bootstrap_servers
=
'
localhost
:9092'
kafka_consumer_bootstrap_servers
=
'
192.168.3.131
:9092'
kafka_consumer_group_id
=
'
my_consumer_group
'
kafka_consumer_group_id
=
'
ocr_consumer_test
'
kafka_consumer_auto_offset_reset
=
'earliest'
kafka_consumer_auto_offset_reset
=
'earliest'
kafka_consumer_subscribe
=
[
'
my
_topic'
]
kafka_consumer_subscribe
=
[
'
ocr
_topic'
]
charset
=
'utf8'
charset
=
'utf8'
#
#
flaskr/controller/ocr_controller.py
0 → 100644
View file @
fe907e4
import
os
from
flask
import
request
,
Blueprint
,
json
import
random
import
re
import
time
import
pymysql
from
paddleocr
import
PaddleOCR
,
draw_ocr
from
PIL
import
Image
import
requests
import
numpy
as
np
# 创建一个蓝图
ocr_classfication_bp
=
Blueprint
(
"ocr_classfication"
,
__name__
)
conn_dev
=
pymysql
.
connect
(
host
=
"192.168.3.227"
,
port
=
3306
,
user
=
"dev_user"
,
password
=
"df234fl"
,
database
=
"glsx_car_service"
,
charset
=
"utf8"
,
)
# 得到一个可以执行SQL语句的光标对象
cursor_dev
=
conn_dev
.
cursor
()
# load model
# Paddleocr目前支持中英文、英文、法语、德语、韩语、日语,可以通过修改 lang参数进行切换
# lang参数依次为`ch`, `en`, `french`, `german`, `korean`, `japan`
ocr
=
PaddleOCR
(
use_angle_cls
=
True
,
# use_angle_cls=False,
lang
=
"ch"
,
# use_gpu=False,
use_gpu
=
True
,
use_tensorrt
=
True
,
det_algorithm
=
"DB"
,
# det_algorithm="EAST",
det_db_score_mode
=
"slow"
,
# use_space_char=False, #不可用
det_model_dir
=
"ch_ppocr_server_v4.0_det_infer/"
,
cls_model_dir
=
"ch_ppocr_mobile_v4.0_cls_infer/"
,
rec_model_dir
=
"ch_ppocr_server_v4.0_rec_infer/"
,
det_db_thresh
=
0.1
,
det_db_box_thresh
=
0.1
,
det_db_unclip_ratio
=
2.0
,
max_batch_size
=
20
,
use_dilation
=
True
,
# rec_algorithm="SVTR_LCNet",
)
# 定义要执行的SQL语句
# SQL = """
# select * from sys_order_file_ocr_sn sofon limit 5
# """
# # 执行SQL语句
# cursor_dev.execute(SQL)
#
# # 获取查询结果
# result = cursor_dev.fetchall()
def
generate_random_filename
():
timestamp
=
int
(
time
.
time
())
# 获取当前时间戳
random_number
=
random
.
randint
(
1000
,
9999
)
# 生成一个随机数
filename
=
f
"file_{timestamp}_{random_number}"
# 创建文件名
return
filename
def
get_extension
(
filename
):
parts
=
filename
.
split
(
"."
)
extension
=
parts
[
-
1
]
return
extension
def
get_project_root
():
# 获取当前脚本文件的路径
current_file
=
os
.
path
.
abspath
(
__file__
)
# 获取当前项目的根目录
project_root
=
os
.
path
.
dirname
(
os
.
path
.
dirname
(
current_file
))
print
(
project_root
)
return
project_root
def
flatten_list
(
nested_list
):
flattened_list
=
[]
for
item
in
nested_list
:
if
isinstance
(
item
,
list
):
flattened_list
.
extend
(
flatten_list
(
item
))
elif
isinstance
(
item
,
tuple
):
for
value
in
item
:
if
type
(
value
)
in
(
int
,
float
):
if
value
>
1.0
:
flattened_list
.
append
(
value
)
else
:
flattened_list
.
append
(
value
)
else
:
# flattened_list.append(item)
pass
return
flattened_list
def
validate_vin
(
item
):
pattern
=
r"^(?=.*[0-9A-HJ-NPR-Z])(?!(.*[IOQ]))([0-9A-HJ-NPR-Z]{17})$"
# pattern 是匹配车牌号的正则表达式
match
=
re
.
match
(
pattern
,
str
(
item
))
if
match
:
return
True
else
:
return
False
def
extract_sn
(
string
):
string
=
re
.
sub
(
r"O"
,
"0"
,
string
)
string
=
re
.
sub
(
r"I"
,
"1"
,
string
)
pattern
=
r"\b(\d{10,11})\b"
match
=
re
.
search
(
pattern
,
string
)
if
match
:
sn
=
match
.
group
()
return
sn
else
:
return
None
def
extract_vin
(
string
):
string
=
re
.
sub
(
r"O"
,
"0"
,
string
)
string
=
re
.
sub
(
r"I"
,
"1"
,
string
)
pattern
=
r"(?=.*[0-9A-HJ-NPR-Z])(?!(.*[IOQ]))([0-9A-HJ-NPR-Z]{17})"
# pattern = r"(?=.*[0-9A-HJ-NPR-Z])(?!(.*[IOQ]))([0-9A-HJ-NPR-Z\s\u4e00-\u9fa5]{17})"
# pattern = r"[0-9A-HJ-NPR-Z]{17}"
# pattern = r"^[A-HJ-NPR-Z\d]{17}$"
match
=
re
.
search
(
pattern
,
string
)
if
match
:
vin
=
match
.
group
()
pattern01
=
r"^\d+$"
match
=
re
.
match
(
pattern01
,
vin
)
# 如果全是数字 则不是车架号
if
bool
(
match
):
return
None
else
:
return
vin
else
:
return
None
def
flip_image_45
(
image
):
# 逆时针旋转45度
print
(
"逆时针旋转45度"
)
# rotated_image = cv2.transpose(image)
# rotated_image = cv2.flip(rotated_image, 0)
rotated_image
=
image
.
rotate
(
45
)
return
rotated_image
def
flip_image_90
(
image
):
# 逆时针旋转90度
print
(
"逆时针旋转90度"
)
# rotated_image = cv2.transpose(image)
# rotated_image = cv2.flip(rotated_image, 0)
rotated_image
=
image
.
rotate
(
90
)
return
rotated_image
def
flip_image_180
(
image
):
# 旋转180度
print
(
"逆时针旋转180度"
)
rotated_image
=
image
.
rotate
(
180
)
return
rotated_image
def
handle_process
(
order_id
,
file_path
,
sns
,
vin
):
print
(
"recognition handle_process:"
,
order_id
,
file_path
,
sns
,
vin
)
# 删除数据库里的旧数据
delete_sql1
=
"DELETE FROM sys_order_ocr_sn_result WHERE order_id = {}"
.
format
(
order_id
)
print
(
delete_sql1
)
cursor_dev
.
execute
(
delete_sql1
)
conn_dev
.
commit
()
delete_sql2
=
"DELETE FROM sys_order_ocr_vin_result WHERE order_id = {}"
.
format
(
order_id
)
print
(
delete_sql2
)
cursor_dev
.
execute
(
delete_sql2
)
conn_dev
.
commit
()
filename
=
generate_random_filename
()
network_img_path
=
file_path
print
(
"network_img_path"
,
network_img_path
)
extension
=
get_extension
(
network_img_path
)
complete_filename
=
f
"{filename}.{extension}"
try
:
response
=
requests
.
get
(
network_img_path
)
except
Exception
as
e
:
response
=
requests
.
get
(
network_img_path
)
path
=
os
.
path
.
join
(
get_project_root
(),
"Identify_sn"
,
"train"
,
"threecode"
,
complete_filename
)
dirname
=
os
.
path
.
dirname
(
path
)
if
not
os
.
path
.
exists
(
dirname
):
os
.
makedirs
(
dirname
)
with
open
(
path
,
"wb"
)
as
file
:
file
.
write
(
response
.
content
)
# urllib.request.urlretrieve(network_img_path, path)
time
.
sleep
(
0.001
)
print
(
"File downloaded successfully."
)
original_image
=
Image
.
open
(
path
)
if
original_image
.
mode
==
"RGBA"
:
original_image
=
original_image
.
convert
(
"RGB"
)
print
(
original_image
.
mode
)
# 放大图片
scale_factor
=
5
# 放大倍数
# scaled_image = original_image.resize(
# (original_image.width * scale_factor, original_image.height * scale_factor)
# )
dst
=
original_image
.
resize
(
(
original_image
.
width
*
scale_factor
,
original_image
.
height
*
scale_factor
)
)
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
print
(
line
)
# print(line[0][1][0])
result_list
=
flatten_list
(
result
)
print
(
"result_list"
,
result_list
)
with
open
(
"./log01.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
# 使用正则表达式进行过滤 获取车架号
sn_str
=
""
flag
=
True
for
item
in
result_list
:
print
(
item
)
sn_check
=
extract_sn
(
item
)
if
sn_check
is
not
None
:
flag
=
False
sn_str
=
sn_check
if
sn_str
:
print
(
sn_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_order_ocr_sn_result (order_id,file_id,db_sn,ocr_sn,create_time,update_time)
VALUES ({order_id},null,'{sns}','{sn_str}',sysdate(),sysddate())
"""
# 执行SQL语句
print
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
if
flag
:
# 旋转45
dst
=
flip_image_45
(
dst
)
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
print
(
line
)
# print(line[0][1][0])
result_list
=
flatten_list
(
result
)
print
(
"result_list"
,
result_list
)
with
open
(
"./log01.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
# 使用正则表达式进行过滤 获取车架号
sn_str
=
""
for
item
in
result_list
:
print
(
item
)
sn_check
=
extract_sn
(
item
)
if
sn_check
is
not
None
:
flag
=
False
sn_str
=
sn_check
if
sn_str
:
print
(
sn_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_order_ocr_sn_result (order_id,file_id,db_sn,ocr_sn,create_time,update_time)
VALUES ({order_id},null,'{sns}','{sn_str}',sysdate(),sysddate())
"""
# 执行SQL语句
print
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
if
flag
:
# 旋转90
dst
=
flip_image_45
(
dst
)
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
print
(
line
)
# print(line[0][1][0])
result_list
=
flatten_list
(
result
)
print
(
"result_list"
,
result_list
)
with
open
(
"./log01.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
# 使用正则表达式进行过滤 获取车架号
sn_str
=
""
for
item
in
result_list
:
print
(
item
)
sn_check
=
extract_sn
(
item
)
if
sn_check
is
not
None
:
flag
=
False
sn_str
=
sn_check
if
sn_str
:
print
(
sn_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_order_ocr_sn_result (order_id,file_id,db_sn,ocr_sn,create_time,update_time)
VALUES ({order_id},null,'{sns}','{sn_str}',sysdate(),sysddate())
"""
# 执行SQL语句
print
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
if
flag
:
# 旋转180
dst
=
flip_image_90
(
dst
)
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
print
(
line
)
# print(line[0][1][0])
result_list
=
flatten_list
(
result
)
print
(
"result_list"
,
result_list
)
with
open
(
"./log01.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
# 使用正则表达式进行过滤 获取车架号
sn_str
=
""
for
item
in
result_list
:
print
(
item
)
sn_check
=
extract_sn
(
item
)
if
sn_check
is
not
None
:
flag
=
False
sn_str
=
sn_check
if
sn_str
:
print
(
sn_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_order_ocr_sn_result (order_id,file_id,db_sn,ocr_sn,create_time,update_time)
VALUES ({order_id},null,'{sns}','{sn_str}',sysdate(),sysddate())
"""
# 执行SQL语句
print
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
if
flag
:
# 定义要执行的SQL语句
SQL02
=
f
"""
INSERT INTO sys_order_ocr_sn_result (order_id,file_id,db_sn,ocr_sn,create_time,update_time)
VALUES ({order_id},null,'{sns}',null,sysdate(),sysddate())
"""
# 执行SQL语句
print
(
SQL02
)
cursor_dev
.
execute
(
SQL02
)
conn_dev
.
commit
()
print
(
"==========================处理vin=================================="
)
# 创建对比度增强器
# enhancer = ImageEnhance.Contrast(original_image)
# 增强对比度(将对比度增强为原来的2倍)
# factor = 2.0
# original_image = enhancer.enhance(factor)
# 放大图片
scale_factor
=
3
# 放大倍数
# scaled_image = original_image.resize(
# (original_image.width * scale_factor, original_image.height * scale_factor)
# )
dst
=
original_image
.
resize
(
(
original_image
.
width
*
scale_factor
,
original_image
.
height
*
scale_factor
)
)
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
print
(
line
)
# print(line[0][1][0])
result_list
=
flatten_list
(
result
)
print
(
"result_list"
,
result_list
)
with
open
(
"./log10.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
# 使用正则表达式进行过滤 获取车架号
vin_str
=
""
for
item
in
result_list
:
print
(
item
)
vin_check
=
extract_vin
(
item
)
if
vin_check
is
not
None
:
vin_str
=
vin_check
break
if
vin_str
:
print
(
vin_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_order_ocr_vin_result (order_id,file_id,db_vin,ocr_vin,create_time,update_time)
VALUES ({order_id},null,'{vin}','{vin_str}',sysdate(),sysdate())
"""
# 执行SQL语句
print
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
else
:
# 旋转90度宠重新识别
dst
=
flip_image_45
(
dst
)
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
print
(
line
)
# print(line[0][1][0])
result_list
=
flatten_list
(
result
)
print
(
"result_list"
,
result_list
)
with
open
(
"./log10.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
vin_str
=
""
for
item
in
result_list
:
print
(
item
)
vin_check
=
extract_vin
(
item
)
if
vin_check
is
not
None
:
vin_str
=
vin_check
break
if
vin_str
:
print
(
vin_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_order_ocr_vin_result (order_id,file_id,db_vin,ocr_vin,create_time,update_time)
VALUES ({order_id},null,'{vin}','{vin_str}',sysdate(),sysdate())
"""
# 执行SQL语句
print
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
else
:
dst
=
flip_image_45
(
dst
)
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
print
(
line
)
# print(line[0][1][0])
result_list
=
flatten_list
(
result
)
print
(
"result_list"
,
result_list
)
with
open
(
"./log10.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
vin_str
=
""
for
item
in
result_list
:
print
(
item
)
vin_check
=
extract_vin
(
item
)
if
vin_check
is
not
None
:
vin_str
=
vin_check
break
if
vin_str
:
print
(
vin_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_order_ocr_vin_result (order_id,file_id,db_vin,ocr_vin,create_time,update_time)
VALUES ({order_id},null,'{vin}','{vin_str}',sysdate(),sysdate())
"""
# 执行SQL语句
print
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
else
:
dst
=
flip_image_90
(
dst
)
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
print
(
line
)
# print(line[0][1][0])
result_list
=
flatten_list
(
result
)
print
(
"result_list"
,
result_list
)
with
open
(
"./log10.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
vin_str
=
""
for
item
in
result_list
:
print
(
item
)
vin_check
=
extract_vin
(
item
)
if
vin_check
is
not
None
:
vin_str
=
vin_check
break
if
vin_str
:
print
(
vin_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_order_ocr_vin_result (order_id,file_id,db_vin,ocr_vin,create_time,update_time)
VALUES ({order_id},null,'{vin}','{vin_str}',sysdate(),sysdate())
"""
# 执行SQL语句
print
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
else
:
# 如果识别不了 直接再放大图片
scale_factor
=
5
dst
=
original_image
.
resize
(
(
original_image
.
width
*
scale_factor
,
original_image
.
height
*
scale_factor
,
)
)
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
print
(
line
)
# print(line[0][1][0])
result_list
=
flatten_list
(
result
)
print
(
"result_list"
,
result_list
)
with
open
(
"./log10.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
vin_str
=
""
for
item
in
result_list
:
print
(
item
)
vin_check
=
extract_vin
(
item
)
if
vin_check
is
not
None
:
vin_str
=
vin_check
break
if
vin_str
:
print
(
vin_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_order_ocr_vin_result (order_id,file_id,db_vin,ocr_vin,create_time,update_time)
VALUES ({order_id},null,'{vin}','{vin_str}',sysdate(),sysdate())
"""
# 执行SQL语句
print
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
else
:
# 定义要执行的SQL语句
SQL02
=
f
"""
INSERT INTO sys_order_ocr_vin_result (order_id,file_id,db_vin,ocr_vin,create_time,update_time)
VALUES ({order_id},null,'{vin}',null,sysdate(),sysdate())
"""
# 执行SQL语句
print
(
SQL02
)
cursor_dev
.
execute
(
SQL02
)
conn_dev
.
commit
()
try
:
os
.
remove
(
path
)
print
(
"文件删除成功"
)
except
FileNotFoundError
:
print
(
"文件不存在"
)
except
PermissionError
:
print
(
"权限不足,无法删除文件"
)
# 在蓝图中定义路由和视图函数
@ocr_classfication_bp.route
(
"/recognition"
,
methods
=
[
"POST"
])
def
identify
():
data
=
request
.
get_data
()
json_data
=
json
.
loads
(
data
.
decode
(
"UTF-8"
))
order_id
=
json_data
.
get
(
"orderId"
)
file_path
=
json_data
.
get
(
"filePath"
)
sns
=
json_data
.
get
(
"sns"
)
vin
=
json_data
.
get
(
"vin"
)
handle_process
(
order_id
,
file_path
,
sns
,
vin
)
kafka_consumer/orc_consumer.py
0 → 100644
View file @
fe907e4
import
json
from
confluent_kafka
import
Consumer
,
KafkaException
,
KafkaError
import
sys
import
logging
# 导入pymysql模块
import
glob
import
os
import
random
import
re
import
time
from
math
import
sqrt
import
cv2
import
numpy
as
np
import
pymysql
import
requests
from
paddleocr
import
PaddleOCR
,
draw_ocr
from
PIL
import
Image
from
pyzbar.pyzbar
import
decode
from
logger
import
log
environment
=
sys
.
argv
[
1
]
if
len
(
sys
.
argv
)
>
1
else
"default"
# environment = os.environ.get('ENV')
if
environment
==
'pro'
:
from
config_pro
import
Config
print
(
vars
(
Config
))
elif
environment
==
'test'
:
from
config_test
import
Config
print
(
vars
(
Config
))
else
:
from
config_dev
import
Config
print
(
vars
(
Config
))
# 配置 Kafka 消费者
conf
=
{
'bootstrap.servers'
:
Config
.
kafka_consumer_bootstrap_servers
,
# Kafka broker 的地址
'group.id'
:
Config
.
kafka_consumer_group_id
,
# 消费者组 ID
'auto.offset.reset'
:
Config
.
kafka_consumer_auto_offset_reset
# 从最早的消息开始消费
}
# 创建 Kafka 消费者
consumer
=
Consumer
(
conf
)
# 订阅主题
consumer
.
subscribe
(
Config
.
kafka_consumer_subscribe
)
# 连接database
conn_dev
=
pymysql
.
connect
(
host
=
Config
.
host
,
port
=
Config
.
port
,
user
=
Config
.
user
,
password
=
Config
.
password
,
database
=
Config
.
database
,
charset
=
"utf8"
,
)
# 得到一个可以执行SQL语句的光标对象
cursor_dev
=
conn_dev
.
cursor
()
# load model
# Paddleocr目前支持中英文、英文、法语、德语、韩语、日语,可以通过修改 lang参数进行切换
# lang参数依次为`ch`, `en`, `french`, `german`, `korean`, `japan`
ocr
=
PaddleOCR
(
use_angle_cls
=
True
,
# use_angle_cls=False,
lang
=
"ch"
,
# use_gpu=False,
use_gpu
=
True
,
use_tensorrt
=
True
,
det_algorithm
=
"DB"
,
# det_algorithm="EAST",
det_db_score_mode
=
"slow"
,
# use_space_char=False, #不可用
det_model_dir
=
"ch_ppocr_server_v4.0_det_infer/"
,
cls_model_dir
=
"ch_ppocr_mobile_v4.0_cls_infer/"
,
rec_model_dir
=
"ch_ppocr_server_v4.0_rec_infer/"
,
det_db_thresh
=
0.1
,
det_db_box_thresh
=
0.1
,
det_db_unclip_ratio
=
2.0
,
max_batch_size
=
20
,
use_dilation
=
True
,
# rec_algorithm="SVTR_LCNet",
)
# 定义要执行的SQL语句
# SQL = """
# select * from sys_order_file_ocr_sn sofon limit 5
# """
# # 执行SQL语句
# cursor_dev.execute(SQL)
#
# # 获取查询结果
# result = cursor_dev.fetchall()
def
generate_random_filename
():
timestamp
=
int
(
time
.
time
())
# 获取当前时间戳
random_number
=
random
.
randint
(
1000
,
9999
)
# 生成一个随机数
filename
=
f
"file_{timestamp}_{random_number}"
# 创建文件名
return
filename
def
get_extension
(
filename
):
parts
=
filename
.
split
(
"."
)
extension
=
parts
[
-
1
]
return
extension
def
get_project_root
():
# 获取当前脚本文件的路径
current_file
=
os
.
path
.
abspath
(
__file__
)
# 获取当前项目的根目录
project_root
=
os
.
path
.
dirname
(
os
.
path
.
dirname
(
current_file
))
print
(
project_root
)
return
project_root
def
flatten_list
(
nested_list
):
flattened_list
=
[]
for
item
in
nested_list
:
if
isinstance
(
item
,
list
):
flattened_list
.
extend
(
flatten_list
(
item
))
elif
isinstance
(
item
,
tuple
):
for
value
in
item
:
if
type
(
value
)
in
(
int
,
float
):
if
value
>
1.0
:
flattened_list
.
append
(
value
)
else
:
flattened_list
.
append
(
value
)
else
:
# flattened_list.append(item)
pass
return
flattened_list
def
validate_vin
(
item
):
pattern
=
r"^(?=.*[0-9A-HJ-NPR-Z])(?!(.*[IOQ]))([0-9A-HJ-NPR-Z]{17})$"
# pattern 是匹配车牌号的正则表达式
match
=
re
.
match
(
pattern
,
str
(
item
))
if
match
:
return
True
else
:
return
False
def
extract_sn
(
string
):
string
=
re
.
sub
(
r"O"
,
"0"
,
string
)
string
=
re
.
sub
(
r"I"
,
"1"
,
string
)
pattern
=
r"\b(\d{10,11})\b"
match
=
re
.
search
(
pattern
,
string
)
if
match
:
sn
=
match
.
group
()
return
sn
else
:
return
None
def
extract_vin
(
string
):
string
=
re
.
sub
(
r"O"
,
"0"
,
string
)
string
=
re
.
sub
(
r"I"
,
"1"
,
string
)
pattern
=
r"(?=.*[0-9A-HJ-NPR-Z])(?!(.*[IOQ]))([0-9A-HJ-NPR-Z]{17})"
# pattern = r"(?=.*[0-9A-HJ-NPR-Z])(?!(.*[IOQ]))([0-9A-HJ-NPR-Z\s\u4e00-\u9fa5]{17})"
# pattern = r"[0-9A-HJ-NPR-Z]{17}"
# pattern = r"^[A-HJ-NPR-Z\d]{17}$"
match
=
re
.
search
(
pattern
,
string
)
if
match
:
vin
=
match
.
group
()
pattern01
=
r"^\d+$"
match
=
re
.
match
(
pattern01
,
vin
)
# 如果全是数字 则不是车架号
if
bool
(
match
):
return
None
else
:
return
vin
else
:
return
None
def
flip_image
(
image
,
angle
):
# 逆时针旋转45度
log
.
info
(
"逆时针旋转"
+
angle
+
"度"
)
# rotated_image = cv2.transpose(image)
# rotated_image = cv2.flip(rotated_image, 0)
rotated_image
=
image
.
rotate
(
angle
)
return
rotated_image
# 处理设备号
def
handler_sn
(
dst
,
flag
,
angle
,
path
,
row
):
# 旋转角度
if
angle
>
0
:
dst
=
flip_image
(
dst
,
angle
)
try
:
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
except
Exception
as
msg
:
log
.
error
(
msg
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
log
.
info
(
line
)
# log.info(line[0][1][0])
result_list
=
flatten_list
(
result
)
log
.
info
(
"result_list
%
s"
,
result_list
)
with
open
(
"./log01.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
# 使用正则表达式进行过滤 获取车架号
for
item
in
result_list
:
log
.
info
(
item
)
sn_check
=
extract_sn
(
item
)
if
sn_check
is
not
None
:
flag
=
False
sn_str
=
sn_check
if
sn_str
:
log
.
info
(
str
(
row
))
log
.
info
(
sn_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_order_ocr_sn_result (order_id,file_id,db_sn,ocr_sn,create_time,update_time)
VALUES ({row[0]},{row[1]},'{row[2]}','{sn_str}',sysdate(),sysddate())
"""
# 执行SQL语句
log
.
info
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
return
flag
# 处理车架号
def
handler_vin
(
dst
,
flag
,
angle
,
path
,
row
):
if
angle
>
0
:
dst
=
flip_image
(
dst
,
angle
)
try
:
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
except
Exception
as
msg
:
log
.
error
(
msg
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
print
(
line
)
# print(line[0][1][0])
result_list
=
flatten_list
(
result
)
print
(
"result_list"
,
result_list
)
with
open
(
"./log10.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
# 使用正则表达式进行过滤 获取车架号
vin_str
=
""
for
item
in
result_list
:
print
(
item
)
vin_check
=
extract_vin
(
item
)
if
vin_check
is
not
None
:
vin_str
=
vin_check
flag
=
False
break
if
vin_str
:
print
(
str
(
row
))
print
(
vin_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_order_ocr_vin_result (order_id,file_id,db_vin,ocr_vin,create_time,update_time)
VALUES ({row[0]},{row[1]},'{row[3]}','{vin_str}',sysdate(),sysddate())
"""
# 执行SQL语句
print
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
return
flag
def
handle_process
(
row
):
print
(
"Received message handle_process: {}"
,
row
)
order_id
=
row
.
get
(
"orderId"
)
file_id
=
row
.
get
(
"fileId"
)
sns
=
row
.
get
(
"sns"
)
vin
=
row
.
get
(
"vin"
)
data
=
[
order_id
,
file_id
,
sns
,
vin
]
# 删除数据库里的旧数据
delete_sql1
=
"DELETE FROM sys_order_ocr_sn_result WHERE order_id = {}"
.
format
(
order_id
)
print
(
delete_sql1
)
cursor_dev
.
execute
(
delete_sql1
)
conn_dev
.
commit
()
delete_sql2
=
"DELETE FROM sys_order_ocr_vin_result WHERE order_id = {}"
.
format
(
order_id
)
print
(
delete_sql2
)
cursor_dev
.
execute
(
delete_sql2
)
conn_dev
.
commit
()
filename
=
generate_random_filename
()
network_img_path
=
row
.
get
(
"filePath"
)
print
(
"network_img_path"
,
network_img_path
)
extension
=
get_extension
(
network_img_path
)
complete_filename
=
f
"{filename}.{extension}"
try
:
response
=
requests
.
get
(
network_img_path
)
except
Exception
as
e
:
response
=
requests
.
get
(
network_img_path
)
path
=
os
.
path
.
join
(
get_project_root
(),
"Identify_sn"
,
"train"
,
"threecode"
,
complete_filename
)
dirname
=
os
.
path
.
dirname
(
path
)
if
not
os
.
path
.
exists
(
dirname
):
os
.
makedirs
(
dirname
)
with
open
(
path
,
"wb"
)
as
file
:
file
.
write
(
response
.
content
)
# urllib.request.urlretrieve(network_img_path, path)
time
.
sleep
(
0.001
)
print
(
"File downloaded successfully."
)
original_image
=
Image
.
open
(
path
)
if
original_image
.
mode
==
"RGBA"
:
original_image
=
original_image
.
convert
(
"RGB"
)
print
(
original_image
.
mode
)
# 放大图片
scale_factor
=
3
# 放大倍数
# scaled_image = original_image.resize(
# (original_image.width * scale_factor, original_image.height * scale_factor)
# )
dst
=
original_image
.
resize
(
(
original_image
.
width
*
scale_factor
,
original_image
.
height
*
scale_factor
)
)
flag
=
True
flag
=
handler_sn
(
dst
,
flag
,
0
,
path
,
data
)
if
flag
:
flag
=
handler_sn
(
dst
,
flag
,
45
,
path
,
data
)
if
flag
:
flag
=
handler_sn
(
dst
,
flag
,
45
,
path
,
data
)
if
flag
:
flag
=
handler_sn
(
dst
,
flag
,
90
,
path
,
data
)
if
flag
:
scale_factor
=
5
dst
=
original_image
.
resize
(
(
original_image
.
width
*
scale_factor
,
original_image
.
height
*
scale_factor
,
)
)
flag
=
handler_sn
(
dst
,
flag
,
0
,
path
,
data
)
if
flag
:
# 定义要执行的SQL语句
SQL02
=
f
"""
INSERT INTO sys_order_ocr_sn_result (order_id,file_id,db_sn,ocr_sn,create_time,update_time)
VALUES ({order_id},{file_id},'{sns}',null,sysdate(),sysddate())
"""
# 执行SQL语句
print
(
SQL02
)
cursor_dev
.
execute
(
SQL02
)
conn_dev
.
commit
()
print
(
"==========================处理vin=================================="
)
# 创建对比度增强器
# enhancer = ImageEnhance.Contrast(original_image)
# 增强对比度(将对比度增强为原来的2倍)
# factor = 2.0
# original_image = enhancer.enhance(factor)
# 放大图片
scale_factor
=
3
# 放大倍数
# scaled_image = original_image.resize(
# (original_image.width * scale_factor, original_image.height * scale_factor)
# )
dst
=
original_image
.
resize
(
(
original_image
.
width
*
scale_factor
,
original_image
.
height
*
scale_factor
)
)
flag
=
True
flag
=
handler_vin
(
dst
,
flag
,
0
,
path
,
data
)
if
flag
:
# 旋转45度宠重新识别
flag
=
handler_vin
(
dst
,
flag
,
45
,
path
,
data
)
if
flag
:
# 旋转90度宠重新识别
flag
=
handler_vin
(
dst
,
flag
,
45
,
path
,
data
)
if
flag
:
# 旋转180度宠重新识别
flag
=
handler_vin
(
dst
,
flag
,
90
,
path
,
data
)
if
flag
:
# 如果识别不了 直接再放大图片
scale_factor
=
5
dst
=
original_image
.
resize
(
(
original_image
.
width
*
scale_factor
,
original_image
.
height
*
scale_factor
,
)
)
flag
=
handler_vin
(
dst
,
flag
,
0
,
path
,
row
)
if
flag
:
# 定义要执行的SQL语句
SQL02
=
f
"""
INSERT INTO sys_order_ocr_vin_result (order_id,file_id,db_vin,ocr_vin,create_time,update_time)
VALUES ({order_id},{file_id},'{vin}',null,sysdate(),sysddate())
"""
# 执行SQL语句
print
(
SQL02
)
cursor_dev
.
execute
(
SQL02
)
conn_dev
.
commit
()
try
:
os
.
remove
(
path
)
print
(
"文件删除成功"
)
except
FileNotFoundError
:
print
(
"文件不存在"
)
except
PermissionError
:
print
(
"权限不足,无法删除文件"
)
# 消费消息并进行逻辑处理
try
:
while
True
:
msg
=
consumer
.
poll
(
timeout
=
1.0
)
if
msg
is
None
:
continue
if
msg
.
error
():
if
msg
.
error
()
.
code
()
==
KafkaError
.
_PARTITION_EOF
:
continue
else
:
print
(
msg
.
error
())
break
# 处理逻辑
handle_process
(
json
.
loads
(
msg
.
value
()
.
decode
(
'utf-8'
)))
except
KeyboardInterrupt
:
sys
.
stderr
.
write
(
'
%%
Aborted by user
\n
'
)
finally
:
# 关闭消费者
consumer
.
close
()
# 关闭光标对象
cursor_dev
.
close
()
# 关闭数据库连接
conn_dev
.
close
()
Identify_sn/
logger.py
→
logger.py
View file @
fe907e4
File moved
manage.py
View file @
fe907e4
...
@@ -3,7 +3,7 @@ from flask import request, render_template
...
@@ -3,7 +3,7 @@ from flask import request, render_template
from
flaskr
import
create_app
from
flaskr
import
create_app
from
flaskr.biz
import
user_service
from
flaskr.biz
import
user_service
from
flaskr.biz.identify_service
import
IdentifyService
from
flaskr.biz.identify_service
import
IdentifyService
from
flaskr.controller
import
image_classfication_controller
from
flaskr.controller
import
image_classfication_controller
,
ocr_controller
import
os
import
os
app
=
create_app
()
app
=
create_app
()
...
@@ -11,6 +11,9 @@ app = create_app()
...
@@ -11,6 +11,9 @@ app = create_app()
app
.
register_blueprint
(
app
.
register_blueprint
(
image_classfication_controller
.
classfication_bp
,
url_prefix
=
"/classfication"
image_classfication_controller
.
classfication_bp
,
url_prefix
=
"/classfication"
)
)
app
.
register_blueprint
(
ocr_controller
.
ocr_classfication_bp
,
url_prefix
=
"/ocr"
)
@app.route
(
"/"
)
@app.route
(
"/"
)
...
...
Write
Preview
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