Commit f9a5c6b7 by 杨林龙

优化mysql 连接池连接

1 parent 164e893a
...@@ -15,7 +15,7 @@ import cv2 ...@@ -15,7 +15,7 @@ import cv2
import numpy as np import numpy as np
import pymysql import pymysql
import requests import requests
from paddleocr import PaddleOCR, draw_ocr from paddleocr import PaddleOCR
from PIL import Image from PIL import Image
from pyzbar.pyzbar import decode from pyzbar.pyzbar import decode
# 获取当前文件所在目录的绝对路径 # 获取当前文件所在目录的绝对路径
...@@ -59,10 +59,10 @@ connection_pool = pooling.MySQLConnectionPool( ...@@ -59,10 +59,10 @@ connection_pool = pooling.MySQLConnectionPool(
port=Config.port, port=Config.port,
database=Config.database, database=Config.database,
user=Config.user, user=Config.user,
password=Config.password password=Config.password,
autocommit=True,
) )
log.info("启动3") log.info("启动3")
# load model # load model
# Paddleocr目前支持中英文、英文、法语、德语、韩语、日语,可以通过修改 lang参数进行切换 # Paddleocr目前支持中英文、英文、法语、德语、韩语、日语,可以通过修改 lang参数进行切换
...@@ -104,8 +104,20 @@ log.info("启动4") ...@@ -104,8 +104,20 @@ log.info("启动4")
# 从连接池获取连接 # 从连接池获取连接
def get_connection(): def get_connection():
log.info("connection_pool size:" + str(connection_pool._cnx_queue.qsize()))
# 从连接池获取连接
return connection_pool.get_connection() 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(): def generate_random_filename():
timestamp = int(time.time()) # 获取当前时间戳 timestamp = int(time.time()) # 获取当前时间戳
random_number = random.randint(1000, 9999) # 生成一个随机数 random_number = random.randint(1000, 9999) # 生成一个随机数
...@@ -197,7 +209,7 @@ def flip_image(image,angle): ...@@ -197,7 +209,7 @@ def flip_image(image,angle):
return rotated_image return rotated_image
# 处理设备号 # 处理设备号
def handler_sn(dst, flag, angle, path, row, connection, cursor): def handler_sn(dst, flag, angle, path, row):
# 旋转角度 # 旋转角度
if angle>0: if angle>0:
dst = flip_image(dst, angle) dst = flip_image(dst, angle)
...@@ -230,12 +242,11 @@ def handler_sn(dst, flag, angle, path, row, connection, cursor): ...@@ -230,12 +242,11 @@ def handler_sn(dst, flag, angle, path, row, connection, cursor):
# 执行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.execute(SQL01) execute_sql(SQL01)
connection.commit()
return flag return flag
# 处理车架号 # 处理车架号
def handler_vin(dst, flag, angle, path, row, connection, cursor): def handler_vin(dst, flag, angle, path, row):
if angle > 0: if angle > 0:
dst = flip_image(dst, angle) dst = flip_image(dst, angle)
...@@ -272,22 +283,21 @@ def handler_vin(dst, flag, angle, path, row, connection, cursor): ...@@ -272,22 +283,21 @@ def handler_vin(dst, flag, angle, path, row, connection, cursor):
# 执行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.execute(SQL01) execute_sql(SQL01)
connection.commit()
return flag return flag
def ocr_sn(dst, path, data, connection, cursor, original_image): def ocr_sn(dst, path, data, original_image):
log.info("==========================处理sn==================================") log.info("==========================处理sn==================================")
flag = True flag = True
flag = handler_sn(dst, flag, 0, path, data ,connection,cursor) flag = handler_sn(dst, flag, 0, path, data)
if flag: if flag:
flag = handler_sn(dst, flag, 45, path, data,connection,cursor) flag = handler_sn(dst, flag, 45, path, data)
if flag: if flag:
flag = handler_sn(dst, flag, 45, path, data,connection,cursor) flag = handler_sn(dst, flag, 45, path, data)
if flag: if flag:
flag = handler_sn(dst, flag, 90, path, data,connection,cursor) flag = handler_sn(dst, flag, 90, path, data)
if flag: if flag:
scale_factor = 5 scale_factor = 5
dst = original_image.resize( dst = original_image.resize(
...@@ -296,7 +306,7 @@ def ocr_sn(dst, path, data, connection, cursor, original_image): ...@@ -296,7 +306,7 @@ def ocr_sn(dst, path, data, connection, cursor, original_image):
original_image.height * scale_factor, 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: if flag:
# 定义要执行的SQL语句 # 定义要执行的SQL语句
SQL02 = f""" SQL02 = f"""
...@@ -306,11 +316,10 @@ def ocr_sn(dst, path, data, connection, cursor, original_image): ...@@ -306,11 +316,10 @@ def ocr_sn(dst, path, data, connection, cursor, original_image):
# 执行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) execute_sql(SQL02)
connection.commit()
def ocr_vin(path, data, connection, cursor, original_image): def ocr_vin(path, data, original_image):
log.info("==========================处理vin==================================") log.info("==========================处理vin==================================")
# 创建对比度增强器 # 创建对比度增强器
...@@ -330,16 +339,16 @@ def ocr_vin(path, data, connection, cursor, original_image): ...@@ -330,16 +339,16 @@ def ocr_vin(path, data, connection, cursor, original_image):
(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)
if flag: if flag:
# 旋转45度宠重新识别 # 旋转45度宠重新识别
flag = handler_vin(dst, flag, 45, path, data, connection, cursor) flag = handler_vin(dst, flag, 45, path, data)
if flag: if flag:
# 旋转90度宠重新识别 # 旋转90度宠重新识别
flag = handler_vin(dst, flag, 45, path, data, connection, cursor) flag = handler_vin(dst, flag, 45, path, data)
if flag: if flag:
# 旋转180度宠重新识别 # 旋转180度宠重新识别
flag = handler_vin(dst, flag, 90, path, data, connection, cursor) flag = handler_vin(dst, flag, 90, path, data)
if flag: if flag:
# 如果识别不了 直接再放大图片 # 如果识别不了 直接再放大图片
scale_factor = 5 scale_factor = 5
...@@ -349,7 +358,7 @@ def ocr_vin(path, data, connection, cursor, original_image): ...@@ -349,7 +358,7 @@ def ocr_vin(path, data, connection, cursor, original_image):
original_image.height * scale_factor, 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: if flag:
# 定义要执行的SQL语句 # 定义要执行的SQL语句
SQL02 = f""" SQL02 = f"""
...@@ -359,9 +368,7 @@ def ocr_vin(path, data, connection, cursor, original_image): ...@@ -359,9 +368,7 @@ def ocr_vin(path, data, connection, cursor, original_image):
# 执行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) execute_sql(SQL02)
connection.commit()
def handle_process(jsonData): def handle_process(jsonData):
...@@ -414,46 +421,34 @@ def handle_process(jsonData): ...@@ -414,46 +421,34 @@ def handle_process(jsonData):
(original_image.width * scale_factor, original_image.height * scale_factor) (original_image.width * scale_factor, original_image.height * scale_factor)
) )
# 获取数据库连接
connection = get_connection()
cursor = connection.cursor()
if file_type == 0: if file_type == 0:
# 删除数据库里的vin旧数据 # 删除数据库里的vin旧数据
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.execute(delete_sql2) execute_sql(delete_sql2)
connection.commit()
# 删除数据库里的sn旧数据 # 删除数据库里的sn旧数据
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.execute(delete_sql1) execute_sql(delete_sql1)
connection.commit() # 识别vin
#识别vin ocr_vin(path, data, original_image)
ocr_vin(path, data, connection, cursor, original_image) # 识别sn
#识别sn ocr_sn(dst, path, data, original_image)
ocr_sn(dst, path, data, connection, cursor, original_image)
if file_type == 1: if file_type == 1:
# 删除数据库里的sn旧数据 # 删除数据库里的sn旧数据
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.execute(delete_sql1) execute_sql(delete_sql1)
connection.commit() ocr_sn(dst, path, data,original_image)
ocr_sn(dst, path, data, connection, cursor, original_image)
os.remove(path)
log.info("文件删除成功")
except FileNotFoundError: except FileNotFoundError:
log.info("文件不存在") log.info("文件不存在")
except PermissionError: except PermissionError:
log.info("权限不足,无法删除文件") log.info("权限不足,无法删除文件")
finally: finally:
os.remove(path)
log.info("文件删除成功")
log.info("finally关闭连接") log.info("finally关闭连接")
if 'cursor' in locals():
cursor.close()
log.info("关闭cursor")
def consume_kafka(): def consume_kafka():
...@@ -467,10 +462,9 @@ def consume_kafka(): ...@@ -467,10 +462,9 @@ def consume_kafka():
start_time = time.time() start_time = time.time()
handle_process(msg) handle_process(msg)
end_time = time.time() 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: except Exception as e:
log.error("ocr consumer Exception: " + str(e)) log.error("ocr consumer Exception: " + str(e))
finally: finally:
# 关闭消费者 # 关闭消费者
consumer.close() consumer.close()
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!