117 lines
3.6 KiB
Python
117 lines
3.6 KiB
Python
import math
|
|
from PIL import Image as pli
|
|
from PIL.Image import Image
|
|
import cv2
|
|
import numpy as np
|
|
from PIL import Image
|
|
|
|
|
|
def distance(p1, p2):
|
|
return math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2)
|
|
|
|
|
|
def find_edge_lengths(points):
|
|
# 计算所有可能的边的长度
|
|
edge_lengths = []
|
|
for i in range(len(points)):
|
|
for j in range(i + 1, len(points)):
|
|
edge_lengths.append(distance(points[i], points[j]))
|
|
return edge_lengths
|
|
|
|
|
|
def find_opposite_edges(edge_lengths):
|
|
# 对边的长度应该是成对的
|
|
edge_lengths.sort()
|
|
return edge_lengths
|
|
|
|
|
|
async def determine_orientation(points):
|
|
# 提取所有点的x和y坐标
|
|
x_coords = [p[0] for p in points]
|
|
y_coords = [p[1] for p in points]
|
|
|
|
# 计算x和y坐标的最大值和最小值
|
|
x_max, x_min = max(x_coords), min(x_coords)
|
|
y_max, y_min = max(y_coords), min(y_coords)
|
|
|
|
# 计算宽度和高度
|
|
width = x_max - x_min
|
|
height = y_max - y_min
|
|
|
|
edge_lengths = find_edge_lengths(points)
|
|
# 计算对边长度
|
|
opposite_edges = find_opposite_edges(edge_lengths)
|
|
|
|
# 判断竖着还是横着
|
|
if height > width:
|
|
return 1, opposite_edges
|
|
else:
|
|
return 0, opposite_edges
|
|
|
|
|
|
async def split_text_box(direction, opposite_edges, text_content, points):
|
|
# 左上,右上,右下,左下
|
|
point_lu_x, point_lu_y = points[0]
|
|
point_ru_x, point_ru_y = points[1]
|
|
point_rd_x, point_rd_y = points[2]
|
|
point_ld_x, point_ld_y = points[3]
|
|
if direction == 0:
|
|
# 文本框 长度
|
|
box_length_l = point_ru_x - point_lu_x
|
|
box_length_r = point_ld_x - point_rd_x
|
|
text_length = len(text_content)
|
|
each_text_size = (box_length_l + box_length_r) / 2 / text_length
|
|
min_size = min(opposite_edges)
|
|
cha = min_size / 6
|
|
print(f'差值:{cha}')
|
|
word_box = []
|
|
point_lu_x_sp = point_lu_x
|
|
point_ru_x_sp = point_ru_x
|
|
point_rd_x_sp = point_rd_x
|
|
point_ld_x_sp = point_ld_x
|
|
for char in text_content:
|
|
word_const = {
|
|
"text": char,
|
|
"box": [[point_lu_x_sp, point_lu_y],
|
|
[point_ru_x_sp, point_ru_y],
|
|
[point_rd_x_sp, point_rd_y],
|
|
[point_ld_x_sp, point_ld_y]]
|
|
}
|
|
point_lu_x_sp += min_size + cha
|
|
point_ru_x_sp += min_size + cha
|
|
point_rd_x_sp += min_size + cha
|
|
point_ld_x_sp += min_size + cha
|
|
word_box.append(word_const)
|
|
return word_box
|
|
elif direction == 1:
|
|
# 文本框 长度
|
|
box_length_l = point_ru_y - point_lu_y
|
|
box_length_r = point_ld_y - point_rd_y
|
|
text_length = len(text_content)
|
|
each_text_size = (box_length_l + box_length_r) / 2 / text_length
|
|
min_size = min(opposite_edges)
|
|
cha = min_size / 5
|
|
print(f'差值:{cha}')
|
|
word_box = []
|
|
point_lu_y_sp = point_lu_y
|
|
point_ru_y_sp = point_ru_y
|
|
point_rd_y_sp = point_rd_y
|
|
point_ld_y_sp = point_ld_y
|
|
for char in text_content:
|
|
word_const = {
|
|
"text": char,
|
|
"box": [[point_lu_x, point_lu_y_sp],
|
|
[point_ru_x, point_ru_y_sp],
|
|
[point_rd_x, point_rd_y_sp],
|
|
[point_ld_x, point_ld_y_sp]]
|
|
}
|
|
point_lu_y_sp += min_size + cha
|
|
point_ru_y_sp += min_size + cha
|
|
point_rd_y_sp += min_size + cha
|
|
point_ld_y_sp += min_size + cha
|
|
word_box.append(word_const)
|
|
return word_box
|
|
|
|
|
|
|