-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson2txt.py
51 lines (42 loc) · 1.94 KB
/
json2txt.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
import json
import os
from PIL import Image
# Define a function to convert bounding boxes to YOLO format
def convert_bbox_to_yolo(image_width, image_height, xmin, ymin, xmax, ymax):
x_center = (xmin + xmax) / 2.0 / image_width
y_center = (ymin + ymax) / 2.0 / image_height
width = (xmax - xmin) / image_width
height = (ymax - ymin) / image_height
return x_center, y_center, width, height
# Paths to the directories
labels_dir = './data/dataset/labels'
images_dir = './data/dataset/images'
output_dir = './data/dataset/yolo_labels'
# Create output directory if it doesn't exist
os.makedirs(output_dir, exist_ok=True)
# Iterate through each label file
for label_file in os.listdir(labels_dir):
if label_file.endswith('.json'):
label_path = os.path.join(labels_dir, label_file)
image_name = label_file.replace('.json', '.jpg') # Adjust extension if different
image_path = os.path.join(images_dir, image_name)
# Load JSON label file
with open(label_path, 'r') as file:
data = json.load(file)
# Open image to get its dimensions
with Image.open(image_path) as img:
image_width, image_height = img.size
# Convert each bounding box and save to corresponding text file
yolo_annotations = []
for annotation in data:
label = annotation['label']
xmin = annotation['xmin']
ymin = annotation['ymin']
xmax = annotation['xmax']
ymax = annotation['ymax']
x_center, y_center, width, height = convert_bbox_to_yolo(image_width, image_height, xmin, ymin, xmax, ymax)
yolo_annotations.append(f"{label} {x_center} {y_center} {width} {height}")
# Write YOLO annotations to a text file
yolo_label_path = os.path.join(output_dir, label_file.replace('.json', '.txt'))
with open(yolo_label_path, 'w') as yolo_file:
yolo_file.write('\n'.join(yolo_annotations))