Commit 18681d96 by 周杭

使用stream形式下载图片

1 parent 621b5e7b
......@@ -121,8 +121,10 @@ def execute_sql(sql):
except Exception as msg:
log.error("sql执行Exception: " + str(msg))
finally:
cursor.close()
conn.close()
if cursor is not None:
cursor.close()
if conn is not None:
conn.close()
def generate_random_filename():
......@@ -216,7 +218,7 @@ def flip_image(image,angle):
return rotated_image
# 处理设备号
def handler_sn(dst, flag, angle, path, row):
def handler_sn(dst, flag, angle, row):
# 旋转角度
if angle>0:
dst = flip_image(dst, angle)
......@@ -253,7 +255,7 @@ def handler_sn(dst, flag, angle, path, row):
return flag
# 处理车架号
def handler_vin(dst, flag, angle, path, row):
def handler_vin(dst, flag, angle, row):
if angle > 0:
dst = flip_image(dst, angle)
......@@ -294,17 +296,17 @@ def handler_vin(dst, flag, angle, path, row):
return flag
def ocr_sn(path, data, original_image):
def ocr_sn(data, original_image):
log.info("==========================处理sn==================================")
flag = True
flag = handler_sn(original_image, flag, 0, path, data)
flag = handler_sn(original_image, flag, 0, data)
if flag:
flag = handler_sn(original_image, flag, 45, path, data)
flag = handler_sn(original_image, flag, 45, data)
if flag:
flag = handler_sn(original_image, flag, 45, path, data)
flag = handler_sn(original_image, flag, 45, data)
if flag:
flag = handler_sn(original_image, flag, 90, path, data)
flag = handler_sn(original_image, flag, 90, data)
# if flag:
# scale_factor = 5
# dst = original_image.resize(
......@@ -326,7 +328,7 @@ def ocr_sn(path, data, original_image):
execute_sql(SQL02)
def ocr_vin(path, data, original_image):
def ocr_vin(data, original_image):
log.info("==========================处理vin==================================")
# 创建对比度增强器
......@@ -346,16 +348,16 @@ def ocr_vin(path, data, original_image):
# (original_image.width * scale_factor, original_image.height * scale_factor)
# )
flag = True
flag = handler_vin(original_image, flag, 0, path, data)
flag = handler_vin(original_image, flag, 0, data)
if flag:
# 旋转45度宠重新识别
flag = handler_vin(original_image, flag, 45, path, data)
flag = handler_vin(original_image, flag, 45, data)
if flag:
# 旋转90度宠重新识别
flag = handler_vin(original_image, flag, 45, path, data)
flag = handler_vin(original_image, flag, 45, data)
if flag:
# 旋转180度宠重新识别
flag = handler_vin(original_image, flag, 90, path, data)
flag = handler_vin(original_image, flag, 90, data)
# if flag:
# # 如果识别不了 直接再放大图片
# scale_factor = 5
......@@ -390,34 +392,31 @@ def handle_process(jsonData):
data = [order_id, file_id, sns, vin]
filename = generate_random_filename()
# 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}"
path = None
# complete_filename = f"{filename}.{extension}"
# path = None
if extension.lower() not in supported_formats:
log.info(f"跳过文件 {network_img_path},因为它的格式 {extension} 不受支持")
return
try:
response = requests.get(network_img_path)
except Exception as e:
log.info("图片下载Exception:"+str(e))
return
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.")
# try:
# response = requests.get(network_img_path)
# except Exception as e:
# log.info("图片下载Exception:"+str(e))
# return
# 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)
# time.sleep(0.001)
# log.info("File downloaded successfully.")
# # 获取文件大小(字节)
# file_size_bytes = os.path.getsize(path)
......@@ -430,7 +429,8 @@ def handle_process(jsonData):
# log.info("Skipping because it's larger than 3MB.")
# return
original_image = Image.open(path)
# original_image = Image.open(path)
original_image = Image.open(requests.get(network_img_path, stream=True).raw)
if original_image.mode == "RGBA":
original_image = original_image.convert("RGB")
......@@ -457,22 +457,19 @@ def handle_process(jsonData):
log.info(delete_sql1)
execute_sql(delete_sql1)
# 识别vin
ocr_vin(path, data, original_image)
ocr_vin(data, original_image)
# 识别sn
ocr_sn(path, data, original_image)
ocr_sn(data, 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)
execute_sql(delete_sql1)
ocr_sn(path, data,original_image)
ocr_sn(data,original_image)
except Exception as e:
log.error("handle_process Exception: " + str(e))
finally:
if path:
os.remove(path)
log.info("文件删除成功")
log.info("finally关闭连接")
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!