From 8c0e2fb362219a9ade478800496f51c43c6cedaa Mon Sep 17 00:00:00 2001 From: hardikdava Date: Mon, 17 Jun 2024 20:44:48 +0200 Subject: [PATCH 1/6] exposing some file utilities --- supervision/__init__.py | 10 +++++++++- supervision/utils/file.py | 24 ++++++++++++------------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/supervision/__init__.py b/supervision/__init__.py index 4f28d49f..2a311c7a 100644 --- a/supervision/__init__.py +++ b/supervision/__init__.py @@ -90,7 +90,15 @@ from supervision.keypoint.core import KeyPoints from supervision.metrics.detection import ConfusionMatrix, MeanAveragePrecision from supervision.tracker.byte_tracker.core import ByteTrack from supervision.utils.conversion import cv2_to_pillow, pillow_to_cv2 -from supervision.utils.file import list_files_with_extensions +from supervision.utils.file import ( + list_files_with_extensions, + read_json_file, + read_txt_file, + read_yaml_file, + save_json_file, + save_text_file, + save_yaml_file, +) from supervision.utils.image import ( ImageSink, create_tiles, diff --git a/supervision/utils/file.py b/supervision/utils/file.py index dfce7eb0..5b7baea3 100644 --- a/supervision/utils/file.py +++ b/supervision/utils/file.py @@ -57,7 +57,7 @@ def list_files_with_extensions( return files_with_extensions -def read_txt_file(file_path: str, skip_empty: bool = False) -> List[str]: +def read_txt_file(file_path: Union[str, Path], skip_empty: bool = False) -> List[str]: """ Read a text file and return a list of strings without newline characters. Optionally skip empty lines. @@ -70,7 +70,7 @@ def read_txt_file(file_path: str, skip_empty: bool = False) -> List[str]: Returns: List[str]: A list of strings representing the lines in the text file. """ - with open(file_path, "r") as file: + with open(file_path.as_posix(), "r") as file: if skip_empty: lines = [line.rstrip("\n") for line in file if line.strip()] else: @@ -79,7 +79,7 @@ def read_txt_file(file_path: str, skip_empty: bool = False) -> List[str]: return lines -def save_text_file(lines: List[str], file_path: str): +def save_text_file(lines: List[str], file_path: Union[str, Path]): """ Write a list of strings to a text file, each string on a new line. @@ -87,12 +87,12 @@ def save_text_file(lines: List[str], file_path: str): lines (List[str]): The list of strings to be written to the file. file_path (str): The path to the text file. """ - with open(file_path, "w") as file: + with open(file_path.as_posix(), "w") as file: for line in lines: file.write(line + "\n") -def read_json_file(file_path: str) -> dict: +def read_json_file(file_path: Union[str, Path]) -> dict: """ Read a json file and return a dict. @@ -102,12 +102,12 @@ def read_json_file(file_path: str) -> dict: Returns: dict: A dict of annotations information """ - with open(file_path, "r") as file: + with open(file_path.as_posix(), "r") as file: data = json.load(file) return data -def save_json_file(data: dict, file_path: str, indent: int = 3) -> None: +def save_json_file(data: dict, file_path: Union[str, Path], indent: int = 3) -> None: """ Write a dict to a json file. @@ -116,11 +116,11 @@ def save_json_file(data: dict, file_path: str, indent: int = 3) -> None: 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 fp: + with open(file_path.as_posix(), "w") as fp: json.dump(data, fp, cls=NumpyJsonEncoder, indent=indent) -def read_yaml_file(file_path: str) -> dict: +def read_yaml_file(file_path: Union[str, Path]) -> dict: """ Read a yaml file and return a dict. @@ -130,12 +130,12 @@ def read_yaml_file(file_path: str) -> dict: Returns: dict: A dict of content information """ - with open(file_path, "r") as file: + with open(file_path.as_posix(), "r") as file: data = yaml.safe_load(file) return data -def save_yaml_file(data: dict, file_path: str) -> None: +def save_yaml_file(data: dict, file_path: Union[str, Path]) -> None: """ Save a dict to a json file. @@ -145,5 +145,5 @@ def save_yaml_file(data: dict, file_path: str) -> None: file_path (str): The path to the json file. """ - with open(file_path, "w") as outfile: + with open(file_path.as_posix(), "w") as outfile: yaml.dump(data, outfile, sort_keys=False, default_flow_style=None) From c14642accf46cfb8c96581c72f111013a763eb0c Mon Sep 17 00:00:00 2001 From: hardikdava Date: Mon, 17 Jun 2024 20:47:36 +0200 Subject: [PATCH 2/6] docstrings updated --- supervision/utils/file.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/supervision/utils/file.py b/supervision/utils/file.py index 5b7baea3..edb50511 100644 --- a/supervision/utils/file.py +++ b/supervision/utils/file.py @@ -63,7 +63,7 @@ def read_txt_file(file_path: Union[str, Path], skip_empty: bool = False) -> List Optionally skip empty lines. Args: - file_path (str): The path to the text file. + file_path (Union[str, Path]): The file path as a string or Path object. skip_empty (bool): If True, skip lines that are empty or contain only whitespace. Default is False. @@ -85,7 +85,7 @@ def save_text_file(lines: List[str], file_path: Union[str, Path]): Args: lines (List[str]): The list of strings to be written to the file. - file_path (str): The path to the text file. + file_path (Union[str, Path]): The file path as a string or Path object. """ with open(file_path.as_posix(), "w") as file: for line in lines: @@ -97,7 +97,7 @@ def read_json_file(file_path: Union[str, Path]) -> dict: Read a json file and return a dict. Args: - file_path (str): The path to the json file. + file_path (Union[str, Path]): The file path as a string or Path object. Returns: dict: A dict of annotations information @@ -114,7 +114,7 @@ def save_json_file(data: dict, file_path: Union[str, Path], indent: int = 3) -> Args: indent: data (dict): dict with unique keys and value as pair. - file_path (str): The path to the json file. + file_path (Union[str, Path]): The file path as a string or Path object. """ with open(file_path.as_posix(), "w") as fp: json.dump(data, fp, cls=NumpyJsonEncoder, indent=indent) @@ -125,7 +125,7 @@ def read_yaml_file(file_path: Union[str, Path]) -> dict: Read a yaml file and return a dict. Args: - file_path (str): The path to the yaml file. + file_path (Union[str, Path]): The file path as a string or Path object. Returns: dict: A dict of content information @@ -142,7 +142,7 @@ def save_yaml_file(data: dict, file_path: Union[str, Path]) -> None: Args: indent: data (dict): dict with unique keys and value as pair. - file_path (str): The path to the json file. + file_path (Union[str, Path]): The file path as a string or Path object. """ with open(file_path.as_posix(), "w") as outfile: From 0c95a6126dcabd17674e3cb22e3c80f1a982eba8 Mon Sep 17 00:00:00 2001 From: hardikdava Date: Mon, 17 Jun 2024 20:50:40 +0200 Subject: [PATCH 3/6] converting paths to string --- supervision/utils/file.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/supervision/utils/file.py b/supervision/utils/file.py index edb50511..b64a1135 100644 --- a/supervision/utils/file.py +++ b/supervision/utils/file.py @@ -70,7 +70,7 @@ def read_txt_file(file_path: Union[str, Path], skip_empty: bool = False) -> List Returns: List[str]: A list of strings representing the lines in the text file. """ - with open(file_path.as_posix(), "r") as file: + with open(str(file_path), "r") as file: if skip_empty: lines = [line.rstrip("\n") for line in file if line.strip()] else: @@ -87,7 +87,7 @@ def save_text_file(lines: List[str], file_path: Union[str, Path]): lines (List[str]): The list of strings to be written to the file. file_path (Union[str, Path]): The file path as a string or Path object. """ - with open(file_path.as_posix(), "w") as file: + with open(str(file_path), "w") as file: for line in lines: file.write(line + "\n") @@ -102,7 +102,7 @@ def read_json_file(file_path: Union[str, Path]) -> dict: Returns: dict: A dict of annotations information """ - with open(file_path.as_posix(), "r") as file: + with open(str(file_path), "r") as file: data = json.load(file) return data @@ -116,7 +116,7 @@ def save_json_file(data: dict, file_path: Union[str, Path], indent: int = 3) -> data (dict): dict with unique keys and value as pair. file_path (Union[str, Path]): The file path as a string or Path object. """ - with open(file_path.as_posix(), "w") as fp: + with open(str(file_path), "w") as fp: json.dump(data, fp, cls=NumpyJsonEncoder, indent=indent) @@ -130,7 +130,7 @@ def read_yaml_file(file_path: Union[str, Path]) -> dict: Returns: dict: A dict of content information """ - with open(file_path.as_posix(), "r") as file: + with open(str(file_path), "r") as file: data = yaml.safe_load(file) return data @@ -145,5 +145,5 @@ def save_yaml_file(data: dict, file_path: Union[str, Path]) -> None: file_path (Union[str, Path]): The file path as a string or Path object. """ - with open(file_path.as_posix(), "w") as outfile: + with open(str(file_path), "w") as outfile: yaml.dump(data, outfile, sort_keys=False, default_flow_style=None) From a56a16937fc89a18fde89c13a3141f21cb3b6ad0 Mon Sep 17 00:00:00 2001 From: hardikdava Date: Mon, 17 Jun 2024 20:54:08 +0200 Subject: [PATCH 4/6] missing rettype added --- supervision/utils/file.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/supervision/utils/file.py b/supervision/utils/file.py index b64a1135..2def5629 100644 --- a/supervision/utils/file.py +++ b/supervision/utils/file.py @@ -1,6 +1,6 @@ import json from pathlib import Path -from typing import List, Optional, Union +from typing import List, Optional, Union, Dict import numpy as np import yaml @@ -79,7 +79,7 @@ def read_txt_file(file_path: Union[str, Path], skip_empty: bool = False) -> List return lines -def save_text_file(lines: List[str], file_path: Union[str, Path]): +def save_text_file(lines: List[str], file_path: Union[str, Path]) -> None: """ Write a list of strings to a text file, each string on a new line. @@ -92,7 +92,7 @@ def save_text_file(lines: List[str], file_path: Union[str, Path]): file.write(line + "\n") -def read_json_file(file_path: Union[str, Path]) -> dict: +def read_json_file(file_path: Union[str, Path]) -> Dict: """ Read a json file and return a dict. @@ -120,7 +120,7 @@ def save_json_file(data: dict, file_path: Union[str, Path], indent: int = 3) -> json.dump(data, fp, cls=NumpyJsonEncoder, indent=indent) -def read_yaml_file(file_path: Union[str, Path]) -> dict: +def read_yaml_file(file_path: Union[str, Path]) -> Dict: """ Read a yaml file and return a dict. From fa7821b1c4c3cf660c2556cb0e7245172b768ca6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 17 Jun 2024 18:54:23 +0000 Subject: [PATCH 5/6] =?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/utils/file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/supervision/utils/file.py b/supervision/utils/file.py index 2def5629..987bf64f 100644 --- a/supervision/utils/file.py +++ b/supervision/utils/file.py @@ -1,6 +1,6 @@ import json from pathlib import Path -from typing import List, Optional, Union, Dict +from typing import Dict, List, Optional, Union import numpy as np import yaml From 14ce0799ceaa69d7ee23b9ef9842a86758aaa4c3 Mon Sep 17 00:00:00 2001 From: hardikdava Date: Wed, 19 Jun 2024 09:46:38 +0200 Subject: [PATCH 6/6] revert global import --- supervision/__init__.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/supervision/__init__.py b/supervision/__init__.py index 2a311c7a..4f28d49f 100644 --- a/supervision/__init__.py +++ b/supervision/__init__.py @@ -90,15 +90,7 @@ from supervision.keypoint.core import KeyPoints from supervision.metrics.detection import ConfusionMatrix, MeanAveragePrecision from supervision.tracker.byte_tracker.core import ByteTrack from supervision.utils.conversion import cv2_to_pillow, pillow_to_cv2 -from supervision.utils.file import ( - list_files_with_extensions, - read_json_file, - read_txt_file, - read_yaml_file, - save_json_file, - save_text_file, - save_yaml_file, -) +from supervision.utils.file import list_files_with_extensions from supervision.utils.image import ( ImageSink, create_tiles,