-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset_processing.py
505 lines (416 loc) · 21.1 KB
/
dataset_processing.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
import torch
import torch.nn as nn
from torch_geometric.data import Dataset
import os
from utils import *
from paddleocr import PaddleOCR, draw_ocr
from PIL import Image, ImageFont
import numpy as np
import cv2
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
from matplotlib.colors import hsv_to_rgb, rgb_to_hsv
from torchvision.models import resnet50, ResNet50_Weights
from model.CNN import Autoencoder
from model.graph import DesignGraph
from config import *
class ProcessedDeStijl(Dataset):
def __init__(self, data_path):
self.path = data_path
self.path_dict = {
'preview': data_path + '/00_preview/',
'background': data_path + '/01_background/',
'image': data_path + '/02_image/',
'decoration': data_path + '/03_decoration/',
'text': data_path + '/04_text/',
'theme': data_path + '/05_theme/'
}
self.data_path = data_path
self.dataset_size = len(next(os.walk(self.path_dict['preview']))[2])
self.ocr = PaddleOCR(use_angle_cls=True, lang='en', use_gpu=False)
self.criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 200, .1)
self.flags = cv2.KMEANS_RANDOM_CENTERS
self.layers = ['background', 'text', "decoration"] # Take this from config file later
# self.layers = ['background', 'text']
self.pretrained_model = resnet50(weights=ResNet50_Weights.IMAGENET1K_V1)
# self.pretrained_model = Autoencoder()
# self.pretrained_model.load_state_dict(torch.load("../CNN_models/CNNAutoencoder/weights/best.pth")["state_dict"])
def len(self):
return self.dataset_size
def get(self, idx):
'''
Return a graph object based on the information
'''
pass
######### RUNTIME EXTRACTION ###########
######### PROCESSING AND ANNOTATING THE DATASET ###########
def extract_text_bbox(self, img_path, preview_image_path):
'''
Input: path to the text image
Extract text using paddleOCR.
Crop text from bounding box.
Extract colors using Kmeans inside the bbox.
Return the dominant color and the position.
DONE: Try to combine very close lines as paragraph bbox.
If the the distance between two bbox is smaller than the bbox height and color is the same,
we can group them as paragraphs.
Return: text color palettes, dominant colors for each text and position list (as bboxes).
'''
# Parameters for KMeans.
n_colors = 3
result = self.ocr.ocr(img_path, cls=True)[0]
image = Image.open(img_path).convert('RGB')
boxes = [line[0] for line in result]
texts = [line[1][0] for line in result]
image = cv2.imread(img_path)
preview_image = cv2.imread(preview_image_path)
palettes = []
dominants = []
new_bboxes = []
# Run KMeans for each text object
for bbox in boxes:
# Crop the text area
x, y = int(bbox[0][0]), int(bbox[0][1])
z, t = int(bbox[2][0]), int(bbox[2][1])
cropped_image = image[y:t, x:z]
# Do template matching to find the places at the actual image because not every image has the same size.
method = cv2.TM_SQDIFF_NORMED
result = cv2.matchTemplate(cropped_image, preview_image, method)
mn,_,mnLoc,_ = cv2.minMaxLoc(result)
MPx,MPy = mnLoc
trows,tcols = cropped_image.shape[:2]
# --> left top, right top, right bottom, left bottom
bbox = [[MPx,MPy], [MPx+tcols, MPy], [MPx+tcols, MPy+trows], [MPx, MPy+trows]]
new_bboxes.append(bbox)
return new_bboxes, boxes, texts
def compose_paragraphs(self, text_bboxes, text_palettes):
'''
Compose text data into paragraphs.
Return: Grouped indices of detected text elements.
'''
num_text_boxes = len(text_bboxes)
if num_text_boxes == 0:
return False
composed_text_idxs = [[0]]
for i in range(num_text_boxes-1):
palette1 = text_palettes[i]
palette2 = text_palettes[i+1]
if np.array_equal(palette1, palette2):
bbox1 = text_bboxes[i]
bbox2 = text_bboxes[i+1]
height1 = bbox1[0][1] - bbox1[3][1]
height2 = bbox2[0][1] - bbox2[3][1]
if abs(bbox1[0][1]-bbox2[0][1]) <= abs(height1)+30:
if i != 0 and i not in composed_text_idxs[-1]:
composed_text_idxs.append([i])
composed_text_idxs[-1].append(i+1)
else:
if i != 0 and i not in composed_text_idxs[-1]:
composed_text_idxs.append([i])
if i == num_text_boxes-2:
composed_text_idxs.append([i+1])
else:
if i != 0 and i not in composed_text_idxs[-1]:
composed_text_idxs.append([i])
if i == (num_text_boxes-2):
composed_text_idxs.append([i+1])
return composed_text_idxs
def merge_bounding_boxes(self, composed_text_idxs, bboxes):
'''
openCV --> x: left-to-right, y: top--to-bottom
bbox coordinates --> [[256.0, 1105.0], [1027.0, 1105.0], [1027.0, 1142.0], [256.0, 1142.0]]
--> left top, right top, right bottom, left bottom
TODO: Also return color palettes for each merged box.
'''
biggest_borders = []
if len(bboxes) == 0:
return biggest_borders
for idxs in composed_text_idxs:
smallest_x = smallest_y = 10000
biggest_y = biggest_x = 0
if len(idxs) > 1:
for idx in idxs:
bbox = bboxes[idx]
bbox_smallest_x, bbox_smallest_y = np.min(bbox, axis=0)
bbox_biggest_x, bbox_biggest_y = np.max(bbox, axis=0)
if smallest_x > bbox_smallest_x:
smallest_x = bbox_smallest_x
if smallest_y > bbox_smallest_y:
smallest_y = bbox_smallest_y
if biggest_x < bbox_biggest_x:
biggest_x = bbox_biggest_x
if biggest_y < bbox_biggest_y:
biggest_y = bbox_biggest_y
biggest_border = [[smallest_x, smallest_y], [biggest_x, smallest_y], [biggest_x, biggest_y], [smallest_x, biggest_y]]
biggest_borders.append(biggest_border)
else:
biggest_borders.append(bboxes[idxs[0]])
return biggest_borders
def mini_kmeans(self, biggest_border, n_colors, image):
# for text
x, y = int(biggest_border[0][0]), int(biggest_border[0][1])
z, t = int(biggest_border[2][0]), int(biggest_border[2][1])
cropped_image = image[y:t, x:z]
pixels = np.float32(cropped_image.reshape(-1, 3))
_, labels, palette = cv2.kmeans(pixels, n_colors, None, self.criteria, 10, self.flags)
palette = np.asarray(palette, dtype=np.int64)
_, counts = np.unique(labels, return_counts=True)
color = palette[np.argmin(counts)]
return color
def extract_text_directly(self, img_path, white_bg_texts):
n_colors = 2
result = self.ocr.ocr(img_path, cls=True)[0]
image = Image.open(img_path).convert('RGB')
boxes = [line[0] for line in result]
texts = [line[1][0].replace(" ", "").lower() for line in result]
white_bg_texts = [elem.replace(" ", "").lower() for elem in white_bg_texts]
image = cv2.imread(img_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
same_idxs = []
new_boxes = []
composed_text_palettes = []
for j, elem in enumerate(white_bg_texts):
for i, text in enumerate(texts):
if similar(elem, text) > 0.85:
new_boxes.append(boxes[i])
biggest_border = boxes[i]
composed_text_palettes.append(self.mini_kmeans(biggest_border, n_colors, image))
elif i+1 != len(texts):
if similar(elem, text + texts[i+1]) > 0.85:
# merge boxes
bboxes = [boxes[i], boxes[i+1]]
smallest_x = 1000
smallest_x = smallest_y = 10000
biggest_y = biggest_x = 0
for idx in [0, 1]:
bbox = bboxes[idx]
bbox_smallest_x, bbox_smallest_y = np.min(bbox, axis=0)
bbox_biggest_x, bbox_biggest_y = np.max(bbox, axis=0)
if smallest_x > bbox_smallest_x:
smallest_x = bbox_smallest_x
if smallest_y > bbox_smallest_y:
smallest_y = bbox_smallest_y
if biggest_x < bbox_biggest_x:
biggest_x = bbox_biggest_x
if biggest_y < bbox_biggest_y:
biggest_y = bbox_biggest_y
biggest_border = [[smallest_x, smallest_y], [biggest_x, smallest_y], [biggest_x, biggest_y], [smallest_x, biggest_y]]
new_boxes.append(biggest_border)
composed_text_palettes.append(self.mini_kmeans(biggest_border, n_colors, image))
return new_boxes, composed_text_palettes
def extract_decor_elements(self, decoration_path, preview_path):
# Determine the number of dominant colors
num_colors = 6
# Load the image
image = cv2.imread(decoration_path)
# Convert the image to the RGB color space
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image2 = image.copy()
# Reshape the image to a 2D array of pixels
pixels = image.reshape(-1, 3)
# Apply K-means clustering with the determined number of colors
kmeans = KMeans(n_clusters=num_colors)
kmeans.fit(pixels)
# Get the RGB values of the dominant colors
colors = kmeans.cluster_centers_.astype(int)
# Convert the colors to the HSV color space
hsv_colors = []
for i, color in enumerate(colors):
x, y, z = color
if not (252 < x < 256 and 252 < y < 256 and 252 < z < 256):
x, y, z = rgb_to_hsv([x/255, y/255, z/255])
hsv_colors.append([x*180, y*255, z*255])
# Convert the image to the HSV color space
hsv_image = cv2.cvtColor(image2, cv2.COLOR_RGB2HSV)
# Create masks for each dominant color
masks = []
hsv_colors = np.asarray(hsv_colors, dtype=np.int32)
colors = []
for i in range(len(hsv_colors)):
h, s, v = hsv_colors[i, :]
lower_color = hsv_colors[i, :] - np.array([10, 50, 50])
upper_color = hsv_colors[i, :] + np.array([10, 255, 255])
mask = cv2.inRange(hsv_image, lower_color, upper_color)
colors.append([h,s,v])
masks.append(mask)
# Find contours in each mask
contours = []
for mask in masks:
contours_color, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours.append(contours_color)
# Draw bounding boxes around the shapes
image_with_boxes = image.copy()
bboxes = []
for i, contour_color in enumerate(contours):
for contour in contour_color:
x, y, w, h = cv2.boundingRect(contour)
# left top, right top, right bottom, left bottom
bboxes.append([[x,y], [x+w, y], [x+w, y+h], [x,y+h]])
new_bboxes = delete_too_small_bboxes(np.asarray(bboxes))
return colors, new_bboxes
def map_decoration_coordinates(self, design_text_coordinate, text_coordinate, decoration_coordinates, prev_size, text_size):
# --> [[256.0, 1105.0], [1027.0, 1105.0], [1027.0, 1142.0], [256.0, 1142.0]]
# --> left top, right top, right bottom, left bottom
prev_x, prev_y = prev_size
text_x, text_y = text_size
design_x, design_y = design_text_coordinate[0]
text_x, text_y = text_coordinate[0]
diff_x = text_x - design_x
diff_y = text_y - design_y
new_coordinates = []
for coordinate in decoration_coordinates:
new_coor = []
for elem in coordinate:
new_coor.append([elem[0]-diff_x, elem[1]-diff_y])
new_coordinates.append(new_coor)
return new_coordinates
def extract_image(self, preview_path, image_path):
'''
Use Template Matching the put a bounding box around the main image. Use it as the position.
Extract colors using KMeans.
Return: image color palettes and position list (as bboxes).
'''
preview_image = cv2.imread(preview_path)
preview_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
cropped_image_path = trim_image(image_path, "02_image")
image = cv2.imread(cropped_image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
if image.shape[0] > preview_image.shape[0]:
diff_x = image.shape[0] - preview_image.shape[0]
image = image[(diff_x//2+1):image.shape[0]-(diff_x//2+1), :]
if image.shape[1] > preview_image.shape[1]:
diff_y = image.shape[1] - preview_image.shape[1]
image = image[:, (diff_y//2+1):image.shape[1]-(diff_y//2+1)]
method = cv2.TM_SQDIFF_NORMED
result = cv2.matchTemplate(image, preview_image, method)
mn,_,mnLoc,_ = cv2.minMaxLoc(result)
MPx,MPy = mnLoc
trows,tcols = image.shape[:2]
bbox = [[MPx,MPy], [MPx+tcols,MPy+trows]]
cropped_image = preview_image[MPx:MPx+tcols, MPy:MPy+trows]
pixels = np.float32(image.reshape(-1, 3))
n_colors = 6
_, labels, palette = cv2.kmeans(pixels, n_colors, None, self.criteria, 10, self.flags)
palette = np.asarray(palette, dtype=np.int64)
return [bbox], palette
def annotate_dataset(self):
for idx in range(388, self.dataset_size):
print("CURRENTLY AT: ", idx)
path_idx = "{:04d}".format(idx)
preview = self.path_dict['preview'] + path_idx + '.png'
decoration = self.path_dict['decoration'] + path_idx + '.png'
image = self.path_dict['image'] + path_idx + '.png'
text = self.path_dict['text'] + path_idx + '.png'
text_bboxes, white_bg_text_boxes, texts = self.extract_text_bbox(text, preview)
text_bboxes_from_design, composed_text_palettes = self.extract_text_directly(preview, texts)
composed_text_idxs = self.compose_paragraphs(text_bboxes_from_design, composed_text_palettes)
merged_bboxes = []
if composed_text_idxs != False:
merged_bboxes = self.merge_bounding_boxes(composed_text_idxs, text_bboxes_from_design)
image_bboxes, image_palette = self.extract_image(preview, image)
#decoration_hsv_xpalettes, decoration_bboxes = self.extract_decor_elements(decoration, preview)
# image_prev = cv2.imread(preview)
# image_text = cv2.imread(text)
#mapped_decoration_bboxes = self.map_decoration_coordinates(text_bboxes_from_design[0], white_bg_text_boxes[0], decoration_bboxes, (image_prev.shape[0], image_prev.shape[1]), (image_text.shape[0], image_text.shape[1]))
#create_xml("../destijl_dataset/xmls/03_decoration", path_idx+".xml", mapped_decoration_bboxes)
if len(merged_bboxes) == 0:
create_xml(self.data_path+"/xmls/04_text", path_idx+".xml", [[[0,0],[0,0],[0,0],[0,0]]])
else:
create_xml(self.data_path+"/xmls/04_text", path_idx+".xml", merged_bboxes)
create_xml(self.data_path+"/xmls/02_image", path_idx+".xml", image_bboxes)
def process_dataset(self, idx):
'''
Process each node. Construct graph features and save the features as pt files.
This code should be used after we have an annotated dataset.
'''
path_idx = "{:04d}".format(idx)
img_path_dict = {
'preview': self.data_path + '/00_preview/' + path_idx + '.png',
'background': self.data_path + '/01_background/' + path_idx + '.png',
'image': self.data_path + '/02_image/' + path_idx + '.png',
'text': self.data_path + '/04_text/' + path_idx + '.png',
'decoration': self.data_path + '/03_decoration/' + path_idx + '.png',
}
annotation_path_dict = {
'preview': self.data_path + '/xmls' +'/00_preview/' + path_idx + '.xml',
'image': self.data_path + '/xmls' + '/02_image/' + path_idx + '.xml',
'text': self.data_path + '/xmls' + '/04_text/' + path_idx + '.xml',
'decoration': self.data_path + '/xmls' + '/03_decoration/' + path_idx + '.xml',
}
all_bboxes = {
'image':[],
'background':[],
"decoration":[],
'text':[]
}
all_images = {
'image':[],
'background':[],
"decoration":[],
'text':[]
}
# For each layer:
# * save all bounding boxes to all_bboxes
# * save all paths of images in which we extract the objects from --> to all_images
# * We generally extract all images from the preview image so that path is preview image path
# * We save this information to use in DesignGraph. It extracts colors from bounding boxes
# using the image we want to extract them from.
for i, layer in enumerate(self.layers):
# Check what is the layer, save information accordingly to dictionaries
if layer == "background":
# load image as CV image
self.preview_img = cv2.imread(img_path_dict[layer])
# save the layer image path
img = img_path_dict[layer]
all_images[layer] = img
# since it is background, just add a trivial bbox. This is not used
all_bboxes[layer] = [[[0, 0], [self.preview_img.shape[0], 0], [self.preview_img.shape[0], self.preview_img.shape[1]], [0, self.preview_img.shape[1]]]]
else:
if layer == 'text':
# get the preview path since we extract text directly from preview image
img_path = img_path_dict['preview']
# get annotations from xml
filename, bboxes = VOC2bbox(annotation_path_dict[layer])
# assign bounding boxes and image path to extract colors later
all_bboxes[layer] = bboxes
all_images[layer] = img_path
elif layer == 'image' or layer == "decoration":
# same logic is applied as text
img_path = img_path_dict['preview']
self.img_img = cv2.imread(img_path)
filename, bboxes = VOC2bbox(annotation_path_dict[layer])
"""
This comment below is for checking whether the annotation boxes work for
each bounding box. It saves the annotated image.
"""
# for k, box in enumerate(bboxes):
# im = cv2.imread("../destijl_dataset/00_preview/"+path_idx+".png")
# # [[xmin, ymin], [xmax, ymin], [xmax, ymax], [xmin, ymax]]
# xmin = box[0][0]
# xmax = box[1][0]
# ymin = box[0][1]
# ymax = box[2][1]
# print(xmin, xmax, ymin, ymax)
# cv2.rectangle(im,(xmin, ymin),(xmax, ymax),(255,0,0),2)
# cv2.imwrite("check_bboxes"+str(k)+".jpg", im)
all_bboxes[layer] = bboxes
all_images[layer] = img_path
# Design graph constructs the graph object and saves it as a pt file.
design_graph = DesignGraph(self.pretrained_model, all_images, all_bboxes, self.layers, img_path_dict['preview'], idx)
return design_graph.get_all_colors_in_design()
def trial(self):
# Save the samples in the range (0, n)
for i in range(0, 3101):
print("Sample: ", i)
all_colors = self.process_dataset(i)
for nested_list in all_colors:
color = nested_list[0]
# Fix if has unnecessary extra dimension
if len(color.shape) == 2:
color = color[0]
color = color.tolist()
if __name__ == "__main__":
config = DataConfig()
dataset_root = config.dataset
dataset = ProcessedDeStijl(data_path=dataset_root)
dataset.trial()