File Utilities¶
sahi.utils.file
¶
Functions¶
download_from_url(from_url, to_path)
¶
Downloads a file from the given URL and saves it to the specified path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The URL of the file to download. |
required |
|
str
|
The path where the downloaded file should be saved. |
required |
Returns:
Type | Description |
---|---|
None |
Source code in sahi/utils/file.py
get_base_filename(path)
¶
Takes a file path, returns (base_filename_with_extension, base_filename_without_extension)
Source code in sahi/utils/file.py
get_file_extension(path)
¶
Get the file extension from a given file path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The file path. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
The file extension. |
Source code in sahi/utils/file.py
import_model_class(model_type, class_name)
¶
Imports a predefined detection class by class name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str "yolov5", "detectron2", "mmdet", "huggingface" etc |
required | |
|
str Name of the detection model class (example: "MmdetDetectionModel") |
required |
Returns: class_: class with given path
Source code in sahi/utils/file.py
increment_path(path, exist_ok=True, sep='')
¶
Increment path, i.e. runs/exp --> runs/exp{sep}0, runs/exp{sep}1 etc.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Union[str, Path]
|
str The base path to increment. |
required |
|
bool
|
bool If True, return the path as is if it already exists. If False, increment the path. |
True
|
|
str
|
str The separator to use between the base path and the increment number. |
''
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The incremented path. |
Example
increment_path("runs/exp", sep="") 'runs/exp_0' increment_path("runs/exp_0", sep="") 'runs/exp_1'
Source code in sahi/utils/file.py
is_colab()
¶
Check if the current environment is a Google Colab instance.
Returns:
Name | Type | Description |
---|---|---|
bool |
True if the environment is a Google Colab instance, False otherwise. |
list_files(directory, contains=['.json'], verbose=1)
¶
Walk given directory and return a list of file path with desired extension
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
str "data/coco/" |
required |
|
list
|
list A list of strings to check if the target file contains them, example: ["coco.png", ".jpg", "jpeg"] |
['.json']
|
|
int
|
int 0: no print 1: print number of files |
1
|
Returns:
Name | Type | Description |
---|---|---|
filepath_list |
List[str]
|
list List of file paths |
Source code in sahi/utils/file.py
list_files_recursively(directory, contains=['.json'], verbose=True)
¶
Walk given directory recursively and return a list of file path with desired extension
Arguments¶
directory : str
"data/coco/"
contains : list
A list of strings to check if the target file contains them, example: ["coco.png", ".jpg", "jpeg"]
verbose : bool
If true, prints some results
Returns¶
relative_filepath_list : list
List of file paths relative to given directory
abs_filepath_list : list
List of absolute file paths
Source code in sahi/utils/file.py
load_json(load_path, encoding='utf-8')
¶
Loads json formatted data (given as "data") from load_path Encoding type can be specified with 'encoding' argument
Example inputs
load_path: "dirname/coco.json"
Source code in sahi/utils/file.py
load_pickle(load_path)
¶
Loads pickle formatted data (given as "data") from load_path Example inputs: load_path: "dirname/coco.pickle"
save_json(data, save_path, indent=None)
¶
Saves json formatted data (given as "data") as save_path Example inputs: data: {"image_id": 5} save_path: "dirname/coco.json" indent: Train json files with indent=None, val json files with indent=4
Source code in sahi/utils/file.py
save_pickle(data, save_path)
¶
Saves pickle formatted data (given as "data") as save_path Example inputs: data: {"image_id": 5} save_path: "dirname/coco.pickle"
Source code in sahi/utils/file.py
unzip(file_path, dest_dir)
¶
Unzips compressed .zip file. Example inputs: file_path: 'data/01_alb_id.zip' dest_dir: 'data/'