统一引号格式,优化架构排布

This commit is contained in:
2024-09-26 15:16:57 +08:00
parent ff9d612e67
commit c5a03ad16f
22 changed files with 143 additions and 302 deletions

View File

@@ -13,14 +13,14 @@ from log import PROJECT_ROOT
@retry(stop=stop_after_attempt(3), wait=wait_random(1, 3), reraise=True,
after=lambda x: logging.warning("获取图片失败!"))
after=lambda x: logging.warning('获取图片失败!'))
def read(image_path):
"""
从网络或本地读取图片
:param image_path: 网络或本地路径
:return: NumPy数组形式的图片
"""
if image_path.startswith("http"):
if image_path.startswith('http'):
# 发送HTTP请求并获取图像数据
resp = urllib.request.urlopen(image_path, timeout=60)
# 将数据读取为字节流
@@ -76,35 +76,35 @@ def split(image, ratio=1.414, overlap=0.05, x_compensation=3):
for i in range(math.ceil(height / step)):
offset = round(step * i)
cropped_img = capture(image, [0, offset, width, offset + new_img_height])
split_result.append({"img": cropped_img, "x_offset": 0, "y_offset": offset})
split_result.append({'img': cropped_img, 'x_offset': 0, 'y_offset': offset})
elif wh_ratio > ratio: # 横向过长
new_img_width = height * ratio
step = height * (ratio - overlap * x_compensation) # 一般文字是横向的,所以横向截取时增大重叠部分
for i in range(math.ceil(width / step)):
offset = round(step * i)
cropped_img = capture(image, [offset, 0, offset + new_img_width, width])
split_result.append({"img": cropped_img, "x_offset": offset, "y_offset": 0})
split_result.append({'img': cropped_img, 'x_offset': offset, 'y_offset': 0})
else:
split_result.append({"img": image, "x_offset": 0, "y_offset": 0})
split_result.append({'img': image, 'x_offset': 0, 'y_offset': 0})
return split_result
def parse_rotation_angles(image):
"""
判断图片旋转角度,逆时针旋转该角度后为正。可能值["0", "90", "180", "270"]
判断图片旋转角度,逆时针旋转该角度后为正。可能值['0', '90', '180', '270']
:param image: 图片NumPy数组或文件路径
:return: 最有可能的两个角度
"""
angles = ['0', '90']
model = PaddleClas(model_name="text_image_orientation")
model = PaddleClas(model_name='text_image_orientation')
clas_result = model.predict(input_data=image)
try:
clas_result = next(clas_result)[0]
if clas_result["scores"][0] < 0.5:
if clas_result['scores'][0] < 0.5:
return angles
angles = clas_result["label_names"]
angles = clas_result['label_names']
except Exception as e:
logging.error("获取图片旋转角度失败", exc_info=e)
logging.error('获取图片旋转角度失败', exc_info=e)
return angles
@@ -201,25 +201,25 @@ def expand_to_a4_size(image):
if hw_ratio >= 1.42:
exp_w = int(height / 1.414 - width)
x_offset = int(exp_w / 2)
exp_img = numpy.zeros((height, x_offset, 3), dtype="uint8")
exp_img = numpy.zeros((height, x_offset, 3), dtype='uint8')
exp_img.fill(255)
image = numpy.hstack([exp_img, image, exp_img])
elif 1 <= hw_ratio <= 1.40:
exp_h = int(width * 1.414 - height)
y_offset = int(exp_h / 2)
exp_img = numpy.zeros((y_offset, width, 3), dtype="uint8")
exp_img = numpy.zeros((y_offset, width, 3), dtype='uint8')
exp_img.fill(255)
image = numpy.vstack([exp_img, image, exp_img])
elif 0.72 <= hw_ratio < 1:
exp_w = int(height * 1.414 - width)
x_offset = int(exp_w / 2)
exp_img = numpy.zeros((height, x_offset, 3), dtype="uint8")
exp_img = numpy.zeros((height, x_offset, 3), dtype='uint8')
exp_img.fill(255)
image = numpy.hstack([exp_img, image, exp_img])
elif hw_ratio <= 0.7:
exp_h = int(width / 1.414 - height)
y_offset = int(exp_h / 2)
exp_img = numpy.zeros((y_offset, width, 3), dtype="uint8")
exp_img = numpy.zeros((y_offset, width, 3), dtype='uint8')
exp_img.fill(255)
image = numpy.vstack([exp_img, image, exp_img])
return image, x_offset, y_offset