Skip to content
Snippets Groups Projects
Commit c709332f authored by Chevalier Paul's avatar Chevalier Paul
Browse files

Ajout d'un convertisseur de JSON à txt lisible par YOLOv5

parent a58c1618
No related branches found
No related tags found
No related merge requests found
import json
'''proposition de chatgpt pour convertir les JSON en txt afin de le donner à YOLOv5 qui ne prend que du txt pour les données'''
# Charger le fichier JSON
with open('annotations.json') as f:
data = json.load(f)
for item in data['annotations']:
# Assurez-vous d'extraire les coordonnées bbox (xmin, ymin, xmax, ymax) et les classes
bbox = item['bbox']
class_id = item['class_id']
# YOLO format: class_id, x_center, y_center, width, height (normalisé entre 0 et 1)
x_center = (bbox['x_min'] + bbox['x_max']) / 2
y_center = (bbox['y_min'] + bbox['y_max']) / 2
width = bbox['x_max'] - bbox['x_min']
height = bbox['y_max'] - bbox['y_min']
# Créer un fichier .txt au format YOLO
with open(f"{item['image_id']}.txt", "w") as f_out:
f_out.write(f"{class_id} {x_center} {y_center} {width} {height}")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment