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 fd42a258
authored
Mar 01, 2024
by
周杭
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
线程池+MySQL连接池
1 parent
4b1edc61
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
80 additions
and
39 deletions
kafka_consumer/ocr_consumer.py
kafka_consumer/test03.py
manage.py
kafka_consumer/ocr_consumer.py
View file @
fd42a25
...
@@ -10,7 +10,7 @@ import random
...
@@ -10,7 +10,7 @@ import random
import
re
import
re
import
time
import
time
from
math
import
sqrt
from
math
import
sqrt
from
mysql.connector
import
pooling
import
cv2
import
cv2
import
numpy
as
np
import
numpy
as
np
import
pymysql
import
pymysql
...
@@ -49,17 +49,19 @@ consumer = KafkaConsumer(
...
@@ -49,17 +49,19 @@ consumer = KafkaConsumer(
auto_offset_reset
=
Config
.
kafka_consumer_auto_offset_reset
# 从最早的消息开始消费
auto_offset_reset
=
Config
.
kafka_consumer_auto_offset_reset
# 从最早的消息开始消费
)
)
log
.
info
(
"启动2"
)
log
.
info
(
"启动2"
)
# 连接database
conn_dev
=
pymysql
.
connect
(
# 创建连接池对象
connection_pool
=
pooling
.
MySQLConnectionPool
(
pool_name
=
"mypool"
,
pool_size
=
10
,
pool_reset_session
=
True
,
host
=
Config
.
host
,
host
=
Config
.
host
,
port
=
Config
.
port
,
port
=
Config
.
port
,
user
=
Config
.
user
,
password
=
Config
.
password
,
database
=
Config
.
database
,
database
=
Config
.
database
,
charset
=
"utf8"
,
user
=
Config
.
user
,
password
=
Config
.
password
)
)
# 得到一个可以执行SQL语句的光标对象
cursor_dev
=
conn_dev
.
cursor
()
log
.
info
(
"启动3"
)
log
.
info
(
"启动3"
)
# load model
# load model
...
@@ -98,6 +100,12 @@ ocr = PaddleOCR(
...
@@ -98,6 +100,12 @@ ocr = PaddleOCR(
# result = cursor_dev.fetchall()
# result = cursor_dev.fetchall()
log
.
info
(
"启动4"
)
log
.
info
(
"启动4"
)
# 从连接池获取连接
def
get_connection
():
return
connection_pool
.
get_connection
()
def
generate_random_filename
():
def
generate_random_filename
():
timestamp
=
int
(
time
.
time
())
# 获取当前时间戳
timestamp
=
int
(
time
.
time
())
# 获取当前时间戳
random_number
=
random
.
randint
(
1000
,
9999
)
# 生成一个随机数
random_number
=
random
.
randint
(
1000
,
9999
)
# 生成一个随机数
...
@@ -189,7 +197,7 @@ def flip_image(image,angle):
...
@@ -189,7 +197,7 @@ def flip_image(image,angle):
return
rotated_image
return
rotated_image
# 处理设备号
# 处理设备号
def
handler_sn
(
dst
,
flag
,
angle
,
path
,
row
):
def
handler_sn
(
dst
,
flag
,
angle
,
path
,
row
,
connection
,
cursor
):
# 旋转角度
# 旋转角度
if
angle
>
0
:
if
angle
>
0
:
dst
=
flip_image
(
dst
,
angle
)
dst
=
flip_image
(
dst
,
angle
)
...
@@ -228,12 +236,12 @@ def handler_sn(dst, flag, angle, path, row):
...
@@ -228,12 +236,12 @@ def handler_sn(dst, flag, angle, path, row):
# 执行SQL语句
# 执行SQL语句
log
.
info
(
SQL01
)
log
.
info
(
SQL01
)
SQL01
=
SQL01
.
replace
(
"'None',"
,
"null,"
)
.
replace
(
"None,"
,
"null,"
)
SQL01
=
SQL01
.
replace
(
"'None',"
,
"null,"
)
.
replace
(
"None,"
,
"null,"
)
cursor
_dev
.
execute
(
SQL01
)
cursor
.
execute
(
SQL01
)
conn
_dev
.
commit
()
conn
ection
.
commit
()
return
flag
return
flag
# 处理车架号
# 处理车架号
def
handler_vin
(
dst
,
flag
,
angle
,
path
,
row
):
def
handler_vin
(
dst
,
flag
,
angle
,
path
,
row
,
connection
,
cursor
):
if
angle
>
0
:
if
angle
>
0
:
dst
=
flip_image
(
dst
,
angle
)
dst
=
flip_image
(
dst
,
angle
)
...
@@ -276,12 +284,13 @@ def handler_vin(dst, flag, angle, path,row):
...
@@ -276,12 +284,13 @@ def handler_vin(dst, flag, angle, path,row):
# 执行SQL语句
# 执行SQL语句
log
.
info
(
SQL01
)
log
.
info
(
SQL01
)
SQL01
=
SQL01
.
replace
(
"'None',"
,
"null,"
)
.
replace
(
"None,"
,
"null,"
)
SQL01
=
SQL01
.
replace
(
"'None',"
,
"null,"
)
.
replace
(
"None,"
,
"null,"
)
cursor
_dev
.
execute
(
SQL01
)
cursor
.
execute
(
SQL01
)
conn
_dev
.
commit
()
conn
ection
.
commit
()
return
flag
return
flag
def
handle_process
(
jsonData
):
def
handle_process
(
jsonData
):
try
:
log
.
info
(
"Received message handle_process:
%
s"
,
jsonData
)
log
.
info
(
"Received message handle_process:
%
s"
,
jsonData
)
row
=
json
.
loads
(
jsonData
.
value
.
decode
(
'utf-8'
))
row
=
json
.
loads
(
jsonData
.
value
.
decode
(
'utf-8'
))
order_id
=
row
.
get
(
"orderId"
)
order_id
=
row
.
get
(
"orderId"
)
...
@@ -291,15 +300,18 @@ def handle_process(jsonData):
...
@@ -291,15 +300,18 @@ def handle_process(jsonData):
data
=
[
order_id
,
file_id
,
sns
,
vin
]
data
=
[
order_id
,
file_id
,
sns
,
vin
]
# 获取数据库连接
connection
=
get_connection
()
cursor
=
connection
.
cursor
()
# 删除数据库里的旧数据
# 删除数据库里的旧数据
delete_sql1
=
"DELETE FROM sys_order_ocr_sn_result WHERE order_id = {}"
.
format
(
order_id
)
delete_sql1
=
"DELETE FROM sys_order_ocr_sn_result WHERE order_id = {}"
.
format
(
order_id
)
log
.
info
(
delete_sql1
)
log
.
info
(
delete_sql1
)
cursor_dev
.
execute
(
delete_sql1
)
cursor
.
execute
(
delete_sql1
)
conn_dev
.
commit
()
connection
.
commit
()
delete_sql2
=
"DELETE FROM sys_order_ocr_vin_result WHERE order_id = {}"
.
format
(
order_id
)
delete_sql2
=
"DELETE FROM sys_order_ocr_vin_result WHERE order_id = {}"
.
format
(
order_id
)
log
.
info
(
delete_sql2
)
log
.
info
(
delete_sql2
)
cursor_dev
.
execute
(
delete_sql2
)
cursor
.
execute
(
delete_sql2
)
conn_dev
.
commit
()
connection
.
commit
()
filename
=
generate_random_filename
()
filename
=
generate_random_filename
()
network_img_path
=
row
.
get
(
"filePath"
)
network_img_path
=
row
.
get
(
"filePath"
)
...
@@ -340,14 +352,14 @@ def handle_process(jsonData):
...
@@ -340,14 +352,14 @@ def handle_process(jsonData):
)
)
flag
=
True
flag
=
True
flag
=
handler_sn
(
dst
,
flag
,
0
,
path
,
data
)
flag
=
handler_sn
(
dst
,
flag
,
0
,
path
,
data
,
connection
,
cursor
)
if
flag
:
if
flag
:
flag
=
handler_sn
(
dst
,
flag
,
45
,
path
,
data
)
flag
=
handler_sn
(
dst
,
flag
,
45
,
path
,
data
,
connection
,
cursor
)
if
flag
:
if
flag
:
flag
=
handler_sn
(
dst
,
flag
,
45
,
path
,
data
)
flag
=
handler_sn
(
dst
,
flag
,
45
,
path
,
data
,
connection
,
cursor
)
if
flag
:
if
flag
:
flag
=
handler_sn
(
dst
,
flag
,
90
,
path
,
data
)
flag
=
handler_sn
(
dst
,
flag
,
90
,
path
,
data
,
connection
,
cursor
)
if
flag
:
if
flag
:
scale_factor
=
5
scale_factor
=
5
dst
=
original_image
.
resize
(
dst
=
original_image
.
resize
(
...
@@ -356,7 +368,7 @@ def handle_process(jsonData):
...
@@ -356,7 +368,7 @@ def handle_process(jsonData):
original_image
.
height
*
scale_factor
,
original_image
.
height
*
scale_factor
,
)
)
)
)
flag
=
handler_sn
(
dst
,
flag
,
0
,
path
,
data
)
flag
=
handler_sn
(
dst
,
flag
,
0
,
path
,
data
,
connection
,
cursor
)
if
flag
:
if
flag
:
# 定义要执行的SQL语句
# 定义要执行的SQL语句
SQL02
=
f
"""
SQL02
=
f
"""
...
@@ -366,9 +378,8 @@ def handle_process(jsonData):
...
@@ -366,9 +378,8 @@ def handle_process(jsonData):
# 执行SQL语句
# 执行SQL语句
log
.
info
(
SQL02
)
log
.
info
(
SQL02
)
SQL02
=
SQL02
.
replace
(
"'None',"
,
"null,"
)
.
replace
(
"None,"
,
"null,"
)
SQL02
=
SQL02
.
replace
(
"'None',"
,
"null,"
)
.
replace
(
"None,"
,
"null,"
)
cursor_dev
.
execute
(
SQL02
)
cursor
.
execute
(
SQL02
)
conn_dev
.
commit
()
connection
.
commit
()
log
.
info
(
"==========================处理vin=================================="
)
log
.
info
(
"==========================处理vin=================================="
)
...
@@ -389,16 +400,16 @@ def handle_process(jsonData):
...
@@ -389,16 +400,16 @@ def handle_process(jsonData):
(
original_image
.
width
*
scale_factor
,
original_image
.
height
*
scale_factor
)
(
original_image
.
width
*
scale_factor
,
original_image
.
height
*
scale_factor
)
)
)
flag
=
True
flag
=
True
flag
=
handler_vin
(
dst
,
flag
,
0
,
path
,
data
)
flag
=
handler_vin
(
dst
,
flag
,
0
,
path
,
data
,
connection
,
cursor
)
if
flag
:
if
flag
:
# 旋转45度宠重新识别
# 旋转45度宠重新识别
flag
=
handler_vin
(
dst
,
flag
,
45
,
path
,
data
)
flag
=
handler_vin
(
dst
,
flag
,
45
,
path
,
data
,
connection
,
cursor
)
if
flag
:
if
flag
:
# 旋转90度宠重新识别
# 旋转90度宠重新识别
flag
=
handler_vin
(
dst
,
flag
,
45
,
path
,
data
)
flag
=
handler_vin
(
dst
,
flag
,
45
,
path
,
data
,
connection
,
cursor
)
if
flag
:
if
flag
:
# 旋转180度宠重新识别
# 旋转180度宠重新识别
flag
=
handler_vin
(
dst
,
flag
,
90
,
path
,
data
)
flag
=
handler_vin
(
dst
,
flag
,
90
,
path
,
data
,
connection
,
cursor
)
if
flag
:
if
flag
:
# 如果识别不了 直接再放大图片
# 如果识别不了 直接再放大图片
scale_factor
=
5
scale_factor
=
5
...
@@ -408,7 +419,7 @@ def handle_process(jsonData):
...
@@ -408,7 +419,7 @@ def handle_process(jsonData):
original_image
.
height
*
scale_factor
,
original_image
.
height
*
scale_factor
,
)
)
)
)
flag
=
handler_vin
(
dst
,
flag
,
0
,
path
,
row
)
flag
=
handler_vin
(
dst
,
flag
,
0
,
path
,
row
,
connection
,
cursor
)
if
flag
:
if
flag
:
# 定义要执行的SQL语句
# 定义要执行的SQL语句
SQL02
=
f
"""
SQL02
=
f
"""
...
@@ -418,16 +429,21 @@ def handle_process(jsonData):
...
@@ -418,16 +429,21 @@ def handle_process(jsonData):
# 执行SQL语句
# 执行SQL语句
log
.
info
(
SQL02
)
log
.
info
(
SQL02
)
SQL02
=
SQL02
.
replace
(
"'None',"
,
"null,"
)
.
replace
(
"None,"
,
"null,"
)
SQL02
=
SQL02
.
replace
(
"'None',"
,
"null,"
)
.
replace
(
"None,"
,
"null,"
)
cursor_dev
.
execute
(
SQL02
)
cursor
.
execute
(
SQL02
)
conn_dev
.
commit
()
connection
.
commit
()
try
:
os
.
remove
(
path
)
os
.
remove
(
path
)
log
.
info
(
"文件删除成功"
)
log
.
info
(
"文件删除成功"
)
except
FileNotFoundError
:
except
FileNotFoundError
:
log
.
info
(
"文件不存在"
)
log
.
info
(
"文件不存在"
)
except
PermissionError
:
except
PermissionError
:
log
.
info
(
"权限不足,无法删除文件"
)
log
.
info
(
"权限不足,无法删除文件"
)
finally
:
log
.
info
(
"finally关闭连接"
)
if
'cursor'
in
locals
():
cursor
.
close
()
log
.
info
(
"关闭cursor"
)
def
consume_kafka
():
def
consume_kafka
():
log
.
info
(
"启动consume_kafka"
)
log
.
info
(
"启动consume_kafka"
)
...
@@ -437,13 +453,13 @@ def consume_kafka():
...
@@ -437,13 +453,13 @@ def consume_kafka():
if
msg
is
None
:
if
msg
is
None
:
continue
continue
# 处理逻辑
# 处理逻辑
start_time
=
time
.
time
()
handle_process
(
msg
)
handle_process
(
msg
)
end_time
=
time
.
time
()
log
.
info
(
"handle_process耗时:"
+
str
(
end_time
-
start_time
))
except
Exception
as
e
:
except
Exception
as
e
:
log
.
error
(
"ocr consumer Exception: "
+
str
(
e
))
log
.
error
(
"ocr consumer Exception: "
+
str
(
e
))
finally
:
finally
:
# 关闭消费者
# 关闭消费者
consumer
.
close
()
consumer
.
close
()
# 关闭光标对象
cursor_dev
.
close
()
# 关闭数据库连接
conn_dev
.
close
()
kafka_consumer/test03.py
0 → 100644
View file @
fd42a25
import
json
from
kafka
import
KafkaProducer
# 创建Kafka生产者
producer
=
KafkaProducer
(
bootstrap_servers
=
"192.168.3.131:9092"
)
# Kafka集群的地址
# 发送消息
topic
=
"ocr_topic"
# Kafka主题名称
key
=
b
"my-key"
# 消息的键(可选)
data
=
{
'orderId'
:
321
,
'fileId'
:
31
,
'filePath'
:
'https://bhg.didihu.com.cn/group1/M00/22/05/wKgFDWWnocCAZllpAADp9tDJibY696.jpg'
,
'sns'
:
'96312110851'
,
'vin'
:
'VIN654321'
}
message
=
json
.
dumps
(
data
)
.
encode
(
'utf-8'
)
producer
.
send
(
topic
,
message
)
# 等待所有消息发送完成
producer
.
flush
()
# 关闭生产者
producer
.
close
()
manage.py
View file @
fd42a25
# coding=utf-8
# coding=utf-8
from
flask
import
request
,
render_template
from
flask
import
request
,
render_template
import
threading
from
logger
import
log
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
...
@@ -64,7 +66,11 @@ def upload():
...
@@ -64,7 +66,11 @@ def upload():
print
(
"plate_number==="
,
plate_number
)
print
(
"plate_number==="
,
plate_number
)
return
plate_number
if
plate_number
is
not
None
else
"None"
return
plate_number
if
plate_number
is
not
None
else
"None"
def
main
():
kafka_thread
=
threading
.
Thread
(
target
=
ocr_consumer
.
consume_kafka
)
kafka_thread
.
daemon
=
True
# 将线程设置为守护线程,当主线程结束时,该线程也会自动结束
kafka_thread
.
start
()
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
ocr_consumer
.
consume_kafka
()
main
()
app
.
run
(
host
=
"0.0.0.0"
,
port
=
5000
)
app
.
run
(
host
=
"0.0.0.0"
,
port
=
5000
)
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