Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to save labels in yolo fomat #156

Open
jugal-sheth opened this issue Nov 17, 2023 · 1 comment
Open

How to save labels in yolo fomat #156

jugal-sheth opened this issue Nov 17, 2023 · 1 comment

Comments

@jugal-sheth
Copy link

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

@jugal-sheth jugal-sheth changed the title How to save in yolo fomat How to save labels in yolo fomat Nov 17, 2023
@marcocaggioni
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants