HumanSignal / label-studio-converter

Tools for converting Label Studio annotations into common dataset formats
https://labelstud.io/
255 stars 132 forks source link

YOLO Export: rotated rectangle annotationes are deformed for non quadratic images #101

Open ferenc-hechler opened 2 years ago

ferenc-hechler commented 2 years ago

A detailed description with screenshots can be found in the label-studio issue https://github.com/heartexlabs/label-studio/issues/2293

For example the following label-studio annotation

"annotations": [
    {
        ...
        "result": [
            {
                "original_width": 1000,
                "original_height": 500,
                ...
                "value": {
                    "x": 20,
                    "y": 20,
                    "width": 30,
                    "height": 20,
                    "rotation": 90,
                    "rectanglelabels": [
                        "EVS"
                    ]
                },
                ...
            },
        ...

Will be exported as `

0.1 0.35 0.2 0.3 ` But correct would be ` 0.15 0.5 0.1 0.6 ` The problem is the non-quadratic aspect ratio of the original-image. The rotation is done without first correcting the aspect. The corresponding code can be found here: https://github.com/heartexlabs/label-studio-converter/blob/598420012b5cb6e9cd5283e62887ff33af36d0bb/label_studio_converter/converter.py#L649 A possible fix would be to first correct the aspect and then do the rotation and afterwards return to the percentage scale: ``` if abs(label_r) > 0: r = math.pi * label_r / 180 label_x = label_x * original_width label_y = label_y * original_height label_w = label_w * original_width label_h = label_h * original_height sin_r = math.sin(r) cos_r = math.cos(r) h_sin_r, h_cos_r = label_h * sin_r, label_h * cos_r x_top_right = label_x + label_w * cos_r y_top_right = label_y + label_w * sin_r x_ls = [ label_x, x_top_right, x_top_right - h_sin_r, label_x - h_sin_r, ] y_ls = [ label_y, y_top_right, y_top_right + h_cos_r, label_y + h_cos_r, ] label_x = max(0, min(x_ls)) / original_width label_y = max(0, min(y_ls)) / original_height label_w = min(100*original_width, max(x_ls)) / original_width - label_x label_h = min(100*original_height, max(y_ls)) / original_height - label_y x = (label_x + label_w / 2) / 100 y = (label_y + label_h / 2) / 100 w = label_w / 100 h = label_h / 100 ```
ferenc-hechler commented 2 years ago

Created Pull-Request #103