-
Notifications
You must be signed in to change notification settings - Fork 95
/
create_annotations.py
54 lines (44 loc) · 1.39 KB
/
create_annotations.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
from pathlib import Path
def create_image_annotation(file_path: Path, width: int, height: int, image_id: int):
file_path = file_path.name
image_annotation = {
"file_name": file_path,
"height": height,
"width": width,
"id": image_id,
}
return image_annotation
def create_annotation_from_yolo_format(
min_x, min_y, width, height, image_id, category_id, annotation_id, segmentation=True
):
bbox = (float(min_x), float(min_y), float(width), float(height))
area = width * height
max_x = min_x + width
max_y = min_y + height
if segmentation:
seg = [[min_x, min_y, max_x, min_y, max_x, max_y, min_x, max_y]]
else:
seg = []
annotation = {
"id": annotation_id,
"image_id": image_id,
"bbox": bbox,
"area": area,
"iscrowd": 0,
"category_id": category_id,
"segmentation": seg,
}
return annotation
def create_annotation_from_yolo_results_format(
min_x, min_y, width, height, image_id, category_id, conf
):
bbox = (float(min_x), float(min_y), float(width), float(height))
annotation = [{
"image_id": image_id,
"category_id": category_id,
"bbox": bbox,
"score": conf
}]
return annotation
# Create the annotations of the ECP dataset (Coco format)
coco_format = {"images": [{}], "categories": [], "annotations": [{}]}