|
1 | 1 | import os |
2 | 2 | import json |
3 | 3 | import io |
| 4 | +import math |
4 | 5 | import logging |
5 | 6 | import pandas as pd |
6 | 7 | import xml.dom |
@@ -610,10 +611,42 @@ def convert_to_yolo(self, input_data, output_dir, output_image_dir=None, output_ |
610 | 611 | category_id = category_name_to_id[category_name] |
611 | 612 |
|
612 | 613 | 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 |
617 | 650 | annotations.append([category_id, x, y, w, h]) |
618 | 651 | else: |
619 | 652 | raise ValueError(f"Unknown label type {label}") |
|
0 commit comments