-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorCNN.py
238 lines (189 loc) · 10.1 KB
/
colorCNN.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
"""
TODO:
* Make the white backgrounds transparent.
* Locate the images in the bounding boxes in preview images in white background.
Cut the images and paste to the location on the decoration layaer.
* The pasting order: Decoration will have white bg. Paste image. Paste text.
* When the white bg images are done, feed them to CNN.
* The output will be the missing color which is the background color.
* Use CIELab distances to train
"""
import cv2
import numpy as np
from utils import *
from dataset_processing import ProcessedDeStijl
from PIL import Image
class DestijlProcessorCNN():
def __init__(self, data_path):
self.path_dict = {
'preview': data_path + '/00_preview/',
'image': data_path + '/02_image/',
'decoration': data_path + '/03_decoration/',
'text': data_path + '/04_text/',
}
self.rgba_path_dict = {
'preview': data_path + '/rgba_dataset/00_preview/',
'image': data_path + '/rgba_dataset/02_image/',
'decoration': data_path + '/rgba_dataset/03_decoration/',
'text': data_path + '/rgba_dataset/04_text/',
'temporary': data_path + '/rgba_dataset/05_temporary/',
'cropped_preview': data_path + '/rgba_dataset/00_preview_cropped/',
}
self.xml_path_dict = {
'preview': data_path + '/xmls/00_preview/',
'image': data_path + '/xmls/02_image/',
'decoration': data_path + '/xmls/03_decoration/',
'text': data_path + '/xmls/04_text/',
}
self.processed_dataset = ProcessedDeStijl("../destijl_dataset")
def whitebg_to_transparent(self, img_path, layer):
"""
WORKING
"""
image_bgr = cv2.imread(img_path)
image_num = img_path[-8:]
# get the image dimensions (height, width and channels)
h, w, c = image_bgr.shape
# append Alpha channel -- required for BGRA (Blue, Green, Red, Alpha)
image_bgra = np.concatenate([image_bgr, np.full((h, w, 1), 255, dtype=np.uint8)], axis=-1)
# create a mask where white pixels ([255, 255, 255]) are True
white = np.all(image_bgr == [255, 255, 255], axis=-1)
# change the values of Alpha to 0 for all the white pixels
image_bgra[white, -1] = 0
# save the image
cv2.imwrite(self.rgba_path_dict[layer]+image_num, image_bgra)
def locate_images_in_image_layer(self, idx):
method = cv2.TM_SQDIFF_NORMED
path_idx = "{:04d}".format(idx)
preview_img = cv2.imread(self.path_dict["preview"]+path_idx+".png")
preview_bboxes = VOC2bbox(self.xml_path_dict["image"]+path_idx+".xml")[1]
image_img = cv2.imread(self.path_dict["image"]+path_idx+".png")
boxes = []
design_boxes = []
for box in preview_bboxes:
xmin = box[0][0]
xmax = box[1][0]
ymin = box[0][1]
ymax = box[2][1]
cropped_img = preview_img[ymin:ymax, xmin:xmax]
if(cropped_img.shape[0] > image_img.shape[0]):
diff_x = abs(cropped_img.shape[0] - image_img.shape[0])
image_img = cv2.copyMakeBorder(image_img, diff_x//2+5, diff_x//2+5, 0, 0, cv2.BORDER_CONSTANT, value=[255, 255, 255])
if(cropped_img.shape[1] > image_img.shape[1]):
diff_y = abs(cropped_img.shape[1] - image_img.shape[1])
image_img = cv2.copyMakeBorder(image_img, 0, 0, diff_y//2+5, diff_y//2+5, cv2.BORDER_CONSTANT, value=[255, 255, 255])
result = cv2.matchTemplate(cropped_img, image_img, method)
mn,_,mnLoc,_ = cv2.minMaxLoc(result)
MPx,MPy = mnLoc
trows,tcols = cropped_img.shape[:2]
boxes.append([MPx, MPx+tcols, MPy, MPy+trows])
design_boxes.append([xmin, xmax, ymin, ymax])
self.check_boxes(design_boxes, idx)
return boxes, design_boxes
def check_boxes(self, bboxes, idx):
path_idx = "{:04d}".format(idx)
im = cv2.imread("../destijl_dataset/02_image/" + path_idx + ".png")
for box in bboxes:
# [[xmin, ymin], [xmax, ymin], [xmax, ymax], [xmin, ymax]]
xmin = box[0]
xmax = box[1]
ymin = box[2]
ymax = box[3]
cv2.rectangle(im,(xmin, ymin),(xmax, ymax),(255,0,0),2)
cv2.imwrite("check_boxes.jpg", im)
def map_image_coordinates(self, text_coordinate, design_text_coordinate, design_img_coordinates, design_size, text_size):
prev_x, prev_y = design_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 design_img_coordinates:
for i in range(len(coordinate)):
if i < 2:
coordinate[i] = int(coordinate[i] + diff_x)
else:
coordinate[i] = int(coordinate[i] + diff_y)
if coordinate[i] < 0:
coordinate[i] *= -1
new_coordinates.append(coordinate)
return new_coordinates
def convert_to_min_max_coordinate(self, box):
xmin, ymin = np.min(box, axis=0)
xmax, ymax = np.max(box, axis=0)
return [int(xmin), int(xmax), int(ymin), int(ymax)]
def paste_onto_decoration_layer(self, idx):
path_idx = "{:04d}".format(idx)
preview_path = self.path_dict["preview"] + path_idx + ".png"
img_path = self.path_dict["image"] + path_idx + ".png"
decoration_path = self.path_dict["decoration"] + path_idx + ".png"
text_path = self.path_dict["text"] + path_idx + ".png"
white_bg_text_path = self.rgba_path_dict["text"] + path_idx + ".png"
white_bg_img_path = self.rgba_path_dict["image"] + path_idx + ".png"
img = cv2.imread(white_bg_img_path)
decoration_img = cv2.imread(decoration_path)
prev = cv2.imread(preview_path)
design_size = (prev.shape[0], prev.shape[1])
text_size = (decoration_img.shape[0], decoration_img.shape[1])
image_boxes, design_image_boxes = self.locate_images_in_image_layer(idx)
text_bboxes, white_bg_text_boxes, texts = self.processed_dataset.extract_text_bbox(text_path, preview_path)
text_bboxes_from_design, composed_text_palettes = self.processed_dataset.extract_text_directly(preview_path, texts)
if not text_bboxes_from_design or not white_bg_text_boxes:
pass
else:
design_text_coordinate = text_bboxes_from_design[0]
text_coordinate = white_bg_text_boxes[0]
new_image_boxes = self.map_image_coordinates(text_coordinate, design_text_coordinate, design_image_boxes, design_size, text_size)
white_bg = np.zeros( [decoration_img.shape[0], decoration_img.shape[1], 3] ,dtype=np.uint8)
white_bg.fill(255)
cv2.imwrite('bg.jpg', white_bg)
white_bg = Image.open('bg.jpg')
decoration_overlay = Image.open(self.rgba_path_dict["decoration"] + path_idx + ".png")
text_overlay = Image.open(white_bg_text_path)
white_bg.paste(decoration_overlay, mask=decoration_overlay)
for j, box in enumerate(new_image_boxes):
xmin1, xmax1, ymin1, ymax1 = box # box place on decoration
xmin2, xmax2, ymin2, ymax2 = image_boxes[j] # box place on image
cropped_img = img[ymin2:ymax2, xmin2:xmax2]
cv2.imwrite(self.rgba_path_dict["temporary"] + path_idx + ".png", cropped_img)
self.whitebg_to_transparent(self.rgba_path_dict["temporary"] + path_idx + ".png", "temporary")
cropped_img = Image.open(self.rgba_path_dict["temporary"] + path_idx + ".png")
offset = (xmin1, ymin1)
white_bg.paste(cropped_img, offset, mask=cropped_img)
white_bg.paste(text_overlay, mask=text_overlay)
white_bg.save(self.rgba_path_dict["preview"] + path_idx + ".png")
def pipeline(self):
for idx in range(550, 706):
print("Sample: ", idx)
path_idx = "{:04d}".format(idx)
img_path = self.path_dict["image"]+path_idx+".png"
text_path = self.path_dict["text"]+path_idx+".png"
decoration_path = self.path_dict["decoration"]+path_idx+".png"
self.whitebg_to_transparent(img_path, "image")
self.whitebg_to_transparent(text_path, "text")
self.whitebg_to_transparent(decoration_path, "decoration")
self.paste_onto_decoration_layer(idx)
def resize_images(self):
for idx in range(84, 705):
path_idx = "{:04d}".format(idx)
if os.path.exists(self.rgba_path_dict["preview"]+path_idx+".png"):
print(idx)
method = cv2.TM_SQDIFF_NORMED
big_image = cv2.imread(self.rgba_path_dict["preview"]+path_idx+".png")
small_image = cv2.imread(self.path_dict["preview"]+path_idx+".png")
if(small_image.shape[0] > big_image.shape[0]):
diff_x = abs(big_image.shape[0] - small_image.shape[0])
big_image = cv2.copyMakeBorder(big_image, diff_x//2+5, diff_x//2+5, 0, 0, cv2.BORDER_CONSTANT, value=[255, 255, 255])
if(small_image.shape[1] > big_image.shape[1]):
diff_y = abs(big_image.shape[1] - small_image.shape[1])
big_image = cv2.copyMakeBorder(big_image, 0, 0, diff_y//2+5, diff_y//2+5, cv2.BORDER_CONSTANT, value=[255, 255, 255])
result = cv2.matchTemplate(small_image, big_image, method)
mn,_,mnLoc,_ = cv2.minMaxLoc(result)
MPx,MPy = mnLoc
trows,tcols = small_image.shape[:2]
box = [MPx, MPx+tcols, MPy, MPy+trows]
new_img = big_image[box[2]:box[3], box[0]:box[1]]
cv2.imwrite(self.rgba_path_dict["cropped_preview"]+path_idx+".png", new_img)
processor = DestijlProcessorCNN("../destijl_dataset")
processor.resize_images()