-
Notifications
You must be signed in to change notification settings - Fork 6
/
remove_categories_scratch_test.py
90 lines (84 loc) · 2.84 KB
/
remove_categories_scratch_test.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
from annotation_utils.coco.structs import COCO_Dataset, \
COCO_License, COCO_Image, COCO_Annotation, COCO_Category
from common_utils.common_types.bbox import BBox
from logger import logger
dataset = COCO_Dataset.new(description='Test')
dataset.categories.append(
COCO_Category(
id=len(dataset.categories),
supercategory='test_category',
name='category_a'
)
)
dataset.categories.append(
COCO_Category(
id=len(dataset.categories),
supercategory='test_category',
name='category_b'
)
)
dataset.categories.append(
COCO_Category(
id=len(dataset.categories),
supercategory='test_category',
name='category_c'
)
)
for i in range(10):
dataset.licenses.append(
COCO_License(url=f'test_license_{i}', name=f'Test License {i}', id=len(dataset.licenses))
)
for i in range(20):
dataset.images.append(
COCO_Image(
license_id=i%len(dataset.licenses),
file_name=f'{i}.jpg',
coco_url=f'/path/to/{i}.jpg',
height=500,
width=500,
date_captured='N/A',
flickr_url=None,
id=len(dataset.images)
)
)
for i in range(len(dataset.images)):
if i % 2 == 0:
dataset.annotations.append(
COCO_Annotation(
category_id=dataset.categories.get_unique_category_from_name('category_a').id,
image_id=dataset.images[i].id,
bbox=BBox(xmin=0, ymin=0, xmax=100, ymax=100),
id=len(dataset.annotations)
)
)
dataset.annotations.append(
COCO_Annotation(
category_id=dataset.categories.get_unique_category_from_name('category_b').id,
image_id=dataset.images[i].id,
bbox=BBox(xmin=0, ymin=0, xmax=100, ymax=100),
id=len(dataset.annotations)
)
)
else:
dataset.annotations.append(
COCO_Annotation(
category_id=dataset.categories.get_unique_category_from_name('category_b').id,
image_id=dataset.images[i].id,
bbox=BBox(xmin=0, ymin=0, xmax=100, ymax=100),
id=len(dataset.annotations)
)
)
dataset.annotations.append(
COCO_Annotation(
category_id=dataset.categories.get_unique_category_from_name('category_c').id,
image_id=dataset.images[i].id,
bbox=BBox(xmin=0, ymin=0, xmax=100, ymax=100),
id=len(dataset.annotations)
)
)
logger.purple('Before:')
dataset.print_handler_lengths()
dataset.remove_categories_by_name(category_names=['category_b', 'category_c'], verbose=True)
logger.purple('After:')
dataset.print_handler_lengths()
dataset.save_to_path(save_path='remove_test.json', overwrite=True)