Commit 018a16a4 by 周杭

分开识别

1 parent fd42a258
...@@ -46,7 +46,7 @@ consumer = KafkaConsumer( ...@@ -46,7 +46,7 @@ consumer = KafkaConsumer(
Config.kafka_consumer_topic, Config.kafka_consumer_topic,
bootstrap_servers=Config.kafka_consumer_bootstrap_servers, # Kafka broker 的地址 bootstrap_servers=Config.kafka_consumer_bootstrap_servers, # Kafka broker 的地址
group_id=Config.kafka_consumer_group_id, # 消费者组 ID group_id=Config.kafka_consumer_group_id, # 消费者组 ID
auto_offset_reset=Config.kafka_consumer_auto_offset_reset # 从最早的消息开始消费 auto_offset_reset=Config.kafka_consumer_auto_offset_reset, # 从最早的消息开始消费
) )
log.info("启动2") log.info("启动2")
...@@ -289,6 +289,93 @@ def handler_vin(dst, flag, angle, path, row, connection, cursor): ...@@ -289,6 +289,93 @@ def handler_vin(dst, flag, angle, path, row, connection, cursor):
return flag return flag
def ocr_sn(dst, path, data, connection, cursor, original_image):
log.info("==========================处理sn==================================")
flag = True
flag = handler_sn(dst, flag, 0, path, data ,connection,cursor)
if flag:
flag = handler_sn(dst, flag, 45, path, data,connection,cursor)
if flag:
flag = handler_sn(dst, flag, 45, path, data,connection,cursor)
if flag:
flag = handler_sn(dst, flag, 90, path, data,connection,cursor)
if flag:
scale_factor = 5
dst = original_image.resize(
(
original_image.width * scale_factor,
original_image.height * scale_factor,
)
)
flag = handler_sn(dst, flag, 0, path, data,connection,cursor)
if flag:
# 定义要执行的SQL语句
SQL02 = f"""
INSERT INTO sys_order_ocr_sn_result (order_id,file_id,db_sn,ocr_sn,create_time,update_time)
VALUES ({data[0]},{data[1]},'{data[2]}',null,sysdate(),sysdate())
"""
# 执行SQL语句
log.info(SQL02)
SQL02=SQL02.replace("'None',", "null,").replace("None,", "null,")
cursor.execute(SQL02)
connection.commit()
def ocr_vin(path, data, connection, cursor, original_image):
log.info("==========================处理vin==================================")
# 创建对比度增强器
# enhancer = ImageEnhance.Contrast(original_image)
# 增强对比度(将对比度增强为原来的2倍)
# factor = 2.0
# original_image = enhancer.enhance(factor)
# 放大图片
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)
)
flag = True
flag = handler_vin(dst, flag, 0, path, data, connection, cursor)
if flag:
# 旋转45度宠重新识别
flag = handler_vin(dst, flag, 45, path, data, connection, cursor)
if flag:
# 旋转90度宠重新识别
flag = handler_vin(dst, flag, 45, path, data, connection, cursor)
if flag:
# 旋转180度宠重新识别
flag = handler_vin(dst, flag, 90, path, data, connection, cursor)
if flag:
# 如果识别不了 直接再放大图片
scale_factor = 5
dst = original_image.resize(
(
original_image.width * scale_factor,
original_image.height * scale_factor,
)
)
flag = handler_vin(dst, flag, 0, path, data, connection, cursor)
if flag:
# 定义要执行的SQL语句
SQL02 = f"""
INSERT INTO sys_order_ocr_vin_result (order_id,file_id,db_vin,ocr_vin,create_time,update_time)
VALUES ({data[0]},{data[1]},'{data[3]}',null,sysdate(),sysdate())
"""
# 执行SQL语句
log.info(SQL02)
SQL02 = SQL02.replace("'None',", "null,").replace("None,", "null,")
cursor.execute(SQL02)
connection.commit()
def handle_process(jsonData): def handle_process(jsonData):
try: try:
log.info("Received message handle_process: %s",jsonData) log.info("Received message handle_process: %s",jsonData)
...@@ -297,22 +384,10 @@ def handle_process(jsonData): ...@@ -297,22 +384,10 @@ def handle_process(jsonData):
file_id = row.get("fileId") file_id = row.get("fileId")
sns = row.get("sns") sns = row.get("sns")
vin = row.get("vin") vin = row.get("vin")
file_type = row.get("fileType")
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)
log.info(delete_sql1)
cursor.execute(delete_sql1)
connection.commit()
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()
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 %s", network_img_path) log.info("network_img_path %s", network_img_path)
...@@ -351,86 +426,26 @@ def handle_process(jsonData): ...@@ -351,86 +426,26 @@ 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 = handler_sn(dst, flag, 0, path, data ,connection,cursor) connection = get_connection()
cursor = connection.cursor()
if flag:
flag = handler_sn(dst, flag, 45, path, data,connection,cursor)
if flag:
flag = handler_sn(dst, flag, 45, path, data,connection,cursor)
if flag:
flag = handler_sn(dst, flag, 90, path, data,connection,cursor)
if flag:
scale_factor = 5
dst = original_image.resize(
(
original_image.width * scale_factor,
original_image.height * scale_factor,
)
)
flag = handler_sn(dst, flag, 0, path, data,connection,cursor)
if flag:
# 定义要执行的SQL语句
SQL02 = f"""
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(),sysdate())
"""
# 执行SQL语句
log.info(SQL02)
SQL02=SQL02.replace("'None',", "null,").replace("None,", "null,")
cursor.execute(SQL02)
connection.commit()
log.info("==========================处理vin==================================")
# 创建对比度增强器
# enhancer = ImageEnhance.Contrast(original_image)
# 增强对比度(将对比度增强为原来的2倍)
# factor = 2.0
# original_image = enhancer.enhance(factor)
# 放大图片 if file_type == 0:
scale_factor = 3 # 放大倍数 # 删除数据库里的vin旧数据
# scaled_image = original_image.resize( delete_sql2 = "DELETE FROM sys_order_ocr_vin_result WHERE order_id = {}".format(order_id)
# (original_image.width * scale_factor, original_image.height * scale_factor) log.info(delete_sql2)
# ) cursor.execute(delete_sql2)
connection.commit()
ocr_vin(path, data, connection, cursor, original_image)
dst = original_image.resize( if file_type == 1:
(original_image.width * scale_factor, original_image.height * scale_factor) # 删除数据库里的sn旧数据
) delete_sql1 = "DELETE FROM sys_order_ocr_sn_result WHERE order_id = {}".format(order_id)
flag = True log.info(delete_sql1)
flag = handler_vin(dst, flag, 0, path, data,connection,cursor) cursor.execute(delete_sql1)
if flag:
# 旋转45度宠重新识别
flag = handler_vin(dst, flag, 45, path, data,connection,cursor)
if flag:
# 旋转90度宠重新识别
flag = handler_vin(dst, flag, 45, path, data,connection,cursor)
if flag:
# 旋转180度宠重新识别
flag = handler_vin(dst, flag, 90, path, data,connection,cursor)
if flag:
# 如果识别不了 直接再放大图片
scale_factor = 5
dst = original_image.resize(
(
original_image.width * scale_factor,
original_image.height * scale_factor,
)
)
flag = handler_vin(dst, flag, 0, path, row,connection,cursor)
if flag:
# 定义要执行的SQL语句
SQL02 = f"""
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(),sysdate())
"""
# 执行SQL语句
log.info(SQL02)
SQL02=SQL02.replace("'None',", "null,").replace("None,", "null,")
cursor.execute(SQL02)
connection.commit() connection.commit()
ocr_sn(dst, path, data, connection, cursor, original_image)
os.remove(path) os.remove(path)
log.info("文件删除成功") log.info("文件删除成功")
......
...@@ -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': 321, 'fileId': 31,'filePath':'https://bhg.didihu.com.cn/group1/M00/22/05/wKgFDWWnocCAZllpAADp9tDJibY696.jpg','sns':'96312110851','vin':'VIN654321'} data = {'orderId': 1234, 'fileId': 31,'filePath':'https://bhg.didihu.com.cn/group1/M00/22/05/wKgFDWWnocCAZllpAADp9tDJibY696.jpg','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)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!