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 use this repo ? #98

Open
ShreyasChhetri opened this issue Jul 2, 2024 · 7 comments
Open

How to use this repo ? #98

ShreyasChhetri opened this issue Jul 2, 2024 · 7 comments

Comments

@ShreyasChhetri
Copy link

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
Copy link

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
Copy link
Member

@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
Copy link

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
Copy link
Member

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! 😊

@VictorGimenez
Copy link

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

@pderrenger
Copy link
Member

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
Copy link

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?

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

4 participants