-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_labels_to_COCO.py
61 lines (47 loc) · 1.63 KB
/
convert_labels_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
50
51
52
53
54
55
56
57
58
59
60
61
import glob
import fiftyone as fo
images_patt = "/path/to/images/*"
# Ex: your custom label format
annotations = {
"/path/to/images/000001.jpg": [
{"bbox": ..., "label": ...},
...
],
...
}
# Create dataset
dataset = fo.Dataset(name="my-detection-dataset")
# Persist the dataset on disk in order to
# be able to load it in one line in the future
dataset.persistent = True
# Add your samples to the dataset
for filepath in glob.glob(images_patt):
sample = fo.Sample(filepath=filepath)
# Convert detections to FiftyOne format
detections = []
for obj in annotations[filepath]:
label = obj["label"]
# Bounding box coordinates should be relative values
# in [0, 1] in the following format:
# [top-left-x, top-left-y, width, height]
bounding_box = obj["bbox"]
detections.append(
fo.Detection(label=label, bounding_box=bounding_box)
)
# Store detections in a field name of your choice
sample["ground_truth"] = fo.Detections(detections=detections)
dataset.add_sample(sample)
export_dir = "/path/for/coco-detection-dataset"
label_field = "ground_truth" # for example
# Export the dataset
dataset.export(
export_dir=export_dir,
dataset_type=fo.types.COCODetectionDataset,
label_field=label_field,
)
# Convert a COCO detection dataset to CVAT image format
fiftyone convert \
--input-dir /mnt/disk1/datasets/iNaturalist/Arthropods/LIMIT1/dataset \
--input-type fiftyone.types.YOLOv5Dataset \
--output-dir /mnt/disk1/datasets/iNaturalist/Arthropods/LIMIT1/tmp \
--output-type fiftyone.types.COCODetectionDataset