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 621b5e7b
authored
Mar 21, 2024
by
周杭
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
优化代码
1 parent
c945b37e
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
172 additions
and
25 deletions
kafka_consumer/ocr_consumer.py
kafka_consumer/test03.py
test06.py
kafka_consumer/ocr_consumer.py
View file @
621b5e7
...
@@ -101,6 +101,8 @@ ocr = PaddleOCR(
...
@@ -101,6 +101,8 @@ ocr = PaddleOCR(
log
.
info
(
"启动4"
)
log
.
info
(
"启动4"
)
# 支持的图片类型
supported_formats
=
{
'bmp'
,
'jpg'
,
'jpeg'
,
'png'
}
# 从连接池获取连接
# 从连接池获取连接
def
get_connection
():
def
get_connection
():
...
@@ -393,7 +395,13 @@ def handle_process(jsonData):
...
@@ -393,7 +395,13 @@ def handle_process(jsonData):
log
.
info
(
"network_img_path
%
s"
,
network_img_path
)
log
.
info
(
"network_img_path
%
s"
,
network_img_path
)
extension
=
get_extension
(
network_img_path
)
extension
=
get_extension
(
network_img_path
)
complete_filename
=
f
"{filename}.{extension}"
complete_filename
=
f
"{filename}.{extension}"
path
=
None
path
=
None
if
extension
.
lower
()
not
in
supported_formats
:
log
.
info
(
f
"跳过文件 {network_img_path},因为它的格式 {extension} 不受支持"
)
return
try
:
try
:
response
=
requests
.
get
(
network_img_path
)
response
=
requests
.
get
(
network_img_path
)
except
Exception
as
e
:
except
Exception
as
e
:
...
@@ -459,10 +467,8 @@ def handle_process(jsonData):
...
@@ -459,10 +467,8 @@ def handle_process(jsonData):
log
.
info
(
delete_sql1
)
log
.
info
(
delete_sql1
)
execute_sql
(
delete_sql1
)
execute_sql
(
delete_sql1
)
ocr_sn
(
path
,
data
,
original_image
)
ocr_sn
(
path
,
data
,
original_image
)
except
FileNotFoundError
:
except
Exception
as
e
:
log
.
info
(
"文件不存在"
)
log
.
error
(
"handle_process Exception: "
+
str
(
e
))
except
PermissionError
:
log
.
info
(
"权限不足,无法删除文件"
)
finally
:
finally
:
if
path
:
if
path
:
os
.
remove
(
path
)
os
.
remove
(
path
)
...
...
kafka_consumer/test03.py
View file @
621b5e7
...
@@ -8,7 +8,7 @@ producer = KafkaProducer(bootstrap_servers="192.168.3.131:9092") # Kafka集群
...
@@ -8,7 +8,7 @@ producer = KafkaProducer(bootstrap_servers="192.168.3.131:9092") # Kafka集群
# 发送消息
# 发送消息
topic
=
"ocr_topic"
# Kafka主题名称
topic
=
"ocr_topic"
# Kafka主题名称
key
=
b
"my-key"
# 消息的键(可选)
key
=
b
"my-key"
# 消息的键(可选)
data
=
{
'orderId'
:
1234
,
'fileId'
:
31
,
'filePath'
:
'https://bhg.didihu.com.cn/group1/M00/2
2/05/wKgFDWWnocCAZllpAADp9tDJibY696.jpg
'
,
'sns'
:
'96312110851'
,
'vin'
:
'VIN654321'
,
'fileType'
:
1
}
data
=
{
'orderId'
:
1234
,
'fileId'
:
31
,
'filePath'
:
'https://bhg.didihu.com.cn/group1/M00/2
A/D0/wKgFDWX6nOKAVKERACzGdqA8Vk076.heic
'
,
'sns'
:
'96312110851'
,
'vin'
:
'VIN654321'
,
'fileType'
:
1
}
message
=
json
.
dumps
(
data
)
.
encode
(
'utf-8'
)
message
=
json
.
dumps
(
data
)
.
encode
(
'utf-8'
)
producer
.
send
(
topic
,
message
)
producer
.
send
(
topic
,
message
)
...
...
test06.py
View file @
621b5e7
import
pymysql
# 导入pymysql模块
import
glob
import
os
import
random
import
re
import
time
from
math
import
sqrt
from
logger
import
log
import
cv2
import
numpy
as
np
from
config_dev
import
Config
from
paddleocr
import
PaddleOCR
,
draw_ocr
,
draw_structure_result
,
save_structure_res
,
PPStructure
from
PIL
import
Image
conn_dev
=
pymysql
.
connect
(
host
=
Config
.
host
,
# load model
port
=
Config
.
port
,
# Paddleocr目前支持中英文、英文、法语、德语、韩语、日语,可以通过修改 lang参数进行切换
user
=
Config
.
user
,
# lang参数依次为`ch`, `en`, `french`, `german`, `korean`, `japan`
password
=
Config
.
password
,
ocr
=
PaddleOCR
(
database
=
Config
.
database
,
use_angle_cls
=
True
,
charset
=
"utf8"
,
# 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
=
10
,
use_dilation
=
True
,
# rec_algorithm="SVTR_LCNet",
)
)
cursor_dev
=
conn_dev
.
cursor
()
table
=
PPStructure
(
show_log
=
True
,
layout
=
False
,
table
=
True
)
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度
print
(
"逆时针旋转"
+
str
(
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
,
angle
):
# 旋转角度
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
)
print
(
"============================================================================================================="
)
return
# 下载图片数据
def
recognize
():
img_path
=
r"C:\Users\mima123456\Desktop\s6.png"
# img_path = "./s4.png"
img
=
cv2
.
imread
(
img_path
)
result
=
table
(
img
)
# 定义要执行的SQL语句
print
(
result
)
SQL
=
"""
print
(
"=============================================================="
)
select * from sys_order_file_ocr_sn_01 sofon limit 2000
print
(
result
[
0
][
'res'
])
"""
# 执行SQL语句
cursor_dev
.
execute
(
SQL
)
# 获取查询结果
result
=
cursor_dev
.
fetchall
()
for
index
,
row
in
enumerate
(
result
):
print
(
row
)
if
__name__
==
"__main__"
:
recognize
()
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