-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathgenerateVggAnno.py
More file actions
345 lines (297 loc) · 10.4 KB
/
generateVggAnno.py
File metadata and controls
345 lines (297 loc) · 10.4 KB
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
import os
import sys
import random
import math
import numpy as np
import skimage.io
import matplotlib
import matplotlib.pyplot as plt
import json
from pycocotools import mask
from skimage import measure
import cv2
import imutils
import matplotlib.patches as patches
from PIL import Image
def checkFashionIsExist(fashion:list,file_name:str)->int:
fashionClass=file_name.split("_")[0]
if fashionClass=="bag" and 1 in fashion:
return 1
elif fashionClass=="footwear" and 2 in fashion:
return 2
elif fashionClass=="outer" and 3 in fashion:
return 3
elif fashionClass=="dress" and 4 in fashion:
return 4
elif fashionClass=="sunglasses" and 5 in fashion:
return 5
elif fashionClass=="pants" and 6 in fashion:
return 6
elif fashionClass=="top" and 7 in fashion:
return 7
elif fashionClass=="shorts" and 8 in fashion:
return 8
elif fashionClass=="skirt" and 9 in fashion:
return 9
elif fashionClass=="headwear" and 10 in fashion:
return 10
else:
return -1
def getFashion(fashion: list, file_name: str):
"""
self.add_class("custom", 1, "bag") 1
self.add_class("custom", 2, "belt") -
self.add_class("custom", 3, "boots") 2
self.add_class("custom", 4, "footwear") 2
self.add_class("custom", 5, "outer") 3
self.add_class("custom", 6, "dress") 4
self.add_class("custom", 7, "sunglasses") 5
self.add_class("custom", 8, "pants") 6
self.add_class("custom", 9, "top") 7
self.add_class("custom", 10, "shorts") 8
self.add_class("custom", 11, "skirt") 9
self.add_class("custom", 12, "headwear") 10
self.add_class("custom", 13, "scarf/tie") -
"""
season = file_name.split("_")[1]
seasonName=""
if season == "ilkb":
seasonName = 1
elif season == "yaz":
seasonName = 2
elif season == "sonb":
seasonName = 3
elif season == "kis":
seasonName = 4
elif season == "4mev":
seasonName = 5
if fashion == 1:
if seasonName == 1:
return 1
elif seasonName == 2:
return 2
elif seasonName == 3:
return 3
elif seasonName == 4:
return 4
elif seasonName == 5:
return 5
elif fashion in [2, 13]:
return -1
elif fashion in [3, 4]:
if seasonName == 1:
return 6
elif seasonName == 2:
return 7
elif seasonName == 3:
return 8
elif seasonName == 4:
return 9
elif seasonName == 5:
return 10
elif fashion == 5:
if seasonName == 1:
return 11
elif seasonName == 2:
return 12
elif seasonName == 3:
return 13
elif seasonName == 4:
return 14
elif seasonName == 5:
return 15
elif fashion == 6:
if seasonName == 1:
return 16
elif seasonName == 2:
return 17
elif seasonName == 3:
return 18
elif seasonName == 4:
return 19
elif seasonName == 5:
return 20
elif fashion == 7:
if seasonName == 1:
return 21
elif seasonName == 2:
return 22
elif seasonName == 3:
return 23
elif seasonName == 4:
return 24
elif seasonName == 5:
return 25
elif fashion == 8:
if seasonName == 1:
return 26
elif seasonName == 2:
return 27
elif seasonName == 3:
return 28
elif seasonName == 4:
return 29
elif seasonName == 5:
return 30
elif fashion == 9:
if seasonName == 1:
return 31
elif seasonName == 2:
return 32
elif seasonName == 3:
return 33
elif seasonName == 4:
return 34
elif seasonName == 5:
return 35
elif fashion == 10:
if seasonName == 1:
return 36
elif seasonName == 2:
return 37
elif seasonName == 3:
return 38
elif seasonName == 4:
return 39
elif seasonName == 5:
return 40
elif fashion == 11:
if seasonName == 1:
return 41
elif seasonName == 2:
return 42
elif seasonName == 3:
return 43
elif seasonName == 4:
return 44
elif seasonName == 5:
return 45
elif fashion == 12:
if seasonName == 1:
return 46
elif seasonName == 2:
return 47
elif seasonName == 3:
return 48
elif seasonName == 4:
return 49
elif seasonName == 5:
return 50
def generateAnnotation(annotations: dict, segmentation: list, fashions:list, file_name, widht, height):
fashion=checkFashionIsExist(fashions,file_name)
fashions=list(fashions)
if fashion>0:
x, y = [], []
regions = []
k=fashions.index(fashion)
#for k in range(len(segmentation)):
for i in range(0, len(segmentation[k][0]), 1):
x.append(segmentation[k][0][i][1])
y.append(segmentation[k][0][i][0])
shape_attributes = {}
shape_attributes["name"] = "polygon"
shape_attributes["all_points_x"] = x
shape_attributes["all_points_y"] = y
region_attributes = {}
fash = getFashion(fashion,file_name)
region_attributes["fashion"] = str(fash)
regions.append({"shape_attributes": shape_attributes, "region_attributes": region_attributes})
filename = file_name
size = -1
file_attributes = {}
file_attributes["widht"] = widht
file_attributes["height"] = height
filenameParent = file_name.split(".")[0]
annotations[filenameParent] = {"filename": filename, "size": -1, "regions": regions,
"file_attributes": file_attributes}
else:
print("Model found anything !! (pn.272) ")
# Root directory of the project
ROOT_DIR = os.path.abspath("../../")
# Import Mask RCNN
sys.path.append(ROOT_DIR) # To find local version of the library
from mrcnn import utils
import mrcnn.model as modellib
from mrcnn import visualize
# Import COCO config
sys.path.append(os.path.join(ROOT_DIR, "samples\\custom\\")) # To find local version
import coco
# %matplotlib inline
# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
# Local path to trained weights file
CUSTOM_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_Modanet.h5")
# Download COCO trained weights from Releases if needed
if not os.path.exists(CUSTOM_MODEL_PATH):
utils.download_trained_weights(CUSTOM_MODEL_PATH)
class InferenceConfig(coco.CocoConfig):
# Set batch size to 1 since we'll be running inference on
# one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
GPU_COUNT = 1
IMAGES_PER_GPU = 1
config = InferenceConfig()
config.display()
# Create model object in inference mode.
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)
# Load weights trained on MS-COCO
model.load_weights(CUSTOM_MODEL_PATH, by_name=True)
# model.load_weights(CUSTOM_MODEL_PATH, by_name=True, exclude=[ "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"])
# COCO Class names
# Index of the class in the list is its ID. For example, to get ID of
# the teddy bear class, use: class_names.index('teddy bear')
# #Update the class names in the order mentioned in the custom.py file
class_names = ['BG', 'bag', 'belt', 'boots', 'footwear', 'outer', 'dress', 'sunglasses', 'pants', 'top', 'shorts',
'skirt' , 'headwear', 'scarf/tie']
# Directory of images to run detection on
IMAGE_DIR = os.path.join(ROOT_DIR, "image")
# Load a random image from the images folder
file_names = next(os.walk(IMAGE_DIR))[2]
annotations = {}
xx=0
for file_name in file_names:
print(xx)
xx+=1
try:
image = skimage.io.imread(os.path.join(IMAGE_DIR, file_name))
height, width, channels = image.shape
# Run detection
results = model.detect([image], verbose=1)
# Visualize results
r = results[0]
#visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'],class_names, r['scores'])
# Crop mask
masked = r["masks"]
k = 0
for i in range(masked.shape[2]):
image = cv2.imread("../../images/" + file_name)
height, width, channels = image.shape
image = imutils.resize(image, width=width)
for j in range(image.shape[2]):
image[:, :, j] = image[:, :, j] * masked[:, :, i]
f = file_name.split(".")[0]
filename = "segmentation/" + f + "_segment_%d.jpg" % k
cv2.imwrite(filename, image)
k += 1
# Generate vgg Annotation
masked = r["masks"].transpose(2, 0, 1)
segmented = []
for testMask in masked:
tempMask = np.zeros((np.size(testMask, 0), np.size(testMask, 1)), dtype=np.uint8)
for i in range(len(testMask)):
for j in range(len(testMask[i])):
if testMask[i][j] == True:
tempMask[i][j] = 1
else:
tempMask[i][j] = 0
fortran_ground_truth_binary_mask = np.asfortranarray(tempMask)
encoded_ground_truth = mask.encode(fortran_ground_truth_binary_mask)
ground_truth_area = mask.area(encoded_ground_truth)
ground_truth_bounding_box = mask.toBbox(encoded_ground_truth)
contours = measure.find_contours(tempMask, 0.5)
segmented.append(contours)
generateAnnotation(annotations, segmented, r['class_ids'], file_name, width, height)
except Exception as err:
print("Hata var: ", file_name)
print(err)
with open('custom_dataset_annotations.json', 'w') as outfile:
json.dump(annotations, outfile, indent=4)