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

Issue with Exporting Polygon Annotations to YOLO Format #365

Open
chen-ricky opened this issue Dec 4, 2024 · 3 comments
Open

Issue with Exporting Polygon Annotations to YOLO Format #365

chen-ricky opened this issue Dec 4, 2024 · 3 comments

Comments

@chen-ricky
Copy link

chen-ricky commented Dec 4, 2024

Description:

Hi there,

I’m encountering an issue when exporting polygon annotations from Label Studio to YOLO format. My task involves labeling polygons around objects (in this case, solar panels), and when I attempt to export the annotations, I receive the following error:

KeyError: 'points'

It seems that the export process is looking for a points key, which is expected for bounding box annotations, but the annotations I have are polygons. The converter appears to be trying to process the polygon annotations as if they were bounding boxes, leading to the error.

Here is a snippet of the labeling configuration used in my task:

<View>
  <Image name="image" value="$image" zoom="true"/>
  <PolygonLabels model_path="my/model/path.pt" name="label" toName="image" model_score_threshold="0.1" opacity="0.1">
    <Label value="panel" background="blue"/>
  </PolygonLabels>
</View>

Also, as you may guess from the above xml, I've made initial predictions for the images before start labeling.

Expected Behavior:

I am looking to export the annotations in YOLO format, which expects bounding boxes with coordinates normalized to the image size. The polygons should be converted into bounding boxes before exporting.

Current Behavior:

When exporting, the process fails because the export function tries to access the points key for polygons, but this key is not defined in the format expected for YOLO.

Steps to Reproduce:

  • Create a project with polygon annotations using the <PolygonLabels> tag.
  • Attempt to export the data to YOLO format.
  • Encounter the KeyError: 'points' error.
    I’ve attached a screenshot showing the issue in case it helps in diagnosing the problem.

Screenshot from 2024-12-04 11-39-47

Is there a way to handle polygon annotations properly when exporting to YOLO format? Or would I need to implement a custom conversion function to transform polygons into bounding boxes?

Thanks for your help!

@rezakarbasi
Copy link

I've seen this issue before.

When using an AI model for predictions, it's possible that some panels may have zero points, which leads to the error you're encountering. This happens because the polygons for those panels don't contain any points, and as a result, the code tries to process them and raises an error.

To resolve this, you can add a condition to check if the polygon has points. If not, you can skip those labels by using a continue statement in the loop to move to the next annotation.

elif "polygonlabels" in label or "polygon" in label:
points_abs = [(x / 100, y / 100) for x, y in label["points"]]
annotations.append(
[category_id]
+ [coord for point in points_abs for coord in point]
)

Here’s a quick example of what that might look like:

 elif "polygonlabels" in label or "polygon" in label:
     if 'points' not in label:
          continue
     points_abs = [(x / 100, y / 100) for x, y in label["points"]] 
     annotations.append( 
         [category_id] 
         + [coord for point in points_abs for coord in point] 
     ) 

This should prevent the error from occurring when there are polygons without points. Let me know if that works for you!

I'll make a pull request for that and will inform you!

@chen-ricky
Copy link
Author

chen-ricky commented Dec 7, 2024

@rezakarbasi
Seems the problem's been solved for me. Thanks

@rezakarbasi
Copy link

@xtrementl @makseq @nick-skriabin
Do you see any problems in what I added to the project that you haven't approved the pull-request yet?

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