diff --git a/supervision/utils/file.py b/supervision/utils/file.py index dfce7eb0..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 +from typing import Dict, List, Optional, Union import numpy as np import yaml @@ -57,20 +57,20 @@ 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. 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. Returns: List[str]: A list of strings representing the lines in the text file. """ - with open(file_path, "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: @@ -79,71 +79,71 @@ 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]) -> None: """ Write a list of strings to a text file, each string on a new line. 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, "w") as file: + with open(str(file_path), "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. 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 """ - with open(file_path, "r") as file: + with open(str(file_path), "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. 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, "w") as fp: + with open(str(file_path), "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. 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 """ - with open(file_path, "r") as file: + with open(str(file_path), "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. 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, "w") as outfile: + with open(str(file_path), "w") as outfile: yaml.dump(data, outfile, sort_keys=False, default_flow_style=None)