From 7849033da34bddbd0b231ad530d7f2387cb23fc4 Mon Sep 17 00:00:00 2001 From: hd Date: Tue, 27 Jun 2023 16:46:28 +0200 Subject: [PATCH 1/4] Fixed yaml file reading code --- supervision/dataset/formats/yolo.py | 18 +++--------------- supervision/utils/file.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/supervision/dataset/formats/yolo.py b/supervision/dataset/formats/yolo.py index 28dc4c59..67357613 100644 --- a/supervision/dataset/formats/yolo.py +++ b/supervision/dataset/formats/yolo.py @@ -13,6 +13,7 @@ from supervision.utils.file import ( list_files_with_extensions, read_txt_file, save_text_file, + read_yaml_file ) @@ -67,21 +68,8 @@ def _with_mask(lines: List[str]) -> bool: def _extract_class_names(file_path: str) -> List[str]: - names = [] - - with open(file_path, "r") as file: - lines = file.readlines() - for line in lines: - if line.strip().startswith("names:"): - start_index = lines.index(line) + 1 - break - - for line in lines[start_index:]: - if line.strip().startswith("-"): - names.append(line.strip().replace("-", "").strip()) - else: - break - + data = read_yaml_file(file_path=file_path) + names = data.get("names", []) return names diff --git a/supervision/utils/file.py b/supervision/utils/file.py index 7af669ca..14e5e014 100644 --- a/supervision/utils/file.py +++ b/supervision/utils/file.py @@ -3,6 +3,7 @@ from pathlib import Path from typing import List, Optional, Union import numpy as np +import yaml class NumpyJsonEncoder(json.JSONEncoder): @@ -107,3 +108,18 @@ def save_json_file(data: dict, file_path: str, indent: int = 3) -> None: """ with open(file_path, "w") as fp: json.dump(data, fp, cls=NumpyJsonEncoder, indent=indent) + + +def read_yaml_file(file_path: str) -> dict: + """ + Read a json file and return a dict. + + Args: + file_path (str): The path to the json file. + + Returns: + dict: A dict of annotations information + """ + with open(file_path, "r") as file: + data = yaml.safe_load(file) + return data \ No newline at end of file From 55c82957553df01019219537986382d0baaf34b4 Mon Sep 17 00:00:00 2001 From: hd Date: Tue, 27 Jun 2023 17:26:07 +0200 Subject: [PATCH 2/4] Fixed data.yaml store and format issue --- supervision/dataset/formats/yolo.py | 6 +++--- supervision/utils/file.py | 24 ++++++++++++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/supervision/dataset/formats/yolo.py b/supervision/dataset/formats/yolo.py index 67357613..b185351c 100644 --- a/supervision/dataset/formats/yolo.py +++ b/supervision/dataset/formats/yolo.py @@ -12,8 +12,9 @@ from supervision.detection.utils import polygon_to_mask, polygon_to_xyxy from supervision.utils.file import ( list_files_with_extensions, read_txt_file, + read_yaml_file, save_text_file, - read_yaml_file + save_yaml_file, ) @@ -244,5 +245,4 @@ def save_yolo_annotations( def save_data_yaml(data_yaml_path: str, classes: List[str]) -> None: data = {"nc": len(classes), "names": classes} Path(data_yaml_path).parent.mkdir(parents=True, exist_ok=True) - with open(data_yaml_path, "w") as outfile: - yaml.dump(data, outfile, default_flow_style=False) + save_yaml_file(data=data, file_path=data_yaml_path) diff --git a/supervision/utils/file.py b/supervision/utils/file.py index 14e5e014..2992d979 100644 --- a/supervision/utils/file.py +++ b/supervision/utils/file.py @@ -103,6 +103,7 @@ def save_json_file(data: dict, file_path: str, indent: int = 3) -> None: Write a dict to a json file. Args: + indent: data (dict): dict with unique keys and value as pair. file_path (str): The path to the json file. """ @@ -112,14 +113,29 @@ def save_json_file(data: dict, file_path: str, indent: int = 3) -> None: def read_yaml_file(file_path: str) -> dict: """ - Read a json file and return a dict. + Read a yaml file and return a dict. Args: - file_path (str): The path to the json file. + file_path (str): The path to the yaml file. Returns: - dict: A dict of annotations information + dict: A dict of content information """ with open(file_path, "r") as file: data = yaml.safe_load(file) - return data \ No newline at end of file + return data + + +def save_yaml_file(data: dict, file_path: str) -> None: + """ + Read a yaml file and return a dict. + + Args: + file_path (str): The path to the yaml file. + + Returns: + dict: A dict of content information + """ + + with open(file_path, "w") as outfile: + yaml.dump(data, outfile, sort_keys=False, default_flow_style=None) From f2eccae88e0728b00f8992f7c88852aa746e1833 Mon Sep 17 00:00:00 2001 From: hd Date: Tue, 27 Jun 2023 17:45:43 +0200 Subject: [PATCH 3/4] Fixed docstring --- supervision/utils/file.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/supervision/utils/file.py b/supervision/utils/file.py index 2992d979..aaaaf6fa 100644 --- a/supervision/utils/file.py +++ b/supervision/utils/file.py @@ -128,13 +128,12 @@ def read_yaml_file(file_path: str) -> dict: def save_yaml_file(data: dict, file_path: str) -> None: """ - Read a yaml file and return a dict. + Save a dict to a json file. Args: - file_path (str): The path to the yaml file. - - Returns: - dict: A dict of content information + indent: + data (dict): dict with unique keys and value as pair. + file_path (str): The path to the json file. """ with open(file_path, "w") as outfile: From 9cbec9fc94c7b52201a12309d971ba12031b0ccb Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Wed, 28 Jun 2023 00:39:21 +0200 Subject: [PATCH 4/4] ready for final tests --- supervision/dataset/formats/yolo.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/supervision/dataset/formats/yolo.py b/supervision/dataset/formats/yolo.py index b185351c..16f9772f 100644 --- a/supervision/dataset/formats/yolo.py +++ b/supervision/dataset/formats/yolo.py @@ -4,7 +4,6 @@ from typing import Dict, List, Optional, Tuple, Union import cv2 import numpy as np -import yaml from supervision.dataset.utils import approximate_mask_with_polygons from supervision.detection.core import Detections @@ -70,8 +69,7 @@ def _with_mask(lines: List[str]) -> bool: def _extract_class_names(file_path: str) -> List[str]: data = read_yaml_file(file_path=file_path) - names = data.get("names", []) - return names + return data["names"] def _image_name_to_annotation_name(image_name: str) -> str: