Commit 1e8314d6 by 杨林龙

优化OCR识别效率

1 parent 14ef8b0f
......@@ -318,7 +318,7 @@ for index, row in enumerate(result):
conn_dev.commit()
else:
# 如果识别不了 直接再放大图片
scale_factor = 10
scale_factor = 5
dst = original_image.resize(
(
original_image.width * scale_factor,
......
......@@ -12,6 +12,7 @@ import pymysql
import requests
from paddleocr import PaddleOCR, draw_ocr
from PIL import Image
from PIL import ImageEnhance
from pyzbar.pyzbar import decode
# 连接database
......@@ -75,8 +76,8 @@ ocr = PaddleOCR(
# 定义要执行的SQL语句
SQL = """
SELECT b.* FROM sys_vin_check_ocr_01 a LEFT JOIN sys_order_file_ocr b ON b.id = a.file_id
WHERE a.ocr_vin IS NULL ;
SELECT b.* FROM sys_vin_check_ocr_06 a LEFT JOIN sys_order_file_ocr b ON b.id = a.file_id
WHERE db_vin NOT LIKE '%*%' AND ocr_vin != db_vin OR ocr_vin IS NULL;
"""
# 执行SQL语句
cursor_dev.execute(SQL)
......@@ -139,16 +140,39 @@ def validate_vin(item):
def extract_vin(string):
string = re.sub(r"O", "0", string)
string = re.sub(r"I", "1", string)
# pattern = r"(?=.*[0-9A-HJ-NPR-Z])(?!(.*[IOQ]))([0-9A-HJ-NPR-Z]{17})"
pattern = r"(?=.*[0-9A-HJ-NPR-Z])(?!(.*[IOQ]))([0-9A-HJ-NPR-Z\s\u4e00-\u9fa5]{17})"
pattern = r"(?=.*[0-9A-HJ-NPR-Z])(?!(.*[IOQ]))([0-9A-HJ-NPR-Z]{17})"
# pattern = r"(?=.*[0-9A-HJ-NPR-Z])(?!(.*[IOQ]))([0-9A-HJ-NPR-Z\s\u4e00-\u9fa5]{17})"
# pattern = r"[0-9A-HJ-NPR-Z]{17}"
# pattern = r"^[A-HJ-NPR-Z\d]{17}$"
match = re.search(pattern, string)
if match:
vin = match.group()
return vin
pattern01 = r"^\d+$"
match = re.match(pattern01, vin)
# 如果全是数字 则不是车架号
if bool(match):
return None
else:
return vin
else:
return None
def flip_image_90(image):
# 逆时针旋转90度
print("逆时针旋转90度")
# rotated_image = cv2.transpose(image)
# rotated_image = cv2.flip(rotated_image, 0)
rotated_image = image.rotate(90)
return rotated_image
def flip_image_180(image):
# 旋转180度
rotated_image = cv2.flip(image, -1)
return rotated_image
# 下载图片数据
for index, row in enumerate(result):
print(row)
......@@ -172,6 +196,13 @@ for index, row in enumerate(result):
original_image = Image.open(path)
# 创建对比度增强器
# enhancer = ImageEnhance.Contrast(original_image)
# 增强对比度(将对比度增强为原来的2倍)
# factor = 2.0
# original_image = enhancer.enhance(factor)
# 放大图片
scale_factor = 3 # 放大倍数
# scaled_image = original_image.resize(
......@@ -190,7 +221,7 @@ for index, row in enumerate(result):
result_list = flatten_list(result)
print("result_list", result_list)
with open("./log08.txt", "a+", encoding="utf-8") as file:
with open("./log11.txt", "a+", encoding="utf-8") as file:
if result_list is not None:
file.write(path + "----" + str(result_list) + "\n")
else:
......@@ -210,7 +241,7 @@ for index, row in enumerate(result):
print(vin_str)
# 定义要执行的SQL语句
SQL01 = f"""
INSERT INTO sys_vin_check_ocr_03 (order_id,file_id,db_vin,ocr_vin,create_time)
INSERT INTO sys_vin_check_ocr_12 (order_id,file_id,db_vin,ocr_vin,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}','{vin_str}',sysdate())
"""
# 执行SQL语句
......@@ -218,15 +249,125 @@ for index, row in enumerate(result):
cursor_dev.execute(SQL01)
conn_dev.commit()
else:
# 定义要执行的SQL语句
SQL02 = f"""
INSERT INTO sys_vin_check_ocr_03 (order_id,file_id,db_vin,ocr_vin,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}',null,sysdate())
"""
# 执行SQL语句
print(SQL02)
cursor_dev.execute(SQL02)
conn_dev.commit()
# 旋转90度宠重新识别
dst = flip_image_90(dst)
result = ocr.ocr(np.asarray(dst), cls=True)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for line in result:
print(line)
# print(line[0][1][0])
result_list = flatten_list(result)
print("result_list", result_list)
with open("./log11.txt", "a+", encoding="utf-8") as file:
if result_list is not None:
file.write(path + "----" + str(result_list) + "\n")
else:
file.write(path + "----" + "NULL" + "\n")
vin_str = ""
for item in result_list:
print(item)
vin_check = extract_vin(item)
if vin_check is not None:
vin_str = vin_check
break
if vin_str:
print(str(row))
print(vin_str)
# 定义要执行的SQL语句
SQL01 = f"""
INSERT INTO sys_vin_check_ocr_12 (order_id,file_id,db_vin,ocr_vin,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}','{vin_str}',sysdate())
"""
# 执行SQL语句
print(SQL01)
cursor_dev.execute(SQL01)
conn_dev.commit()
else:
dst = flip_image_90(dst)
result = ocr.ocr(np.asarray(dst), cls=True)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for line in result:
print(line)
# print(line[0][1][0])
result_list = flatten_list(result)
print("result_list", result_list)
with open("./log11.txt", "a+", encoding="utf-8") as file:
if result_list is not None:
file.write(path + "----" + str(result_list) + "\n")
else:
file.write(path + "----" + "NULL" + "\n")
vin_str = ""
for item in result_list:
print(item)
vin_check = extract_vin(item)
if vin_check is not None:
vin_str = vin_check
break
if vin_str:
print(str(row))
print(vin_str)
# 定义要执行的SQL语句
SQL01 = f"""
INSERT INTO sys_vin_check_ocr_12 (order_id,file_id,db_vin,ocr_vin,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}','{vin_str}',sysdate())
"""
# 执行SQL语句
print(SQL01)
cursor_dev.execute(SQL01)
conn_dev.commit()
else:
# 如果识别不了 直接再放大图片
scale_factor = 5
dst = original_image.resize(
(
original_image.width * scale_factor,
original_image.height * scale_factor,
)
)
result = ocr.ocr(np.asarray(dst), cls=True)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for line in result:
print(line)
# print(line[0][1][0])
result_list = flatten_list(result)
print("result_list", result_list)
with open("./log11.txt", "a+", encoding="utf-8") as file:
if result_list is not None:
file.write(path + "----" + str(result_list) + "\n")
else:
file.write(path + "----" + "NULL" + "\n")
vin_str = ""
for item in result_list:
print(item)
vin_check = extract_vin(item)
if vin_check is not None:
vin_str = vin_check
break
if vin_str:
print(str(row))
print(vin_str)
# 定义要执行的SQL语句
SQL01 = f"""
INSERT INTO sys_vin_check_ocr_12 (order_id,file_id,db_vin,ocr_vin,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}','{vin_str}',sysdate())
"""
# 执行SQL语句
print(SQL01)
cursor_dev.execute(SQL01)
conn_dev.commit()
else:
# 定义要执行的SQL语句
SQL02 = f"""
INSERT INTO sys_vin_check_ocr_12 (order_id,file_id,db_vin,ocr_vin,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}',null,sysdate())
"""
# 执行SQL语句
print(SQL02)
cursor_dev.execute(SQL02)
conn_dev.commit()
# # 关闭光标对象
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!