Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

Commit a7ddf79

Browse files
authored
Fix YOLO coordinate deviation of rotated rectangle (#73)
* Fix YOLO coordinate deviation of rotated rectangle * Update converter.py * rotation > 0
1 parent 824b648 commit a7ddf79

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

label_studio_converter/converter.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import json
33
import io
4+
import math
45
import logging
56
import pandas as pd
67
import xml.dom
@@ -610,10 +611,42 @@ def convert_to_yolo(self, input_data, output_dir, output_image_dir=None, output_
610611
category_id = category_name_to_id[category_name]
611612

612613
if "rectanglelabels" in label or 'labels' in label:
613-
x = (label['x'] + label['width']/2) / 100
614-
y = (label['y'] + label['height']/2) / 100
615-
w = label['width'] / 100
616-
h = label['height'] / 100
614+
label_x, label_y, label_w, label_h, label_r = (
615+
label["x"],
616+
label["y"],
617+
label["width"],
618+
label["height"],
619+
label["rotation"],
620+
)
621+
if abs(label_r) > 0:
622+
r = math.pi * label_r / 180
623+
sin_r = math.sin(r)
624+
cos_r = math.cos(r)
625+
h_sin_r, h_cos_r = label_h * sin_r, label_h * cos_r
626+
x_top_right = label_x + label_w * cos_r
627+
y_top_right = label_y + label_w * sin_r
628+
629+
x_ls = [
630+
label_x,
631+
x_top_right,
632+
x_top_right - h_sin_r,
633+
label_x - h_sin_r,
634+
]
635+
y_ls = [
636+
label_y,
637+
y_top_right,
638+
y_top_right + h_cos_r,
639+
label_y + h_cos_r,
640+
]
641+
label_x = max(0, min(x_ls))
642+
label_y = max(0, min(y_ls))
643+
label_w = min(100, max(x_ls)) - label_x
644+
label_h = min(100, max(y_ls)) - label_y
645+
646+
x = (label_x + label_w / 2) / 100
647+
y = (label_y + label_h / 2) / 100
648+
w = label_w / 100
649+
h = label_h / 100
617650
annotations.append([category_id, x, y, w, h])
618651
else:
619652
raise ValueError(f"Unknown label type {label}")

0 commit comments

Comments
 (0)