-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
422 lines (363 loc) · 15.3 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import datetime
import os
import cv2
import numpy as np
from PIL import Image
import math
# from simple_lama_inpainting import SimpleLama
# simple_lama = SimpleLama()
def image_crop_new(boxes,image_path,idx):
pil_image=cv2.imread(image_path)
min_x = max(min([x for x, _ in boxes]),0)
max_x = max([x for x, _ in boxes])
min_y = max(min([y for _, y in boxes]),0)
max_y = max([y for _, y in boxes])
patch_image = pil_image[min_y:max_y, min_x:max_x]
if image_path[-10:-4]=='reedit':
save_image_path= image_path[:-11]+f'{idx}crop.png'
else:
save_image_path= image_path[:-4]+f'{idx}crop.png'
cv2.imwrite(save_image_path,patch_image)
return save_image_path
def image_crop(pil_imgae,boxes,image_path,idx):
pil_image=cv2.imread(image_path)
min_x = max(min([x for x, _ in boxes]),0)
max_x = max([x for x, _ in boxes])
min_y = max(min([y for _, y in boxes]),0)
max_y = max([y for _, y in boxes])
patch_image = pil_image[min_y:max_y, min_x:max_x]
if image_path[-10:-4]=='reedit':
save_image_path= image_path[:-11]+f'{idx}crop.png'
else:
save_image_path= image_path[:-4]+f'{idx}crop.png'
cv2.imwrite(save_image_path,patch_image)
return save_image_path
def image_crop_google(boxes,image_path,idx):
pil_image=cv2.imread(image_path)
min_x = max(min([x for x, _ in boxes]),0)
max_x = max([x for x, _ in boxes])
min_y = max(min([y for _, y in boxes]),0)
max_y = max([y for _, y in boxes])
patch_image = pil_image[min_y:max_y, min_x:max_x]
save_image_path= image_path[:-4]+f'{idx}crop.png'
cv2.imwrite(save_image_path,patch_image)
return save_image_path
def save_images(img_list, folder):
if not os.path.exists(folder):
os.makedirs(folder)
now = datetime.datetime.now()
date_str = now.strftime("%Y-%m-%d")
folder_path = os.path.join(folder, date_str)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
time_str = now.strftime("%H_%M_%S")
for idx, img in enumerate(img_list):
image_number = idx + 1
filename = f"{time_str}_{image_number}.jpg"
save_path = os.path.join(folder_path, filename)
cv2.imwrite(save_path, img[..., ::-1])
def check_channels(image):
channels = image.shape[2] if len(image.shape) == 3 else 1
if channels == 1:
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
elif channels > 3:
image = image[:, :, :3]
return image
def resize_image(img, max_length=768):
height, width = img.shape[:2]
max_dimension = max(height, width)
if max_dimension > max_length:
scale_factor = max_length / max_dimension
new_width = int(round(width * scale_factor))
new_height = int(round(height * scale_factor))
new_size = (new_width, new_height)
img = cv2.resize(img, new_size)
height, width = img.shape[:2]
img = cv2.resize(img, (width-(width % 64), height-(height % 64)))
return img
def resize_image_boxes(img,boxes, max_length=768):
height, width = img.shape[:2]
height_original,width_original=height,width
max_dimension = max(height, width)
if max_dimension > max_length:
scale_factor = max_length / max_dimension
new_width = int(round(width * scale_factor))
new_height = int(round(height * scale_factor))
new_size = (new_width, new_height)
img = cv2.resize(img, new_size)
height, width = img.shape[:2]
img = cv2.resize(img, (width-(width % 64), height-(height % 64)))
height_end,width_end=img.shape[:2]
height_scale=height_end/height_original
width_scale=width_end/width_original
new_boxes=[]
for box_coordinates in boxes:
box_coordinates=np.array(box_coordinates, dtype=np.int32)
rect = cv2.minAreaRect(box_coordinates)
center, size, angle = rect
if angle<45:
# 调整box
size_new=(size[0]*width_scale,size[1]*height_scale)
else:
size_new=(size[0]*height_scale,size[1]*width_scale)
# 调整中心点坐标
new_center = (center[0] * width_scale, center[1] * height_scale)
new_rect = (new_center, size_new, angle)
new_box_coordinates = (cv2.boxPoints(new_rect).tolist())
new_coords = []
for i, coord in enumerate(new_box_coordinates):
if i in (0, 1): # 对于检测框的左上角和左下角进行向下取整
new_coords.append([math.floor(x) for x in coord])
elif i in (2, 3): # 对于检测框的右下角和右上角进行向上取整
new_coords.append([math.ceil(x) for x in coord])
new_boxes.append(new_coords)
return img,new_boxes
# 计算连续两点间的距离
def distance(pt1, pt2):
return np.sqrt((pt1[0] - pt2[0])**2 + (pt1[1] - pt2[1])**2)
def enlarge_box_bigger(box):
# 找到最小外接矩形
width=distance(box[0],box[1])
height=distance(box[2],box[3])
delta_width=width*0.06
delta_height=height*0.06
# 计算矩形的中心、宽度、高度和旋转角
box=np.array(box, dtype=np.int32)
rect = cv2.minAreaRect(box)
center, size, angle = rect
if angle<45:
size_new=(size[0]+delta_width,size[1]+delta_height)
else:
size_new=(size[0]+delta_height,size[1]+delta_width)
# 生成新的最小外接矩形的四个顶点
new_rect = (center, size_new, angle)
new_box = cv2.boxPoints(new_rect)
# # 获取图像大小
# img_height,img_width = pil_image.shape[:2]
# # 确保放大后的box坐标不超出图像边界
# new_box[:, 0] = np.clip(new_box[:, 0], 1, img_width-10)
# new_box[:, 1] = np.clip(new_box[:, 1], 1, img_height-10)
return new_box
def resize_mask2(pil_image,box_coordinates):
# 获取图像大小
height,width = pil_image.shape[:2]
# 将坐标数据转换为float类型
box_coordinates = np.array(box_coordinates, dtype=np.int32)
whether_erase=False
dis_kuan=np.linalg.norm(box_coordinates[1]-box_coordinates[0])
dis_gao=np.linalg.norm(box_coordinates[2]-box_coordinates[1])
# 找到最小外接矩形
rect = cv2.minAreaRect(box_coordinates)
# 计算矩形的中心、宽度、高度和旋转角
center, size, angle = rect
scale_factor=word_count_new/word_count_old
if mode=='ch2en':
scale_factor=scale_factor/2.5
elif mode=='en2ch' or mode=='fr2ch':
scale_factor=scale_factor*1.8
else:
scale_factor=scale_factor
if scale_factor>=0.8 and scale_factor<=1.2:
scale_factor=1
if scale_factor<0.8:#尺度缩小过大才用擦图
whether_erase=True
if angle<45:
# 根据单词数翻译前后的比例调整矩形的长度
size_new=(size[0]*scale_factor,size[1])
else:
size_new=(size[0],size[1]*scale_factor)
# 生成新的最小外接矩形的四个顶点
new_rect = (center, size_new, angle)
new_box = cv2.boxPoints(new_rect)
new_box = np.int0(new_box)
# 创建一个全黑的图像作为初始掩码
mask = np.zeros((height,width), dtype=np.uint8)
# 在掩码上绘制新的多边形,使用白色填充
cv2.drawContours(mask, [new_box], 0, (255), -1)
mask = 255 - mask
return mask,whether_erase
def resize_mask(img_path,box_coordinates, word_count_old,word_count_new,mode):
pil_image=cv2.imread(img_path)
# 获取图像大小
height,width = pil_image.shape[:2]
# 将坐标数据转换为float类型
box_coordinates = np.array(box_coordinates, dtype=np.int32)
whether_erase=False
dis_kuan=np.linalg.norm(box_coordinates[1]-box_coordinates[0])
dis_gao=np.linalg.norm(box_coordinates[2]-box_coordinates[1])
# 找到最小外接矩形
rect = cv2.minAreaRect(box_coordinates)
# 计算矩形的中心、宽度、高度和旋转角
center, size, angle = rect
scale_factor=word_count_new/word_count_old
if mode=='ch2en':
scale_factor=scale_factor/2.5
elif mode=='en2ch' or mode=='fr2ch':
scale_factor=scale_factor*1.8
else:
scale_factor=scale_factor
if scale_factor>=0.8 and scale_factor<=1.2:
scale_factor=1
if scale_factor<0.8:#尺度缩小过大才用擦图
whether_erase=True
if angle<45:
# 根据单词数翻译前后的比例调整矩形的长度
size_new=(size[0]*scale_factor,size[1])
else:
size_new=(size[0],size[1]*scale_factor)
# 生成新的最小外接矩形的四个顶点
new_rect = (center, size_new, angle)
new_box = cv2.boxPoints(new_rect)
new_box = np.int0(new_box)
# 创建一个全黑的图像作为初始掩码
mask = np.zeros((height,width), dtype=np.uint8)
# 在掩码上绘制新的多边形,使用白色填充
cv2.drawContours(mask, [new_box], 0, (255), -1)
mask = 255 - mask
return mask,whether_erase
def resize_mask_returnbox(img_path,box_coordinates, word_count_old,word_count_new,mode):
pil_image=cv2.imread(img_path)
# 获取图像大小
height,width = pil_image.shape[:2]
# 将坐标数据转换为float类型
box_coordinates = np.array(box_coordinates, dtype=np.int32)
whether_erase=False
dis_kuan=np.linalg.norm(box_coordinates[1]-box_coordinates[0])
dis_gao=np.linalg.norm(box_coordinates[2]-box_coordinates[1])
# 找到最小外接矩形
rect = cv2.minAreaRect(box_coordinates)
# 计算矩形的中心、宽度、高度和旋转角
center, size, angle = rect
scale_factor=word_count_new/word_count_old
if mode=='ch2en':
scale_factor=scale_factor/3
elif mode=='en2ch' or mode=='fr2ch':
scale_factor=scale_factor*1.8
else:
scale_factor=scale_factor
if scale_factor>=0.8 and scale_factor<=1.2:
scale_factor=1
if scale_factor<0.8:#尺度缩小过大才用擦图
whether_erase=True
if angle<45:
# 根据单词数翻译前后的比例调整矩形的长度
size_new=(size[0]*scale_factor,size[1])
else:
size_new=(size[0],size[1]*scale_factor)
# 生成新的最小外接矩形的四个顶点
new_rect = (center, size_new, angle)
new_box = cv2.boxPoints(new_rect)
new_box = np.int0(new_box)
# 创建一个全黑的图像作为初始掩码
mask = np.zeros((height,width), dtype=np.uint8)
# 在掩码上绘制新的多边形,使用白色填充
cv2.drawContours(mask, [new_box], 0, (255), -1)
mask = 255 - mask
return mask,whether_erase,new_box
def resize_mask_returnbox_suokuan(img_path,box_coordinates, word_count_old,word_count_new,mode):
pil_image=cv2.imread(img_path)
# 获取图像大小
height,width = pil_image.shape[:2]
# 将坐标数据转换为float类型
box_coordinates = np.array(box_coordinates, dtype=np.int32)
whether_erase=False
dis_kuan=np.linalg.norm(box_coordinates[1]-box_coordinates[0])
dis_gao=np.linalg.norm(box_coordinates[2]-box_coordinates[1])
# 找到最小外接矩形
rect = cv2.minAreaRect(box_coordinates)
# 计算矩形的中心、宽度、高度和旋转角
center, size, angle = rect
scale_factor=word_count_new/word_count_old
if mode=='ch2en':
scale_factor=scale_factor/2.9
elif mode=='en2ch' or mode=='fr2ch':
scale_factor=scale_factor*1.8
elif mode=='en2jp':
scale_factor=scale_factor/1.2
else:
scale_factor=scale_factor
if scale_factor>=0.8 and scale_factor<=1.2:
scale_factor=1
size_new=(size[0],size[1])
if scale_factor<0.8 and mode=='ch2en':
scale_factor=1
size_new=(size[0],size[1])
if scale_factor<0.8 and mode!='ch2en':#尺度缩小用擦图
whether_erase=True
if angle<45:
# 根据单词数翻译前后的比例调整矩形的长度
size_new=(size[0]*scale_factor,size[1])
else:
size_new=(size[0],size[1]*scale_factor)
if scale_factor>1.2:#尺度过大则用缩宽
whether_erase=True
if angle<45:
# 根据单词数翻译前后的比例调整矩形的长度
size_new=(size[0],size[1]/scale_factor)
else:
size_new=(size[0]/scale_factor,size[1])
# 生成新的最小外接矩形的四个顶点
new_rect = (center, size_new, angle)
new_box = cv2.boxPoints(new_rect)
new_box = np.int0(new_box)
# 创建一个全黑的图像作为初始掩码
mask = np.zeros((height,width), dtype=np.uint8)
# 在掩码上绘制新的多边形,使用白色填充
cv2.drawContours(mask, [new_box], 0, (255), -1)
mask = 255 - mask
return mask,whether_erase,new_box
def pad_mask_styletext(img_path,box_coordinates,edited_img_path):
# 读取原图和编辑后的图片
ori_image = cv2.imread(img_path)
edited_image = cv2.imread(edited_img_path)
# 获取原图中需要替换的区域的检测框坐标
pts_dst = np.array(box_coordinates, dtype='float32')
# 获取编辑后图片的尺寸
h, w = edited_image.shape[:2]
# 调整pts_src的顺序以避免旋转
pts_src = np.array([[0, h], [0, 1], [w-1, 1], [w-1, h]], dtype='float32')
# 计算透视变换矩阵
M = cv2.getPerspectiveTransform(pts_src, pts_dst)
# 进行变换,将编辑后图像适配到原图的检测框中
transformed_edited_image = cv2.warpPerspective(edited_image, M, (ori_image.shape[1], ori_image.shape[0]))
# 创建一个掩模,代表替换区域
mask = np.zeros(ori_image.shape, dtype='uint8')
cv2.fillPoly(mask, [np.int32(pts_dst)], (255, 255, 255))
# 使用掩模将原图中替换区域部分设为0
ori_image = cv2.bitwise_and(ori_image, cv2.bitwise_not(mask))
# 使用掩模将变换后的编辑图像的替换部分加入到原图
result_image = cv2.add(ori_image, cv2.bitwise_and(transformed_edited_image, mask))
if 'inpainted' not in (img_path.split('/'))[-1]:
image_name=(img_path.split('/'))[-1][:-4]+'_inpainted.png'
else:
image_name=img_path
save_path= os.path.dirname(img_path)
fused_image_path=os.path.join(save_path,f'{image_name}')
# transformed_edited_image_path=os.path.join(save_path,f'trans_{image_name}')
# cv2.imwrite(transformed_edited_image_path,transformed_edited_image)
cv2.imwrite(fused_image_path, result_image)
return fused_image_path
def resize_mask_returnbox_suokuan_woresize(img_path,box_coordinates, word_count_old,word_count_new,mode):
pil_image=cv2.imread(img_path)
# 获取图像大小
height,width = pil_image.shape[:2]
# 将坐标数据转换为float类型
box_coordinates = np.array(box_coordinates, dtype=np.int32)
whether_erase=False
dis_kuan=np.linalg.norm(box_coordinates[1]-box_coordinates[0])
dis_gao=np.linalg.norm(box_coordinates[2]-box_coordinates[1])
# 找到最小外接矩形
rect = cv2.minAreaRect(box_coordinates)
# 计算矩形的中心、宽度、高度和旋转角
center, size, angle = rect
size_new=(size[0],size[1])
# 生成新的最小外接矩形的四个顶点
new_rect = (center, size_new, angle)
new_box = cv2.boxPoints(new_rect)
new_box = np.int0(new_box)
# 创建一个全黑的图像作为初始掩码
mask = np.zeros((height,width), dtype=np.uint8)
# 在掩码上绘制新的多边形,使用白色填充
cv2.drawContours(mask, [new_box], 0, (255), -1)
mask = 255 - mask
return mask,whether_erase,new_box