-
Notifications
You must be signed in to change notification settings - Fork 0
/
GTJSONReaderMuret.py
executable file
·510 lines (347 loc) · 16 KB
/
GTJSONReaderMuret.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
504
505
506
507
508
509
510
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#==============================================================================
"""
Created on Tue Sep 3 08:20:50 2019
@author: Francisco J. Castellanos
@project name: DAMA
"""
#==============================================================================
from enum import Enum
import numpy as np
import random
from CustomJson import CustomJson
from file_manager import FileManager
class PropertyType(Enum):
PROPERTY_TYPE_PAGES,\
PROPERTY_TYPE_REGIONS,\
PROPERTY_TYPE_REGION_TYPE,\
PROPERTY_TYPE_BOUNDING_BOX,\
PROPERTY_TYPE_BBOX_FROM_X,\
PROPERTY_TYPE_BBOX_TO_X,\
PROPERTY_TYPE_BBOX_FROM_Y,\
PROPERTY_TYPE_BBOX_TO_Y,\
PROPERTY_TYPE_STAFF_REGION,\
PROPERTY_TYPE_SYMBOLS,\
PROPERTY_TYPE_AGNOSTIC_SYMBOL,\
PROPERTY_TYPE_POSITION_IN_STAFF\
= range(12)
def __str__(self):
return property_type_keys[self]
def __repr__(self):
return self.__str__()
property_type_keys = {
PropertyType.PROPERTY_TYPE_PAGES: "pages",\
PropertyType.PROPERTY_TYPE_REGIONS: "regions",\
PropertyType.PROPERTY_TYPE_REGION_TYPE: "type",\
PropertyType.PROPERTY_TYPE_BOUNDING_BOX: "bounding_box",\
PropertyType.PROPERTY_TYPE_BBOX_FROM_X: "fromX",\
PropertyType.PROPERTY_TYPE_BBOX_TO_X: "toX",\
PropertyType.PROPERTY_TYPE_BBOX_FROM_Y: "fromY",\
PropertyType.PROPERTY_TYPE_BBOX_TO_Y: "toY",\
PropertyType.PROPERTY_TYPE_STAFF_REGION: "staff",\
PropertyType.PROPERTY_TYPE_SYMBOLS: "symbols",\
PropertyType.PROPERTY_TYPE_AGNOSTIC_SYMBOL: "agnostic_symbol_type",\
PropertyType.PROPERTY_TYPE_POSITION_IN_STAFF: "position_in_staff"\
}
# =============================================================================
# Music symbol
# =============================================================================
class GTSymbol:
name_label = ""
position_in_staff = ""
def __i_integrity(self):
assert(type(self.name_label) is str)
assert(self.name_label != "")
assert(type(self.position_in_staff) is str)
assert(self.position_in_staff != "")
def __init__(self):
self.name_label = ""
self.position_in_staff = ""
def __str__(self):
self.__i_integrity()
return self.name_label + ":" + self.position_in_staff
def __repr__(self):
self.__i_integrity()
return self.__str__()
def fromDictionary(self, dictionary):
assert (type(dictionary) is dict)
self.name_label = dictionary[str(PropertyType.PROPERTY_TYPE_AGNOSTIC_SYMBOL)]
self.position_in_staff = dictionary[str(PropertyType.PROPERTY_TYPE_POSITION_IN_STAFF)]
self.__i_integrity()
# =============================================================================
# Music region
# =============================================================================
class GTRegion:
name_label = ""
coord_p1 = (0,0)
coord_p2 = (0,0)
symbols = []
def __i_integrity(self):
assert(type(self.name_label) is str)
assert(self.name_label != "")
assert(type(self.coord_p1) is tuple)
assert(type(self.coord_p2) is tuple)
assert(self.symbols is None or type(self.symbols) is list)
def __init__(self):
self.name_label = ""
self.coord_p1 = (0,0)
self.coord_p2 = (0,0)
def __str__(self):
self.__i_integrity()
return self.name_label + ":" + "(" + str(self.coord_p1) + "->" + str(self.coord_p2) + ")"
def __repr__(self):
self.__i_integrity()
return self.__str__()
def isNameInList(self, list_symbol_labels):
self.__i_integrity()
assert (type(list_symbol_labels) is list)
return self.name_label in list_symbol_labels
def getSRCSample(self, src_image):
self.__i_integrity()
if len(src_image.shape) == 3:
sample = src_image[self.coord_p1[0]:self.coord_p2[0], self.coord_p1[1]:self.coord_p2[1], :]
else:
sample = src_image[self.coord_p1[0]:self.coord_p2[0], self.coord_p1[1]:self.coord_p2[1]]
return sample
def getNameSymbol(self):
self.__i_integrity()
return self.name_label
def append_label_without_repetitions(self, list_symbol_labels):
self.__i_integrity()
assert (type(list_symbol_labels) is list)
if self.name_label not in list_symbol_labels:
list_symbol_labels.append(self.name_label)
def fromDictionary_bounding_box(self, dictionary):
assert (type(dictionary) is dict)
fromX = int(dictionary[str(PropertyType.PROPERTY_TYPE_BBOX_FROM_X)])
toX = int(dictionary[str(PropertyType.PROPERTY_TYPE_BBOX_TO_X)])
fromY = int(dictionary[str(PropertyType.PROPERTY_TYPE_BBOX_FROM_Y)])
toY = int(dictionary[str(PropertyType.PROPERTY_TYPE_BBOX_TO_Y)])
self.coord_p1 = (fromY, fromX)
self.coord_p2 = (toY, toX)
def fromDictionary(self, dictionary):
assert (type(dictionary) is dict)
info_bbox = dictionary[str(PropertyType.PROPERTY_TYPE_BOUNDING_BOX)]
self.fromDictionary_bounding_box(info_bbox)
self.name_label = dictionary[str(PropertyType.PROPERTY_TYPE_REGION_TYPE)]
if str(PropertyType.PROPERTY_TYPE_SYMBOLS) in dictionary:
info_symbols = dictionary[str(PropertyType.PROPERTY_TYPE_SYMBOLS)]
for info_symbol in info_symbols:
symbol = GTSymbol()
symbol.fromDictionary(info_symbol)
self.symbols.append(symbol)
self.__i_integrity()
class GTPage:
coord_p1 = (0,0)
coord_p2 = (0,0)
regions = []
def __i_integrity(self):
assert(type(self.coord_p1) is tuple)
assert(type(self.coord_p2) is tuple)
assert(self.regions is None or type(self.regions) is list)
def __init__(self):
self.coord_p1 = (0,0)
self.coord_p2 = (0,0)
self.regions = []
def hasRegions(self):
self.__i_integrity()
if (self.regions is None):
return False
else:
assert(len(self.regions) > 0)
return True
def getBBoxPage(self, considered_classes):
self.__i_integrity()
list_bbox = []
if considered_classes is None:
list_bbox.append((self.coord_p1[0], self.coord_p1[1], self.coord_p2[0], self.coord_p2[1]))
elif str(PropertyType.PROPERTY_TYPE_PAGES) in considered_classes:
list_bbox.append((self.coord_p1[0], self.coord_p1[1], self.coord_p2[0], self.coord_p2[1]))
for region in self.regions:
if considered_classes is None or region.isNameInList(considered_classes):
list_bbox.append((region.coord_p1[0], region.coord_p1[1], region.coord_p2[0], region.coord_p2[1]))
return list_bbox
def getListRegions(self, list_possible_region_names=None):
self.__i_integrity()
list_regions_considered = []
for region in self.regions:
if list_possible_region_names is None or region.isNameInList(list_possible_region_names):
list_regions_considered.append(region)
return list_regions_considered
def getListRegionNames(self):
self.__i_integrity()
list_labels = []
for region in self.regions:
region.append_label_without_repetitions(list_labels)
return list_labels
def __str__(self):
self.__i_integrity()
return "Page(" + str(self.coord_p1) + "->" + str(self.coord_p2) + ")"
def __repr__(self):
self.__i_integrity()
return self.__str__()
def isNameInList(self, list_symbol_labels):
self.__i_integrity()
assert (type(list_symbol_labels) is list)
return str(PropertyType.PROPERTY_TYPE_PAGES) in list_symbol_labels
def getSRCSample(self, src_image, sample_size=None):
self.__i_integrity()
if len(src_image.shape) == 3:
sample = src_image[self.coord_p1[0]:self.coord_p2[0], self.coord_p1[1]:self.coord_p2[1], :]
else:
sample = src_image[self.coord_p1[0]:self.coord_p2[0], self.coord_p1[1]:self.coord_p2[1]]
return sample
def fromDictionary_bounding_box(self, dictionary):
assert (type(dictionary) is dict)
fromX = int(dictionary[str(PropertyType.PROPERTY_TYPE_BBOX_FROM_X)])
toX = int(dictionary[str(PropertyType.PROPERTY_TYPE_BBOX_TO_X)])
fromY = int(dictionary[str(PropertyType.PROPERTY_TYPE_BBOX_FROM_Y)])
toY = int(dictionary[str(PropertyType.PROPERTY_TYPE_BBOX_TO_Y)])
self.coord_p1 = (fromY, fromX)
self.coord_p2 = (toY, toX)
def fromDictionary(self, dictionary):
assert (type(dictionary) is dict)
info_bbox = dictionary[str(PropertyType.PROPERTY_TYPE_BOUNDING_BOX)]
self.fromDictionary_bounding_box(info_bbox)
if (str(PropertyType.PROPERTY_TYPE_REGIONS) in dictionary):
list_info_regions = dictionary[str(PropertyType.PROPERTY_TYPE_REGIONS)]
for info_region in list_info_regions:
region = GTRegion()
region.fromDictionary(info_region)
self.regions.append(region)
else:
self.regions = []
self.__i_integrity()
def addGT(self, gt_im, considered_classes, vertical_reduction_regions=0.):
self.__i_integrity()
if str(PropertyType.PROPERTY_TYPE_PAGES) in considered_classes:
x_start = self.coord_p1[0]
y_start = self.coord_p1[1]
x_end = self.coord_p2[0]
y_end = self.coord_p2[1]
gt_im[x_start:x_end, y_start:y_end] = 1
for region in self.regions:
if region.name_label in considered_classes:
x_start = region.coord_p1[0]
y_start = region.coord_p1[1]
x_end = region.coord_p2[0]
y_end = region.coord_p2[1]
if vertical_reduction_regions is not None and vertical_reduction_regions > 0.:
vertical_region_size = x_end - x_start
vertical_reduction_region_side = int((vertical_reduction_regions * vertical_region_size) // 2)
x_start += int(vertical_reduction_region_side)
x_end -= int(vertical_reduction_region_side)
gt_im[x_start:x_end, y_start:y_end] = 1
class GTJSONReaderMuret:
filename = None
pages = []
def __i_integrity(self):
assert(self.filename is not None)
assert(type(self.filename) is str)
def __init__(self):
self.filename = None
self.pages = []
def hasRegions(self):
self.__i_integrity()
for page in self.pages:
if (page.hasRegions()):
return True
return False
def getFileName(self):
self.__i_integrity()
return self.filename
def getListRegionNames(self):
self.__i_integrity()
list_labels = []
for page in self.pages:
list_region_names_in_page = page.getListRegionNames()
for region_names_in_page in list_region_names_in_page:
if region_names_in_page not in list_labels:
list_labels.append(region_names_in_page)
return list_labels
def getListRegions(self, list_possible_region_names=None):
self.__i_integrity()
list_labels = []
for page in self.pages:
list_region_names_in_page = page.getListRegions(list_possible_region_names)
for region_names_in_page in list_region_names_in_page:
if region_names_in_page not in list_labels:
list_labels.append(region_names_in_page)
return list_labels
def getListBoundingBoxes(self, considered_classes=None):
self.__i_integrity()
list_bbox = []
for page in self.pages:
list_bbox_page = page.getBBoxPage(considered_classes)
for bbox_page in list_bbox_page:
list_bbox.append(bbox_page)
return list_bbox
def getListBoundingBoxesPerClass(self, considered_classes):
self.__i_integrity()
dict_bbox = {}
region_names = self.getListRegionNames()
if considered_classes is not None:
region_names = considered_classes
for region_name in region_names:
dict_bbox[region_name] = []
list_bbox_page = []
for page in self.pages:
for region_name in region_names:
list_bbox_page = page.getBBoxPage(list([region_name]))
for bbox_page in list_bbox_page:
dict_bbox[region_name].append(bbox_page)
return dict_bbox
def fromDictionary(self, dictionary):
assert (type(dictionary) is dict)
self.filename = dictionary["filename"]
if str(PropertyType.PROPERTY_TYPE_PAGES) in dictionary:
info_pages = dictionary[str(PropertyType.PROPERTY_TYPE_PAGES)]
for info_page in info_pages:
page = GTPage()
page.fromDictionary(info_page)
self.pages.append(page)
def load (self, js):
assert (isinstance(js, CustomJson))
dictionary = js.dictionary
self.fromDictionary(dictionary)
self.__i_integrity()
def generateGT(self, considered_classes, img_shape, vertical_reduction_regions = 0.):
assert(type(considered_classes) is list)
assert(type(img_shape) is tuple)
assert(len(img_shape) == 2)
gt_im = np.zeros(img_shape)
for page in self.pages:
page.addGT(gt_im, considered_classes, vertical_reduction_regions)
return gt_im
# =============================================================================
# PRINT
# =============================================================================
def __str__(self):
self.__i_integrity()
num_regions = len(self.getListBoundingBoxes())
num_classes = len(self.getListRegionNames())
return self.filename + ":" + "[" + str(self.pages) + "]" + str(num_regions) + " regions with " + str(num_classes) + " classes"
def __repr__(self):
return self.__str__()
if __name__ == "__main__":
# =============================================================================
# str_pathdir_json = "../databases/MURET/JSON/b-53-781/11591.JPG.json"
# str_pathdir_src = "../databases/MURET/SRC/b-53-781/11591.JPG.json"
# str_path_file_gt_out = "../databases/MURET/prueba/b-53-781/11591.JPG.json"
# generate_GTs_from_paths(str_path_file_src, str_path_file_json, str_path_file_gt_out)
#
# =============================================================================
json_pathfile = "datasets/dev/00525.JPG.json"
img_pathfile = "datasets/dev/00525.JPG"
js = CustomJson()
js.loadJson(json_pathfile)
gtjson = GTJSONReaderMuret()
gtjson.load(js)
print(gtjson)
bboxes = gtjson.getListBoundingBoxes(considered_classes=["staff", "empty-staff"])
print(bboxes)
im_src = FileManager.loadImage(img_pathfile, False)
img_shape = im_src.shape
gt_im = gtjson.generateGT(considered_classes=["staff", "empty-staff"], img_shape = img_shape, vertical_reduction_regions=0.)
FileManager.saveImageFullPath(gt_im*255, "pruebas/prueba.png")