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 d260b457
authored
Jan 22, 2024
by
杨林龙
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
优化OCR检测逻辑
1 parent
50024163
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
483 additions
and
5 deletions
Identify_sn/main04.py
Identify_sn/main05.py
Identify_sn/main06.py
Identify_sn/main04.py
View file @
d260b45
...
@@ -75,7 +75,7 @@ ocr = PaddleOCR(
...
@@ -75,7 +75,7 @@ ocr = PaddleOCR(
# 定义要执行的SQL语句
# 定义要执行的SQL语句
SQL
=
"""
SQL
=
"""
select * from sys_order_file_ocr_sn sofon limit
1
000
select * from sys_order_file_ocr_sn sofon limit
2
000
"""
"""
# 执行SQL语句
# 执行SQL语句
cursor_dev
.
execute
(
SQL
)
cursor_dev
.
execute
(
SQL
)
...
@@ -171,7 +171,7 @@ for index, row in enumerate(result):
...
@@ -171,7 +171,7 @@ for index, row in enumerate(result):
original_image
=
Image
.
open
(
path
)
original_image
=
Image
.
open
(
path
)
# 放大图片
# 放大图片
scale_factor
=
3
# 放大倍数
scale_factor
=
5
# 放大倍数
# scaled_image = original_image.resize(
# scaled_image = original_image.resize(
# (original_image.width * scale_factor, original_image.height * scale_factor)
# (original_image.width * scale_factor, original_image.height * scale_factor)
# )
# )
...
@@ -188,7 +188,7 @@ for index, row in enumerate(result):
...
@@ -188,7 +188,7 @@ for index, row in enumerate(result):
result_list
=
flatten_list
(
result
)
result_list
=
flatten_list
(
result
)
print
(
"result_list"
,
result_list
)
print
(
"result_list"
,
result_list
)
with
open
(
"./log0.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
with
open
(
"./log0
1
.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
else
:
...
@@ -208,7 +208,7 @@ for index, row in enumerate(result):
...
@@ -208,7 +208,7 @@ for index, row in enumerate(result):
print
(
sn_str
)
print
(
sn_str
)
# 定义要执行的SQL语句
# 定义要执行的SQL语句
SQL01
=
f
"""
SQL01
=
f
"""
INSERT INTO sys_vin_check_ocr_sn (order_id,file_id,db_sn,ocr_sn,create_time)
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())
VALUES ({row[1]},{row[0]},'{row[9]}','{sn_str}',sysdate())
"""
"""
# 执行SQL语句
# 执行SQL语句
...
@@ -218,7 +218,7 @@ for index, row in enumerate(result):
...
@@ -218,7 +218,7 @@ for index, row in enumerate(result):
else
:
else
:
# 定义要执行的SQL语句
# 定义要执行的SQL语句
SQL02
=
f
"""
SQL02
=
f
"""
INSERT INTO sys_vin_check_ocr_sn (order_id,file_id,db_sn,ocr_sn,create_time)
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]}',null,sysdate())
VALUES ({row[1]},{row[0]},'{row[9]}',null,sysdate())
"""
"""
# 执行SQL语句
# 执行SQL语句
...
...
Identify_sn/main05.py
0 → 100644
View file @
d260b45
# 导入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
# 连接database
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",
)
# 连接database
# conn = pymysql.connect(
# host="192.168.5.1",
# port=3307,
# user="user_yangll",
# password="vGxw9jWg",
# database="glsx_car_service",
# charset="utf8",
# )
# # 得到一个可以执行SQL语句的光标对象
# cursor = conn.cursor()
# # 定义要执行的SQL语句
# SQL = """
# select sof.*,so.vin_no from sys_order_file sof LEFT JOIN sys_order so ON so.id = sof.order_id
# WHERE so.order_source = 'pazl' AND sof.file_name = '三码合一';
# """
# # 执行SQL语句
# cursor.execute(SQL)
# # 获取查询结果
# result = cursor.fetchall()
# 定义要执行的SQL语句
SQL
=
"""
SELECT b.* FROM sys_vin_check_ocr_sn_01 a LEFT JOIN sys_order_file_ocr_sn b ON b.id = a.file_id
WHERE FIND_IN_SET(a.ocr_sn, a.db_sn) = 0 OR a.ocr_sn IS NULL ;
"""
# 执行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_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})"
match
=
re
.
search
(
pattern
,
string
)
if
match
:
vin
=
match
.
group
()
return
vin
else
:
return
None
# 下载图片数据
for
index
,
row
in
enumerate
(
result
):
print
(
row
)
filename
=
generate_random_filename
()
network_img_path
=
row
[
4
]
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_vinandsn"
,
"train"
,
"threecode"
,
complete_filename
)
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
)
# 放大图片
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
(
"./log02.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
)
vin_check
=
extract_vin
(
item
)
if
vin_check
is
not
None
:
sn_str
=
vin_check
break
if
sn_str
:
print
(
str
(
row
))
print
(
sn_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_vin_check_ocr_sn_02 (order_id,file_id,db_sn,ocr_sn,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}','{sn_str}',sysdate())
"""
# 执行SQL语句
print
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
else
:
# 定义要执行的SQL语句
SQL02
=
f
"""
INSERT INTO sys_vin_check_ocr_sn_02 (order_id,file_id,db_sn,ocr_sn,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}',null,sysdate())
"""
# 执行SQL语句
print
(
SQL02
)
cursor_dev
.
execute
(
SQL02
)
conn_dev
.
commit
()
# # 关闭光标对象
# cursor.close()
# # 关闭数据库连接
# conn.close()
# 关闭光标对象
cursor_dev
.
close
()
# 关闭数据库连接
conn_dev
.
close
()
Identify_sn/main06.py
0 → 100644
View file @
d260b45
# 导入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
# 连接database
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",
)
# 连接database
# conn = pymysql.connect(
# host="192.168.5.1",
# port=3307,
# user="user_yangll",
# password="vGxw9jWg",
# database="glsx_car_service",
# charset="utf8",
# )
# # 得到一个可以执行SQL语句的光标对象
# cursor = conn.cursor()
# # 定义要执行的SQL语句
# SQL = """
# select sof.*,so.vin_no from sys_order_file sof LEFT JOIN sys_order so ON so.id = sof.order_id
# WHERE so.order_source = 'pazl' AND sof.file_name = '三码合一';
# """
# # 执行SQL语句
# cursor.execute(SQL)
# # 获取查询结果
# result = cursor.fetchall()
# 定义要执行的SQL语句
SQL
=
"""
select * from sys_order_file_ocr_sn_01 sofon limit 2000
"""
# 执行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
# 下载图片数据
for
index
,
row
in
enumerate
(
result
):
print
(
row
)
filename
=
generate_random_filename
()
network_img_path
=
row
[
4
]
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
)
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
)
# 放大图片
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
(
"./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
)
vin_check
=
extract_sn
(
item
)
if
vin_check
is
not
None
:
sn_str
=
vin_check
break
if
sn_str
:
print
(
str
(
row
))
print
(
sn_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_vin_check_ocr_sn_03 (order_id,file_id,db_sn,ocr_sn,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}','{sn_str}',sysdate())
"""
# 执行SQL语句
print
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
else
:
# 定义要执行的SQL语句
SQL02
=
f
"""
INSERT INTO sys_vin_check_ocr_sn_03 (order_id,file_id,db_sn,ocr_sn,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}',null,sysdate())
"""
# 执行SQL语句
print
(
SQL02
)
cursor_dev
.
execute
(
SQL02
)
conn_dev
.
commit
()
# # 关闭光标对象
# cursor.close()
# # 关闭数据库连接
# conn.close()
# 关闭光标对象
cursor_dev
.
close
()
# 关闭数据库连接
conn_dev
.
close
()
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