ultralytics / JSON2YOLO

Convert JSON annotations into YOLO format.
https://docs.ultralytics.com
GNU Affero General Public License v3.0
863 stars 232 forks source link

How to use this repo ? #98

Open ShreyasChhetri opened 4 months ago

ShreyasChhetri commented 4 months ago

HI @glenn-jocher I have downloaded the requirements.txt file but nowhere in the description can I see how to use or run this repo what code I have to use to change my JSON files to txt files so please help regarding this

VictorGimenez commented 3 months ago

I am in the same boat, looking for any tutorial in how to use this repo and convert the JSON(VGG Image Annotator)/COCO format into YOLO (.pt). Unfortunately no one put any tutorial about how to use this repository and what should we run to convert

pderrenger commented 3 months ago

@VictorGimenez hello!

Thank you for reaching out. To convert JSON (VGG Image Annotator/COCO) format annotations to YOLO format, you can follow these steps:

  1. Install the required packages: Ensure you have all the necessary dependencies installed. You can do this by running:

    pip install -r requirements.txt
  2. Prepare your JSON files: Make sure your JSON files are correctly formatted according to the VGG Image Annotator or COCO specifications.

  3. Use the conversion script: Ultralytics provides a script to convert annotations. You can use the coco2yolo.py script available in the repository. Here is an example of how you can run it:

    python path/to/coco2yolo.py --json_path path/to/your/json_file.json --output_path path/to/output_directory

    Replace path/to/coco2yolo.py, path/to/your/json_file.json, and path/to/output_directory with the actual paths on your system.

  4. Verify the conversion: After running the script, check the output directory for the converted .txt files. These files should now be in the YOLO format.

If you encounter any issues or if the script does not work as expected, please ensure you are using the latest version of the repository. You can update your local copy by running:

git pull origin master

Feel free to ask if you have any more questions or need further assistance. The YOLO community and the Ultralytics team are here to help! 😊

VictorGimenez commented 3 months ago

Hi @pderrenger! Thanks for the feedback!

Where is the coco2yolo.py script located? I am trying to find it here in this GitHub but can't see it, I found it implemented by other people but it isn't the version from Ultralytics.

I would like to know the format of the desired input JSON file so that the script works!

Thanks again for the response!

pderrenger commented 3 months ago

Hi @VictorGimenez! Thanks for your patience and for reaching out.

It looks like the coco2yolo.py script isn't directly available in the Ultralytics repository. However, you can easily convert COCO JSON annotations to YOLO format using a custom script. Below is a simple example to help you get started:

import json
import os

def convert_coco_to_yolo(json_path, output_path):
    with open(json_path) as f:
        data = json.load(f)

    if not os.path.exists(output_path):
        os.makedirs(output_path)

    for image in data['images']:
        image_id = image['id']
        file_name = image['file_name']
        width = image['width']
        height = image['height']

        annotations = [ann for ann in data['annotations'] if ann['image_id'] == image_id]

        with open(os.path.join(output_path, f"{os.path.splitext(file_name)[0]}.txt"), 'w') as f:
            for ann in annotations:
                category_id = ann['category_id']
                bbox = ann['bbox']
                x_center = (bbox[0] + bbox[2] / 2) / width
                y_center = (bbox[1] + bbox[3] / 2) / height
                w = bbox[2] / width
                h = bbox[3] / height
                f.write(f"{category_id} {x_center} {y_center} {w} {h}\n")

json_path = 'path/to/your/coco_annotations.json'
output_path = 'path/to/output_directory'
convert_coco_to_yolo(json_path, output_path)

Steps to Use the Script:

  1. Save the script: Save the above script as coco2yolo.py.
  2. Run the script: Execute the script by running:
    python coco2yolo.py

    Make sure to replace json_path and output_path with the actual paths to your COCO JSON file and the desired output directory.

Input JSON Format:

The input JSON file should follow the COCO format, which typically includes:

If you encounter any issues, please ensure you are using the latest version of the Ultralytics package. You can update it by running:

pip install --upgrade ultralytics

Feel free to reach out if you have any more questions or need further assistance. The YOLO community and the Ultralytics team are here to help! 😊

VictorGimenez commented 3 months ago

Greeat!! Many thanks for the help!!! I am gonna give a test on it and come back here after to say the results ;)

pderrenger commented 3 months ago

You're welcome! 😊 I'm glad I could help. Please go ahead and test the script with your dataset. If you encounter any issues or have further questions, feel free to come back and share your results or any challenges you face.

Also, make sure you are using the latest version of the Ultralytics package to avoid any compatibility issues. You can update it by running:

pip install --upgrade ultralytics

Looking forward to hearing how it goes! 🚀

cena001plus commented 3 months ago

Hi @VictorGimenez! Thanks for your patience and for reaching out.

It looks like the coco2yolo.py script isn't directly available in the Ultralytics repository. However, you can easily convert COCO JSON annotations to YOLO format using a custom script. Below is a simple example to help you get started:

import json
import os

def convert_coco_to_yolo(json_path, output_path):
    with open(json_path) as f:
        data = json.load(f)

    if not os.path.exists(output_path):
        os.makedirs(output_path)

    for image in data['images']:
        image_id = image['id']
        file_name = image['file_name']
        width = image['width']
        height = image['height']

        annotations = [ann for ann in data['annotations'] if ann['image_id'] == image_id]

        with open(os.path.join(output_path, f"{os.path.splitext(file_name)[0]}.txt"), 'w') as f:
            for ann in annotations:
                category_id = ann['category_id']
                bbox = ann['bbox']
                x_center = (bbox[0] + bbox[2] / 2) / width
                y_center = (bbox[1] + bbox[3] / 2) / height
                w = bbox[2] / width
                h = bbox[3] / height
                f.write(f"{category_id} {x_center} {y_center} {w} {h}\n")

json_path = 'path/to/your/coco_annotations.json'
output_path = 'path/to/output_directory'
convert_coco_to_yolo(json_path, output_path)

Steps to Use the Script:

  1. Save the script: Save the above script as coco2yolo.py.
  2. Run the script: Execute the script by running:

    python coco2yolo.py

    Make sure to replace json_path and output_path with the actual paths to your COCO JSON file and the desired output directory.

Input JSON Format:

The input JSON file should follow the COCO format, which typically includes:

  • images: A list of image dictionaries with id, file_name, width, and height.
  • annotations: A list of annotation dictionaries with image_id, category_id, and bbox (bounding box in [x, y, width, height] format).

If you encounter any issues, please ensure you are using the latest version of the Ultralytics package. You can update it by running:

pip install --upgrade ultralytics

Feel free to reach out if you have any more questions or need further assistance. The YOLO community and the Ultralytics team are here to help! 😊 with open(os.path.join(output_path, f"{os.path.splitext(file_name)[0]}.txt"), 'w') as f: work?

raahim-syed commented 1 week ago

My Final Year Project in college is near its end and I've been really struggling to convert the keypoints from COCO to YOLO format, is there any script available that could help out. Its easy to find ones which are converting bounding boxes or segmentation masks but not for keypoints.

Secondly, to floating points matter in conversion, my convertor does not convert to floating point 8, but does that make a difference.

Any support would be much appreciated.

pderrenger commented 1 week ago

Converting keypoints from COCO to YOLO format isn't directly supported in the Ultralytics repo, but you can adapt existing scripts for bounding boxes by modifying them to handle keypoints. Regarding floating points, precision can affect model performance, so it's generally best to maintain as much precision as possible. If you encounter issues, ensure you're using the latest version of the Ultralytics package.

raahim-syed commented 1 week ago

Do you know guide that could help in achieving this? Do share any code aswell.

pderrenger commented 1 week ago

For converting keypoints from COCO to YOLO format, you might need to adapt existing scripts for bounding boxes. Unfortunately, there isn't a specific guide for keypoints, but you can start by modifying the bounding box conversion logic to handle keypoints. If you need further assistance, please ensure you're using the latest version of the Ultralytics package.