anylabeling icon indicating copy to clipboard operation
anylabeling copied to clipboard

How to save labels in yolo fomat

Open jugal-sheth opened this issue 2 years ago • 1 comments

instead of saving labels as json file i need to save it in YOLO format <class_label>

jugal-sheth avatar Nov 17 '23 10:11 jugal-sheth

to convert the json label to the yolo txt labels I use:

import json
from pathlib import Path

def create_yolo_label(file, class_dict):

    with open(file, "r") as json_file:
        data = json.load(json_file)

    height=data['imageHeight']
    width=data['imageWidth']

    with open(f"{file.stem}.txt", "a") as yolo_label_file:
        for segment in data['shapes']:
            label_index=class_dict[segment['label']]
            label_line = f'{label_index} ' + ' '.join(f'{x/width} {y/height} ' for x,y in segment['points'])
            yolo_label_file.write(f'{label_line}\n')

filelist=list(Path('./').glob('*.json'))
class_dict={'dog':0} #example sigle class

for file in filelist:
    create_yolo_label(file, class_dict=class_dict)

this script will look for all the json files in the folder and save for each a .txt file in the yolov8 format

marcocaggioni avatar Apr 26 '24 01:04 marcocaggioni