File Utilities¶
sahi.utils.file
¶
File I/O utilities for SAHI.
Classes¶
NumpyEncoder
¶
Bases: JSONEncoder
JSON encoder for numpy types.
Source code in sahi/utils/file.py
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
|
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 |
str
|
The file extension. |
Source code in sahi/utils/file.py
import_model_class(model_type, class_name)
¶
Import a predefined detection model class by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Framework type ("yolov5", "detectron2", "mmdet", etc). |
required |
|
str
|
Name of the detection model class (e.g., "MmdetDetectionModel"). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
class_ |
type
|
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 |
|---|---|---|---|
|
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 |
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 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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Directory path to walk, e.g. "data/coco/". |
required |
|
list[str]
|
A list of strings to check if the target file contains them, example: ["coco.png", ".jpg", "jpeg"]. |
['.json']
|
|
bool
|
If true, prints some results. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
relative_filepath_list |
list[str]
|
List of file paths relative to given directory. |
abs_filepath_list |
list[str]
|
List of absolute file paths. |
Source code in sahi/utils/file.py
load_json(load_path, encoding='utf-8')
¶
Load JSON formatted data from file.
Encoding type can be specified with 'encoding' argument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
str "dirname/coco.json" |
required |
|
str
|
str Encoding type, default is 'utf-8' |
'utf-8'
|
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str | Path
|
str "dirname/coco.pickle" |
required |
Example inputs
load_path: "dirname/coco.pickle"
Source code in sahi/utils/file.py
save_json(data, save_path, indent=None)
¶
Saves json formatted data (given as "data") as save_path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
object
|
dict Data to be saved as json |
required |
|
str | Path
|
str "dirname/coco.json" |
required |
|
int | None
|
int or None Indentation level for pretty-printing the JSON data. If None, the most compact representation will be used. If an integer is provided, it specifies the number of spaces to use for indentation. Example: indent=4 will format the JSON data with an indentation of 4 spaces per level. |
None
|
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
object
|
dict Data to be saved as pickle |
required |
|
str | Path
|
str "dirname/coco.pickle" |
required |
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/'