forked from supervisely-ecosystem/RT-DETR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_to_coco.py
49 lines (44 loc) · 1.65 KB
/
convert_to_coco.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
import supervisely as sly
from pycocotools.coco import COCO
def get_coco_annotations(dataset: sly.Dataset, meta: sly.ProjectMeta, selected_classes: list):
coco_anno = {"images": [], "categories": [], "annotations": []}
cat2id = {name: i for i, name in enumerate(selected_classes)}
img_id = 1
ann_id = 1
for name in dataset.get_items_names():
ann = dataset.get_ann(name, meta)
img_dict = {
"id": img_id,
"height": ann.img_size[0],
"width": ann.img_size[1],
"file_name": name
}
coco_anno["images"].append(img_dict)
for label in ann.labels:
if isinstance(label.geometry, (sly.Bitmap, sly.Polygon)):
rect = label.geometry.to_bbox()
elif isinstance(label.geometry, sly.Rectangle):
rect = label.geometry
else:
continue
class_name = label.obj_class.name
if class_name not in selected_classes:
continue
x,y,x2,y2 = rect.left, rect.top, rect.right, rect.bottom
ann_dict = {
"id": ann_id,
"image_id": img_id,
"category_id": cat2id[class_name],
"bbox": [x, y, x2 - x, y2 - y],
"area": (x2 - x) * (y2 - y),
"iscrowd": 0
}
coco_anno["annotations"].append(ann_dict)
ann_id += 1
img_id += 1
coco_anno["categories"] = [{"id": i, "name": name} for name, i in cat2id.items()]
# Test:
coco_api = COCO()
coco_api.dataset = coco_anno
coco_api.createIndex()
return coco_anno