From 4c024214b27d45f445e6ffde9d13cebc7f67edd8 Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Tue, 16 May 2023 23:51:08 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9A=20shell=20of=20YOLO=20format=20pro?= =?UTF-8?q?cessing=20added?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervision/dataset/formats/yolo.py | 40 ++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/supervision/dataset/formats/yolo.py b/supervision/dataset/formats/yolo.py index e47aefd8..3ed36690 100644 --- a/supervision/dataset/formats/yolo.py +++ b/supervision/dataset/formats/yolo.py @@ -1,9 +1,10 @@ from pathlib import Path -from typing import Dict, List, Tuple, Union +from typing import Dict, List, Tuple, Union, Optional import cv2 import numpy as np +from supervision.dataset.ultils import approximate_mask_with_polygons from supervision.detection.core import Detections from supervision.detection.utils import polygon_to_mask, polygon_to_xyxy from supervision.file import list_files_with_extensions, read_txt_file @@ -161,5 +162,42 @@ def load_yolo_annotations( return classes, images, annotations +def object_to_pascal_voc( + xyxy: np.ndarray, + class_id: int, + image_shape: Tuple[int, int, int], + polygon: Optional[np.ndarray] = None +) -> str: + pass + + +def detections_to_yolo_annotations( + detections: Detections, + image_shape: Tuple[int, int, int], + min_image_area_percentage: float = 0.0, + max_image_area_percentage: float = 1.0, + approximation_percentage: float = 0.75, +) -> str: + annotation = [] + for xyxy, mask, _, class_id, _ in detections: + if mask is not None: + polygons = approximate_mask_with_polygons( + mask=mask, + min_image_area_percentage=min_image_area_percentage, + max_image_area_percentage=max_image_area_percentage, + approximation_percentage=approximation_percentage, + ) + for polygon in polygons: + xyxy = polygon_to_xyxy(polygon=polygon) + next_object = object_to_pascal_voc( + xyxy=xyxy, class_id=class_id, image_shape=image_shape, polygon=polygon + ) + annotation.append(next_object) + else: + next_object = object_to_pascal_voc(xyxy=xyxy, class_id=class_id, image_shape=image_shape) + annotation.append(next_object) + return "\n".join(annotation) + + def save_yolo_annotations(): pass