Commit 18681d96 by 周杭

使用stream形式下载图片

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