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 f9a5c6b7
authored
Mar 12, 2024
by
杨林龙
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
优化mysql 连接池连接
1 parent
164e893a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
44 additions
and
50 deletions
kafka_consumer/ocr_consumer.py
kafka_consumer/ocr_consumer.py
View file @
f9a5c6b
...
...
@@ -15,7 +15,7 @@ import cv2
import
numpy
as
np
import
pymysql
import
requests
from
paddleocr
import
PaddleOCR
,
draw_ocr
from
paddleocr
import
PaddleOCR
from
PIL
import
Image
from
pyzbar.pyzbar
import
decode
# 获取当前文件所在目录的绝对路径
...
...
@@ -59,10 +59,10 @@ connection_pool = pooling.MySQLConnectionPool(
port
=
Config
.
port
,
database
=
Config
.
database
,
user
=
Config
.
user
,
password
=
Config
.
password
password
=
Config
.
password
,
autocommit
=
True
,
)
log
.
info
(
"启动3"
)
# load model
# Paddleocr目前支持中英文、英文、法语、德语、韩语、日语,可以通过修改 lang参数进行切换
...
...
@@ -104,8 +104,20 @@ log.info("启动4")
# 从连接池获取连接
def
get_connection
():
log
.
info
(
"connection_pool size:"
+
str
(
connection_pool
.
_cnx_queue
.
qsize
()))
# 从连接池获取连接
return
connection_pool
.
get_connection
()
def
execute_sql
(
sql
):
conn
=
get_connection
()
cursor
=
conn
.
cursor
()
cursor
.
execute
(
sql
)
conn
.
commit
()
cursor
.
close
()
conn
.
close
()
log
.
info
(
"sql 执行结束"
)
def
generate_random_filename
():
timestamp
=
int
(
time
.
time
())
# 获取当前时间戳
random_number
=
random
.
randint
(
1000
,
9999
)
# 生成一个随机数
...
...
@@ -197,7 +209,7 @@ def flip_image(image,angle):
return
rotated_image
# 处理设备号
def
handler_sn
(
dst
,
flag
,
angle
,
path
,
row
,
connection
,
cursor
):
def
handler_sn
(
dst
,
flag
,
angle
,
path
,
row
):
# 旋转角度
if
angle
>
0
:
dst
=
flip_image
(
dst
,
angle
)
...
...
@@ -230,12 +242,11 @@ def handler_sn(dst, flag, angle, path, row, connection, cursor):
# 执行SQL语句
log
.
info
(
SQL01
)
SQL01
=
SQL01
.
replace
(
"'None',"
,
"null,"
)
.
replace
(
"None,"
,
"null,"
)
cursor
.
execute
(
SQL01
)
connection
.
commit
()
execute_sql
(
SQL01
)
return
flag
# 处理车架号
def
handler_vin
(
dst
,
flag
,
angle
,
path
,
row
,
connection
,
cursor
):
def
handler_vin
(
dst
,
flag
,
angle
,
path
,
row
):
if
angle
>
0
:
dst
=
flip_image
(
dst
,
angle
)
...
...
@@ -272,22 +283,21 @@ def handler_vin(dst, flag, angle, path, row, connection, cursor):
# 执行SQL语句
log
.
info
(
SQL01
)
SQL01
=
SQL01
.
replace
(
"'None',"
,
"null,"
)
.
replace
(
"None,"
,
"null,"
)
cursor
.
execute
(
SQL01
)
connection
.
commit
()
execute_sql
(
SQL01
)
return
flag
def
ocr_sn
(
dst
,
path
,
data
,
connection
,
cursor
,
original_image
):
def
ocr_sn
(
dst
,
path
,
data
,
original_image
):
log
.
info
(
"==========================处理sn=================================="
)
flag
=
True
flag
=
handler_sn
(
dst
,
flag
,
0
,
path
,
data
,
connection
,
cursor
)
flag
=
handler_sn
(
dst
,
flag
,
0
,
path
,
data
)
if
flag
:
flag
=
handler_sn
(
dst
,
flag
,
45
,
path
,
data
,
connection
,
cursor
)
flag
=
handler_sn
(
dst
,
flag
,
45
,
path
,
data
)
if
flag
:
flag
=
handler_sn
(
dst
,
flag
,
45
,
path
,
data
,
connection
,
cursor
)
flag
=
handler_sn
(
dst
,
flag
,
45
,
path
,
data
)
if
flag
:
flag
=
handler_sn
(
dst
,
flag
,
90
,
path
,
data
,
connection
,
cursor
)
flag
=
handler_sn
(
dst
,
flag
,
90
,
path
,
data
)
if
flag
:
scale_factor
=
5
dst
=
original_image
.
resize
(
...
...
@@ -296,7 +306,7 @@ def ocr_sn(dst, path, data, connection, cursor, original_image):
original_image
.
height
*
scale_factor
,
)
)
flag
=
handler_sn
(
dst
,
flag
,
0
,
path
,
data
,
connection
,
cursor
)
flag
=
handler_sn
(
dst
,
flag
,
0
,
path
,
data
)
if
flag
:
# 定义要执行的SQL语句
SQL02
=
f
"""
...
...
@@ -306,11 +316,10 @@ def ocr_sn(dst, path, data, connection, cursor, original_image):
# 执行SQL语句
log
.
info
(
SQL02
)
SQL02
=
SQL02
.
replace
(
"'None',"
,
"null,"
)
.
replace
(
"None,"
,
"null,"
)
cursor
.
execute
(
SQL02
)
connection
.
commit
()
execute_sql
(
SQL02
)
def
ocr_vin
(
path
,
data
,
connection
,
cursor
,
original_image
):
def
ocr_vin
(
path
,
data
,
original_image
):
log
.
info
(
"==========================处理vin=================================="
)
# 创建对比度增强器
...
...
@@ -330,16 +339,16 @@ def ocr_vin(path, data, connection, cursor, original_image):
(
original_image
.
width
*
scale_factor
,
original_image
.
height
*
scale_factor
)
)
flag
=
True
flag
=
handler_vin
(
dst
,
flag
,
0
,
path
,
data
,
connection
,
cursor
)
flag
=
handler_vin
(
dst
,
flag
,
0
,
path
,
data
)
if
flag
:
# 旋转45度宠重新识别
flag
=
handler_vin
(
dst
,
flag
,
45
,
path
,
data
,
connection
,
cursor
)
flag
=
handler_vin
(
dst
,
flag
,
45
,
path
,
data
)
if
flag
:
# 旋转90度宠重新识别
flag
=
handler_vin
(
dst
,
flag
,
45
,
path
,
data
,
connection
,
cursor
)
flag
=
handler_vin
(
dst
,
flag
,
45
,
path
,
data
)
if
flag
:
# 旋转180度宠重新识别
flag
=
handler_vin
(
dst
,
flag
,
90
,
path
,
data
,
connection
,
cursor
)
flag
=
handler_vin
(
dst
,
flag
,
90
,
path
,
data
)
if
flag
:
# 如果识别不了 直接再放大图片
scale_factor
=
5
...
...
@@ -349,7 +358,7 @@ def ocr_vin(path, data, connection, cursor, original_image):
original_image
.
height
*
scale_factor
,
)
)
flag
=
handler_vin
(
dst
,
flag
,
0
,
path
,
data
,
connection
,
cursor
)
flag
=
handler_vin
(
dst
,
flag
,
0
,
path
,
data
)
if
flag
:
# 定义要执行的SQL语句
SQL02
=
f
"""
...
...
@@ -359,9 +368,7 @@ def ocr_vin(path, data, connection, cursor, original_image):
# 执行SQL语句
log
.
info
(
SQL02
)
SQL02
=
SQL02
.
replace
(
"'None',"
,
"null,"
)
.
replace
(
"None,"
,
"null,"
)
cursor
.
execute
(
SQL02
)
connection
.
commit
()
execute_sql
(
SQL02
)
def
handle_process
(
jsonData
):
...
...
@@ -414,46 +421,34 @@ def handle_process(jsonData):
(
original_image
.
width
*
scale_factor
,
original_image
.
height
*
scale_factor
)
)
# 获取数据库连接
connection
=
get_connection
()
cursor
=
connection
.
cursor
()
if
file_type
==
0
:
# 删除数据库里的vin旧数据
delete_sql2
=
"DELETE FROM sys_order_ocr_vin_result WHERE order_id = {}"
.
format
(
order_id
)
log
.
info
(
delete_sql2
)
cursor
.
execute
(
delete_sql2
)
connection
.
commit
()
execute_sql
(
delete_sql2
)
# 删除数据库里的sn旧数据
delete_sql1
=
"DELETE FROM sys_order_ocr_sn_result WHERE order_id = {}"
.
format
(
order_id
)
log
.
info
(
delete_sql1
)
cursor
.
execute
(
delete_sql1
)
connection
.
commit
()
#识别vin
ocr_vin
(
path
,
data
,
connection
,
cursor
,
original_image
)
#识别sn
ocr_sn
(
dst
,
path
,
data
,
connection
,
cursor
,
original_image
)
execute_sql
(
delete_sql1
)
# 识别vin
ocr_vin
(
path
,
data
,
original_image
)
# 识别sn
ocr_sn
(
dst
,
path
,
data
,
original_image
)
if
file_type
==
1
:
# 删除数据库里的sn旧数据
delete_sql1
=
"DELETE FROM sys_order_ocr_sn_result WHERE order_id = {}"
.
format
(
order_id
)
log
.
info
(
delete_sql1
)
cursor
.
execute
(
delete_sql1
)
connection
.
commit
()
ocr_sn
(
dst
,
path
,
data
,
connection
,
cursor
,
original_image
)
os
.
remove
(
path
)
log
.
info
(
"文件删除成功"
)
execute_sql
(
delete_sql1
)
ocr_sn
(
dst
,
path
,
data
,
original_image
)
except
FileNotFoundError
:
log
.
info
(
"文件不存在"
)
except
PermissionError
:
log
.
info
(
"权限不足,无法删除文件"
)
finally
:
os
.
remove
(
path
)
log
.
info
(
"文件删除成功"
)
log
.
info
(
"finally关闭连接"
)
if
'cursor'
in
locals
():
cursor
.
close
()
log
.
info
(
"关闭cursor"
)
def
consume_kafka
():
...
...
@@ -467,10 +462,9 @@ def consume_kafka():
start_time
=
time
.
time
()
handle_process
(
msg
)
end_time
=
time
.
time
()
log
.
info
(
"handle_process耗时
:
"
+
str
(
end_time
-
start_time
))
log
.
info
(
"handle_process耗时
:
"
+
str
(
end_time
-
start_time
))
except
Exception
as
e
:
log
.
error
(
"ocr consumer Exception: "
+
str
(
e
))
finally
:
# 关闭消费者
consumer
.
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