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,68 +289,8 @@ def handler_vin(dst, flag, angle, path, row, connection, cursor): ...@@ -289,68 +289,8 @@ def handler_vin(dst, flag, angle, path, row, connection, cursor):
return flag return flag
def handle_process(jsonData): def ocr_sn(dst, path, data, connection, cursor, original_image):
try: log.info("==========================处理sn==================================")
log.info("Received message handle_process: %s",jsonData)
row = json.loads(jsonData.value.decode('utf-8'))
order_id = row.get("orderId")
file_id = row.get("fileId")
sns = row.get("sns")
vin = row.get("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()
network_img_path = row.get("filePath")
log.info("network_img_path %s", network_img_path)
extension = get_extension(network_img_path)
complete_filename = f"{filename}.{extension}"
try:
response = requests.get(network_img_path)
except Exception as e:
response = requests.get(network_img_path)
path = os.path.join(
get_project_root(), "Identify_sn", "train", "threecode", complete_filename
)
dirname = os.path.dirname(path)
if not os.path.exists(dirname):
os.makedirs(dirname)
with open(path, "wb") as file:
file.write(response.content)
# urllib.request.urlretrieve(network_img_path, path)
time.sleep(0.001)
log.info("File downloaded successfully.")
original_image = Image.open(path)
if original_image.mode == "RGBA":
original_image = original_image.convert("RGB")
log.info(original_image.mode)
# 放大图片
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 = True
flag = handler_sn(dst, flag, 0, path, data ,connection,cursor) flag = handler_sn(dst, flag, 0, path, data ,connection,cursor)
...@@ -373,7 +313,7 @@ def handle_process(jsonData): ...@@ -373,7 +313,7 @@ def handle_process(jsonData):
# 定义要执行的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(),sysdate()) VALUES ({data[0]},{data[1]},'{data[2]}',null,sysdate(),sysdate())
""" """
# 执行SQL语句 # 执行SQL语句
log.info(SQL02) log.info(SQL02)
...@@ -381,6 +321,8 @@ def handle_process(jsonData): ...@@ -381,6 +321,8 @@ def handle_process(jsonData):
cursor.execute(SQL02) cursor.execute(SQL02)
connection.commit() connection.commit()
def ocr_vin(path, data, connection, cursor, original_image):
log.info("==========================处理vin==================================") log.info("==========================处理vin==================================")
# 创建对比度增强器 # 创建对比度增强器
...@@ -400,16 +342,16 @@ def handle_process(jsonData): ...@@ -400,16 +342,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,connection,cursor) flag = handler_vin(dst, flag, 0, path, data, connection, cursor)
if flag: if flag:
# 旋转45度宠重新识别 # 旋转45度宠重新识别
flag = handler_vin(dst, flag, 45, path, data,connection,cursor) flag = handler_vin(dst, flag, 45, path, data, connection, cursor)
if flag: if flag:
# 旋转90度宠重新识别 # 旋转90度宠重新识别
flag = handler_vin(dst, flag, 45, path, data,connection,cursor) flag = handler_vin(dst, flag, 45, path, data, connection, cursor)
if flag: if flag:
# 旋转180度宠重新识别 # 旋转180度宠重新识别
flag = handler_vin(dst, flag, 90, path, data,connection,cursor) flag = handler_vin(dst, flag, 90, path, data, connection, cursor)
if flag: if flag:
# 如果识别不了 直接再放大图片 # 如果识别不了 直接再放大图片
scale_factor = 5 scale_factor = 5
...@@ -419,19 +361,92 @@ def handle_process(jsonData): ...@@ -419,19 +361,92 @@ def handle_process(jsonData):
original_image.height * scale_factor, original_image.height * scale_factor,
) )
) )
flag = handler_vin(dst, flag, 0, path, row,connection,cursor) flag = handler_vin(dst, flag, 0, path, data, connection, cursor)
if flag: if flag:
# 定义要执行的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(),sysdate()) VALUES ({data[0]},{data[1]},'{data[3]}',null,sysdate(),sysdate())
""" """
# 执行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.execute(SQL02) cursor.execute(SQL02)
connection.commit() connection.commit()
def handle_process(jsonData):
try:
log.info("Received message handle_process: %s",jsonData)
row = json.loads(jsonData.value.decode('utf-8'))
order_id = row.get("orderId")
file_id = row.get("fileId")
sns = row.get("sns")
vin = row.get("vin")
file_type = row.get("fileType")
data = [order_id, file_id, sns, vin]
filename = generate_random_filename()
network_img_path = row.get("filePath")
log.info("network_img_path %s", network_img_path)
extension = get_extension(network_img_path)
complete_filename = f"{filename}.{extension}"
try:
response = requests.get(network_img_path)
except Exception as e:
response = requests.get(network_img_path)
path = os.path.join(
get_project_root(), "Identify_sn", "train", "threecode", complete_filename
)
dirname = os.path.dirname(path)
if not os.path.exists(dirname):
os.makedirs(dirname)
with open(path, "wb") as file:
file.write(response.content)
# urllib.request.urlretrieve(network_img_path, path)
time.sleep(0.001)
log.info("File downloaded successfully.")
original_image = Image.open(path)
if original_image.mode == "RGBA":
original_image = original_image.convert("RGB")
log.info(original_image.mode)
# 放大图片
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)
)
# 获取数据库连接
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()
ocr_vin(path, data, connection, cursor, 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) os.remove(path)
log.info("文件删除成功") log.info("文件删除成功")
except FileNotFoundError: except FileNotFoundError:
......
...@@ -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!