-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextract_bbox_info.py
31 lines (25 loc) · 1.11 KB
/
extract_bbox_info.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
from PIL import Image
class_names = ['boneanomaly', 'bonelesion', 'foreignbody', 'fracture', 'metal', 'periostealreaction', 'pronatorsign', 'softtissue', 'text']
# function to convert YOLO format to dictionary
def yolo_to_dict(yolo_file, image_file):
img = Image.open(image_file)
width, height = img.size
# read YOLO file
with open(yolo_file, 'r') as f:
lines = f.readlines()
# create dictionary to store annotations
annotations = {}
for class_name in class_names:
annotations[class_name] = []
# read annotations from YOLO file
for line in lines:
line = line.strip().split()
if len(line) == 5:
class_id, x_center, y_center, bbox_width, bbox_height = map(float, line)
class_name = class_names[int(class_id)]
x_min = int((x_center - bbox_width/2) * width)
y_min = int((y_center - bbox_height/2) * height)
x_max = int((x_center + bbox_width/2) * width)
y_max = int((y_center + bbox_height/2) * height)
annotations[class_name].append((x_min, y_min, x_max, y_max))
return annotations