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 58500fcf
authored
Feb 26, 2024
by
周杭
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
ocrConsumer功能
1 parent
3a4822db
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
27 additions
and
42 deletions
config_dev.py
config_pro.py
config_test.py
kafka_consumer/orc_consumer.py
config_dev.py
View file @
58500fc
...
@@ -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
=
'192.168.3.131:9092'
kafka_consumer_bootstrap_servers
=
'192.168.3.131:9092'
kafka_consumer_group_id
=
'ocr_consumer_dev'
kafka_consumer_group_id
=
'ocr_consumer_dev'
kafka_consumer_auto_offset_reset
=
'earliest'
kafka_consumer_auto_offset_reset
=
'earliest'
kafka_consumer_
subscribe
=
[
'ocr_topic'
]
kafka_consumer_
topic
=
'ocr_topic'
charset
=
'utf8'
charset
=
'utf8'
#
#
config_pro.py
View file @
58500fc
...
@@ -11,6 +11,6 @@ class Config:
...
@@ -11,6 +11,6 @@ class Config:
kafka_consumer_bootstrap_servers
=
'localhost:9092'
kafka_consumer_bootstrap_servers
=
'localhost:9092'
kafka_consumer_group_id
=
'my_consumer_group'
kafka_consumer_group_id
=
'my_consumer_group'
kafka_consumer_auto_offset_reset
=
'earliest'
kafka_consumer_auto_offset_reset
=
'earliest'
kafka_consumer_
subscribe
=
[
'my_topic'
]
kafka_consumer_
topic
=
'ocr_topic'
charset
=
'utf8'
charset
=
'utf8'
#
#
config_test.py
View file @
58500fc
...
@@ -11,6 +11,6 @@ class Config:
...
@@ -11,6 +11,6 @@ class Config:
kafka_consumer_bootstrap_servers
=
'192.168.3.131:9092'
kafka_consumer_bootstrap_servers
=
'192.168.3.131:9092'
kafka_consumer_group_id
=
'ocr_consumer_test'
kafka_consumer_group_id
=
'ocr_consumer_test'
kafka_consumer_auto_offset_reset
=
'earliest'
kafka_consumer_auto_offset_reset
=
'earliest'
kafka_consumer_
subscribe
=
[
'ocr_topic'
]
kafka_consumer_
topic
=
'ocr_topic'
charset
=
'utf8'
charset
=
'utf8'
#
#
kafka_consumer/orc_consumer.py
View file @
58500fc
import
json
import
json
from
confluent_kafka
import
Consumer
,
KafkaException
,
KafkaErro
r
from
kafka
import
KafkaConsume
r
import
sys
import
sys
import
logging
import
logging
# 导入pymysql模块
# 导入pymysql模块
...
@@ -33,17 +33,12 @@ else:
...
@@ -33,17 +33,12 @@ else:
log
.
info
(
vars
(
Config
))
log
.
info
(
vars
(
Config
))
# 配置 Kafka 消费者
# 配置 Kafka 消费者
conf
=
{
consumer
=
KafkaConsumer
(
'bootstrap.servers'
:
Config
.
kafka_consumer_bootstrap_servers
,
# Kafka broker 的地址
Config
.
kafka_consumer_topic
,
'group.id'
:
Config
.
kafka_consumer_group_id
,
# 消费者组 ID
bootstrap_servers
=
Config
.
kafka_consumer_bootstrap_servers
,
# Kafka broker 的地址
'auto.offset.reset'
:
Config
.
kafka_consumer_auto_offset_reset
# 从最早的消息开始消费
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
# 连接database
conn_dev
=
pymysql
.
connect
(
conn_dev
=
pymysql
.
connect
(
...
@@ -177,7 +172,7 @@ def extract_vin(string):
...
@@ -177,7 +172,7 @@ def extract_vin(string):
def
flip_image
(
image
,
angle
):
def
flip_image
(
image
,
angle
):
# 逆时针旋转45度
# 逆时针旋转45度
log
.
info
(
"逆时针旋转"
+
angle
+
"度"
)
log
.
info
(
"逆时针旋转"
+
str
(
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
(
angle
)
rotated_image
=
image
.
rotate
(
angle
)
...
@@ -218,7 +213,7 @@ def handler_sn(dst, flag, angle, path, row):
...
@@ -218,7 +213,7 @@ def handler_sn(dst, flag, angle, path, row):
# 定义要执行的SQL语句
# 定义要执行的SQL语句
SQL01
=
f
"""
SQL01
=
f
"""
INSERT INTO sys_order_ocr_sn_result (order_id,file_id,db_sn,ocr_sn,create_time,update_time)
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(),sysd
d
ate())
VALUES ({row[0]},{row[1]},'{row[2]}','{sn_str}',sysdate(),sysdate())
"""
"""
# 执行SQL语句
# 执行SQL语句
log
.
info
(
SQL01
)
log
.
info
(
SQL01
)
...
@@ -242,7 +237,7 @@ def handler_vin(dst, flag, angle, path,row):
...
@@ -242,7 +237,7 @@ def handler_vin(dst, flag, angle, path,row):
# log.info(line[0][1][0])
# log.info(line[0][1][0])
result_list
=
flatten_list
(
result
)
result_list
=
flatten_list
(
result
)
log
.
info
(
"result_list"
,
result_list
)
log
.
info
(
"result_list
%
s
"
,
result_list
)
with
open
(
"./log10.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
with
open
(
"./log10.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
"
)
...
@@ -265,7 +260,7 @@ def handler_vin(dst, flag, angle, path,row):
...
@@ -265,7 +260,7 @@ def handler_vin(dst, flag, angle, path,row):
# 定义要执行的SQL语句
# 定义要执行的SQL语句
SQL01
=
f
"""
SQL01
=
f
"""
INSERT INTO sys_order_ocr_vin_result (order_id,file_id,db_vin,ocr_vin,create_time,update_time)
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(),sysd
d
ate())
VALUES ({row[0]},{row[1]},'{row[3]}','{vin_str}',sysdate(),sysdate())
"""
"""
# 执行SQL语句
# 执行SQL语句
log
.
info
(
SQL01
)
log
.
info
(
SQL01
)
...
@@ -274,9 +269,9 @@ def handler_vin(dst, flag, angle, path,row):
...
@@ -274,9 +269,9 @@ def handler_vin(dst, flag, angle, path,row):
return
flag
return
flag
def
handle_process
(
row
):
def
handle_process
(
jsonData
):
log
.
info
(
"Received message handle_process:
{}"
,
row
)
log
.
info
(
"Received message handle_process:
%
s"
,
jsonData
)
row
=
json
.
loads
(
jsonData
.
value
.
decode
(
'utf-8'
))
order_id
=
row
.
get
(
"orderId"
)
order_id
=
row
.
get
(
"orderId"
)
file_id
=
row
.
get
(
"fileId"
)
file_id
=
row
.
get
(
"fileId"
)
sns
=
row
.
get
(
"sns"
)
sns
=
row
.
get
(
"sns"
)
...
@@ -296,7 +291,7 @@ def handle_process(row):
...
@@ -296,7 +291,7 @@ def handle_process(row):
filename
=
generate_random_filename
()
filename
=
generate_random_filename
()
network_img_path
=
row
.
get
(
"filePath"
)
network_img_path
=
row
.
get
(
"filePath"
)
log
.
info
(
"network_img_path"
,
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}"
try
:
try
:
...
@@ -354,7 +349,7 @@ def handle_process(row):
...
@@ -354,7 +349,7 @@ def handle_process(row):
# 定义要执行的SQL语句
# 定义要执行的SQL语句
SQL02
=
f
"""
SQL02
=
f
"""
INSERT INTO sys_order_ocr_sn_result (order_id,file_id,db_sn,ocr_sn,create_time,update_time)
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(),sysd
d
ate())
VALUES ({order_id},{file_id},'{sns}',null,sysdate(),sysdate())
"""
"""
# 执行SQL语句
# 执行SQL语句
log
.
info
(
SQL02
)
log
.
info
(
SQL02
)
...
@@ -405,7 +400,7 @@ def handle_process(row):
...
@@ -405,7 +400,7 @@ def handle_process(row):
# 定义要执行的SQL语句
# 定义要执行的SQL语句
SQL02
=
f
"""
SQL02
=
f
"""
INSERT INTO sys_order_ocr_vin_result (order_id,file_id,db_vin,ocr_vin,create_time,update_time)
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(),sysd
d
ate())
VALUES ({order_id},{file_id},'{vin}',null,sysdate(),sysdate())
"""
"""
# 执行SQL语句
# 执行SQL语句
log
.
info
(
SQL02
)
log
.
info
(
SQL02
)
...
@@ -423,23 +418,13 @@ def handle_process(row):
...
@@ -423,23 +418,13 @@ def handle_process(row):
# 消费消息并进行逻辑处理
# 消费消息并进行逻辑处理
try
:
try
:
while
True
:
for
msg
in
consumer
:
msg
=
consumer
.
poll
(
timeout
=
1.0
)
if
msg
is
None
:
if
msg
is
None
:
continue
continue
if
msg
.
error
():
if
msg
.
error
()
.
code
()
==
KafkaError
.
_PARTITION_EOF
:
continue
else
:
log
.
info
(
msg
.
error
())
break
# 处理逻辑
# 处理逻辑
handle_process
(
json
.
loads
(
msg
.
value
()
.
decode
(
'utf-8'
)))
handle_process
(
msg
)
except
Exception
as
e
:
except
KeyboardInterrupt
:
log
.
error
(
"ocr consumer Exception: "
+
str
(
e
))
sys
.
stderr
.
write
(
'
%%
Aborted by user
\n
'
)
finally
:
finally
:
# 关闭消费者
# 关闭消费者
consumer
.
close
()
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