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 does the YOLO test result return to the annotated txt file format? #760

Open
1 task done
Aq114 opened this issue Jul 8, 2024 · 1 comment
Open
1 task done
Labels
question A HUB question that does not involve a bug

Comments

@Aq114
Copy link

Aq114 commented Jul 8, 2024

Search before asking

Question

I have conducted preliminary training on your platform and program to define the data set, and obtained some pictures of prediction results. However, I want to return the format of the annotation file from these results. What should I do?

Additional

There are categories for drawing borders and predictions in the test result picture.

@Aq114 Aq114 added the question A HUB question that does not involve a bug label Jul 8, 2024
@pderrenger
Copy link
Member

@Aq114 hello!

Thank you for reaching out with your question. It's great to hear that you've been working with the Ultralytics platform and achieving prediction results! 😊

To convert the YOLO test results back into the annotated .txt file format, you can follow these steps:

  1. Run Inference and Get Results:
    First, ensure you have the inference results in JSON format. You can use the Ultralytics HUB Inference API to get these results. Here's a quick Python example to get you started:

    import requests
    
    # API URL, use actual MODEL_ID
    url = "https://api.ultralytics.com/v1/predict/MODEL_ID"
    
    # Headers, use actual API_KEY
    headers = {"x-api-key": "API_KEY"}
    
    # Inference arguments (optional)
    data = {"size": 640, "confidence": 0.25, "iou": 0.45}
    
    # Load image and send request
    with open("path/to/image.jpg", "rb") as image_file:
        files = {"image": image_file}
        response = requests.post(url, headers=headers, files=files, data=data)
    
    results = response.json()
  2. Extract and Convert Results:
    Once you have the results, you can extract the necessary information and convert it into the YOLO annotation format. The YOLO format typically includes the class, center x, center y, width, and height of the bounding box, all normalized by the image dimensions.

    Here's a sample code snippet to convert the results:

    import json
    
    def convert_to_yolo_format(results, image_width, image_height):
        annotations = []
        for detection in results['data']:
            class_id = detection['class']
            x_center = detection['xcenter'] * image_width
            y_center = detection['ycenter'] * image_height
            width = detection['width'] * image_width
            height = detection['height'] * image_height
            annotations.append(f"{class_id} {x_center} {y_center} {width} {height}")
        return annotations
    
    # Assuming you have the image dimensions
    image_width = 640
    image_height = 480
    
    yolo_annotations = convert_to_yolo_format(results, image_width, image_height)
    
    # Save to a .txt file
    with open("path/to/annotations.txt", "w") as file:
        for annotation in yolo_annotations:
            file.write(f"{annotation}\n")
  3. Verify and Adjust:
    Ensure that the image dimensions (image_width and image_height) are correctly set to match your input image. The annotations will be saved in the YOLO format in the specified .txt file.

If you encounter any issues or if the problem persists, please make sure you are using the latest versions of the Ultralytics packages and the YOLO model. Updates often include important bug fixes and improvements.

For more detailed information, you can refer to the Ultralytics HUB Inference API documentation.

Feel free to reach out if you have any further questions. Happy coding! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question A HUB question that does not involve a bug
Projects
None yet
Development

No branches or pull requests

2 participants