-
Notifications
You must be signed in to change notification settings - Fork 0
/
4-generate-composites.py
executable file
·290 lines (232 loc) · 8.65 KB
/
4-generate-composites.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
#!/usr/bin/env python3
import os
import cv2
import json
import random
import numpy as np
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
# debug
import matplotlib.pyplot as plt
SEED = 48543
GEN_IMAGES = 2500
MIN_PER_IMAGE = 2
MAX_PER_IMAGE = 7
MIN_COVER = 0.1
MAX_COVER = 0.6
IMGSZ = 640
DEBUG = "DEBUG" in os.environ and os.environ["DEBUG"] == "1"
INPUT_DIR = "data/object-cutouts"
INPUT_BACKGROUND_DIR = "data/backgrounds"
OUTPUT_IMAGES_DIR = "data/dataset/train/images"
OUTPUT_LABELS_DIR = "data/dataset/train/labels"
random.seed(SEED)
# load class data
with open("data/classes.json", "r") as f:
CLASS = json.loads(f.read())
# create bucketed file source list
# this is really shit code but it works :)
cutouts = {}
cutout_index = {}
for cls in CLASS.keys():
cutouts[cls] = []
cutout_index[cls] = 0
for file in os.listdir(INPUT_DIR):
if file.endswith(".png"):
cls, id, name = file.split(",", 2)
cutouts[cls].append(file)
def shuffle_cutout(cls):
random.shuffle(cutouts[cls])
# we prioritise using all unique images before rotations
cutouts[cls].sort(key=lambda x: int(x.split(",", 2)[1]))
for cls in CLASS.keys():
shuffle_cutout(cls)
queue = [a for a in CLASS.keys()]
random.shuffle(queue)
def next_in_queue():
global queue
if len(queue) == 0:
queue = [a for a in CLASS.keys()]
random.shuffle(queue)
return queue.pop()
def next_cutout():
cls = next_in_queue()
file = cutouts[cls][cutout_index[cls]]
# increment index and reshuffle
cutout_index[cls] += 1
if cutout_index[cls] >= len(cutouts[cls]):
cutout_index[cls] = 0
shuffle_cutout(cls)
# return file
return file
def load_bg(file):
print("Loading background...", file)
img = cv2.imread(os.path.join(INPUT_BACKGROUND_DIR, file))
if IMGSZ is not None:
height, width, _ = img.shape
img = cv2.resize(img, (round(width * (IMGSZ / height)), round(IMGSZ)))
return img
# load backgrounds
backgrounds = [
file
for file in [
load_bg(file) for file in os.listdir(INPUT_BACKGROUND_DIR) if file != ".gitkeep"
]
if file is not None
]
random.shuffle(backgrounds)
# generate required number of composite images
for i in range(1, GEN_IMAGES + 1):
print(f"[{i} / {GEN_IMAGES}]")
background = backgrounds.pop(0)
backgrounds.append(background)
background_height, background_width, _ = background.shape
plot = np.array(background, copy=True)
GEN_OBJECTS = random.randint(MIN_PER_IMAGE, MAX_PER_IMAGE)
SEGMENTS = []
for _ in range(0, GEN_OBJECTS):
cutout = next_cutout()
print("Loading cutout", cutout)
cls, _ = cutout.split(",", 1)
cutout_image = cv2.imread(os.path.join(INPUT_DIR, cutout), cv2.IMREAD_UNCHANGED)
cutout_height, cutout_width, _ = cutout_image.shape
target_cover = random.uniform(MIN_COVER, MAX_COVER)
try_width, try_height = (
background_width * target_cover,
background_height * target_cover,
)
acceptable_width, acceptable_height = (
min(
cutout_width,
min(background_width, (try_height / cutout_height) * cutout_width),
),
min(
cutout_height,
min(background_height, (try_width / cutout_width) * cutout_height),
),
)
target_width, target_height = (
min(acceptable_width, acceptable_height * (cutout_width / cutout_height)),
min(acceptable_height, acceptable_width * (cutout_height / cutout_width)),
)
sf = target_width / cutout_width
cutout_image = cv2.resize(
cutout_image, (round(target_width), round(target_height))
)
cutout_height, cutout_width, _ = cutout_image.shape
# target placement
x = random.randint(0, background_width - cutout_width)
y = random.randint(0, background_height - cutout_height)
# create the overlay image
overlay_image = np.zeros(
(background_height, background_width, 4), dtype=np.uint8
)
overlay_image[y : y + cutout_height, x : x + cutout_width] = cutout_image
# extract alpha channel and use it as a mask
alpha = overlay_image[:, :, 3] / 255.0
mask = np.repeat(alpha[:, :, np.newaxis], 3, axis=2)
# composite the images together
plot = plot * (1.0 - mask) + overlay_image[:, :, :3] * mask
# load segment data
segment = np.load(os.path.join(INPUT_DIR, cutout[:-3] + "npy"))
segment = np.array(
[
[(x + px * sf) / background_width, (y + py * sf) / background_height]
for px, py in segment
]
)
try:
# occlude existing masks
polygon = Polygon(segment)
for j in range(0, len(SEGMENTS)):
# remove any points that are within the mask we just created
cls_id, mask = SEGMENTS[j]
keep = [not polygon.contains(Point([x, y])) for x, y in mask]
new_mask = mask[keep]
try:
# try to merge points into the object
merge_index = next(i for i, x in enumerate(keep) if not x)
new_polygon = Polygon(mask)
new_points = np.array(
[
[x, y]
for x, y in segment
if new_polygon.contains(Point([x, y]))
]
) # NOTE: we assume there's only one segment of overlap
# this will not work properly with more but this is more
# of a bandaid fix than anything.
# FIXME: handle each segment of points and do some funny math
# to determine which segment from the object needs to be pulled out
# try to roughly match the direction of the two merged segments
isx, isy = new_mask[merge_index - 1]
iex, iey = new_mask[merge_index]
jsx, jsy = new_points[0]
jex, jey = new_points[len(new_points) - 1]
flip_x, flip_y = False, False
if (iex > isx and jsx > jex) or (isx > iex and jex > jsx):
flip_x = True
if (iey > isy and jsy > jey) or (isy > iey and jey > jsy):
flip_y = True
# apply operation
if flip_x or flip_y:
new_points = new_points[::-1]
SEGMENTS[j] = (
cls_id,
np.concatenate(
(
new_mask[:merge_index],
new_points,
new_mask[merge_index:],
),
axis=0,
),
)
except StopIteration:
pass
except IndexError:
# give up if we can't read points
SEGMENTS[j] = (cls_id, new_mask)
# add new mask
SEGMENTS.append((CLASS[cls], segment))
except Exception as e:
print("Skipping object due to error")
print(e)
continue
plot = plot.astype(np.uint8)
if DEBUG:
# plot segmentation masks
i, j, k = 0, 0, 0
for _, segment in SEGMENTS:
segxy = np.array(
[[x * background_width, y * background_height] for x, y in segment]
)
cv2.drawContours(plot, np.int32([segxy]), -1, (i, j, k), 8)
i += 128
if i > 255:
i = 0
j += 128
if j > 255:
j = 0
k += 128
if k > 255:
k = 0
# show debug output
plotted_image = cv2.cvtColor(plot, cv2.COLOR_BGR2RGB)
plt.axis("off")
plt.imshow(plotted_image)
plt.show()
else:
# write to the dataset
cv2.imwrite(os.path.join(OUTPUT_IMAGES_DIR, f"{i}.png"), plot)
with open(os.path.join(OUTPUT_LABELS_DIR, f"{i}.txt"), "w") as f:
f.write(
"\n".join(
[
str(cls)
+ " "
+ " ".join([str(a) for a in segment.flatten().tolist()])
for cls, segment in SEGMENTS
]
)
)