From d5f358ddd20e29f850b564502ec18188ad9c9952 Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Thu, 1 Aug 2024 15:24:45 +0300 Subject: [PATCH 1/7] added train, val, test directory paths for yolo data.yaml --- supervision/dataset/formats/yolo.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/supervision/dataset/formats/yolo.py b/supervision/dataset/formats/yolo.py index 0ecbac4b5..79175f449 100644 --- a/supervision/dataset/formats/yolo.py +++ b/supervision/dataset/formats/yolo.py @@ -267,6 +267,12 @@ def save_yolo_annotations( def save_data_yaml(data_yaml_path: str, classes: List[str]) -> None: - data = {"nc": len(classes), "names": classes} + data = { + "nc": len(classes), + "names": classes, + "train": "train/images", + "val": "valid/images", + "test": "test/images" + } Path(data_yaml_path).parent.mkdir(parents=True, exist_ok=True) save_yaml_file(data=data, file_path=data_yaml_path) From a190149b5fd04f769e98cbb74deaac17e0986898 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 1 Aug 2024 12:53:23 +0000 Subject: [PATCH 2/7] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto=20?= =?UTF-8?q?format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervision/dataset/formats/yolo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/supervision/dataset/formats/yolo.py b/supervision/dataset/formats/yolo.py index 79175f449..ae8aa4ab0 100644 --- a/supervision/dataset/formats/yolo.py +++ b/supervision/dataset/formats/yolo.py @@ -272,7 +272,7 @@ def save_data_yaml(data_yaml_path: str, classes: List[str]) -> None: "names": classes, "train": "train/images", "val": "valid/images", - "test": "test/images" + "test": "test/images", } Path(data_yaml_path).parent.mkdir(parents=True, exist_ok=True) save_yaml_file(data=data, file_path=data_yaml_path) From 2770acc70616ba2122cd7acc16f368bffab37842 Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Wed, 7 Aug 2024 13:19:03 +0300 Subject: [PATCH 3/7] added subset_type argument to as_yolo() --- supervision/dataset/core.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/supervision/dataset/core.py b/supervision/dataset/core.py index 7d320bbc9..ef7348723 100644 --- a/supervision/dataset/core.py +++ b/supervision/dataset/core.py @@ -504,6 +504,7 @@ def as_yolo( images_directory_path: Optional[str] = None, annotations_directory_path: Optional[str] = None, data_yaml_path: Optional[str] = None, + subset_type: Optional[str] = None, min_image_area_percentage: float = 0.0, max_image_area_percentage: float = 1.0, approximation_percentage: float = 0.0, @@ -523,6 +524,11 @@ def as_yolo( data_yaml_path (Optional[str]): The path where the data.yaml file should be saved. If not provided, the file will not be saved. + subset_type (Optional[str]): The subset type of the exported + dataset. + Accepted values: "train", "val", "test" + If not provided, the subset dataset path will not be + added or modified in data yaml file. min_image_area_percentage (float): The minimum percentage of detection area relative to the image area for a detection to be included. From 511578c6baab45e77bd27993ad06ea2824e85170 Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Wed, 7 Aug 2024 13:20:46 +0300 Subject: [PATCH 4/7] added subset_type argument to save_data_yaml() --- supervision/dataset/formats/yolo.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/supervision/dataset/formats/yolo.py b/supervision/dataset/formats/yolo.py index ae8aa4ab0..60115c7ea 100644 --- a/supervision/dataset/formats/yolo.py +++ b/supervision/dataset/formats/yolo.py @@ -1,6 +1,6 @@ import os from pathlib import Path -from typing import TYPE_CHECKING, Dict, List, Optional, Tuple +from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union import cv2 import numpy as np @@ -266,13 +266,14 @@ def save_yolo_annotations( save_text_file(lines=lines, file_path=yolo_annotations_path) -def save_data_yaml(data_yaml_path: str, classes: List[str]) -> None: - data = { - "nc": len(classes), - "names": classes, - "train": "train/images", - "val": "valid/images", - "test": "test/images", - } +def save_data_yaml(data_yaml_path: str, classes: List[str], subset_type: Union[str, None]) -> None: + if Path(data_yaml_path).exists(): + data = read_yaml_file(data_yaml_path) + else: + data = {} + data["nc"] = len(classes) + data["names"] = classes + if subset_type is not None: + data[subset_type] = f"{subset_type}/images" Path(data_yaml_path).parent.mkdir(parents=True, exist_ok=True) save_yaml_file(data=data, file_path=data_yaml_path) From 4d5fb6c8d50708859e3bbeebebb8e3a9b2fad7cd Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Wed, 7 Aug 2024 13:21:17 +0300 Subject: [PATCH 5/7] modified as_yolo() to pass subset_type argument --- supervision/dataset/core.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/supervision/dataset/core.py b/supervision/dataset/core.py index ef7348723..4ff6fab0c 100644 --- a/supervision/dataset/core.py +++ b/supervision/dataset/core.py @@ -555,7 +555,11 @@ def as_yolo( approximation_percentage=approximation_percentage, ) if data_yaml_path is not None: - save_data_yaml(data_yaml_path=data_yaml_path, classes=self.classes) + save_data_yaml( + data_yaml_path=data_yaml_path, + classes=self.classes, + subset_type=subset_type, + ) @classmethod def from_coco( From c4419cf8845b557e3e9b4bc4611583e3f6e3c79e Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Wed, 7 Aug 2024 13:23:01 +0300 Subject: [PATCH 6/7] fixed typo in descriptions --- supervision/utils/file.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/supervision/utils/file.py b/supervision/utils/file.py index 987bf64f3..4627cc614 100644 --- a/supervision/utils/file.py +++ b/supervision/utils/file.py @@ -112,7 +112,6 @@ def save_json_file(data: dict, file_path: Union[str, Path], indent: int = 3) -> Write a dict to a json file. Args: - indent: data (dict): dict with unique keys and value as pair. file_path (Union[str, Path]): The file path as a string or Path object. """ @@ -137,10 +136,9 @@ def read_yaml_file(file_path: Union[str, Path]) -> Dict: def save_yaml_file(data: dict, file_path: Union[str, Path]) -> None: """ - Save a dict to a json file. + Save a dict to a yaml file. Args: - indent: data (dict): dict with unique keys and value as pair. file_path (Union[str, Path]): The file path as a string or Path object. """ From a2be707c54f1f221ba3d3a010be09ebecb8dcdcf Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Wed, 7 Aug 2024 13:28:41 +0300 Subject: [PATCH 7/7] =?UTF-8?q?=F0=9F=8E=A8=20pre-commit=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervision/dataset/formats/yolo.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/supervision/dataset/formats/yolo.py b/supervision/dataset/formats/yolo.py index 60115c7ea..fdd497262 100644 --- a/supervision/dataset/formats/yolo.py +++ b/supervision/dataset/formats/yolo.py @@ -266,13 +266,15 @@ def save_yolo_annotations( save_text_file(lines=lines, file_path=yolo_annotations_path) -def save_data_yaml(data_yaml_path: str, classes: List[str], subset_type: Union[str, None]) -> None: +def save_data_yaml( + data_yaml_path: str, classes: List[str], subset_type: Union[str, None] +) -> None: if Path(data_yaml_path).exists(): data = read_yaml_file(data_yaml_path) else: data = {} data["nc"] = len(classes) - data["names"] = classes + data["names"] = classes if subset_type is not None: data[subset_type] = f"{subset_type}/images" Path(data_yaml_path).parent.mkdir(parents=True, exist_ok=True)