Utilities#

FilePath: TypeAliasType = str | bytes | os.PathLike#

Type alias for a file path.

class glasses_detector.utils.eval_infer_mode(model: Module)[source]#

Bases: object

Context manager and decorator for evaluation and inference.

This class can be used as a context manager or a decorator to set a PyTorch Module to evaluation mode via eval() and enable inference_mode for the duration of a function or a with statement. After the function or the with statement, the model’s mode, i.e., training property, and inference_mode are restored to their original states.

Example

model = ...  # Your PyTorch model

@eval_infer_mode(model)
def your_function():
    # E.g., forward pass
    pass

# or

with eval_infer_mode(model):
    # E.g., forward pass
    pass
Parameters:

model (torch.nn.Module) – The PyTorch model to be set to evaluation mode.

glasses_detector.utils.is_url(x: str) bool[source]#

Check if a string is a valid URL.

Takes any string and checks if it is a valid URL.

Parameters:

x (str) – The string to check.

Returns:

True if the string is a valid URL, False otherwise.

Return type:

bool

glasses_detector.utils.flatten(items: T) T[source]#
glasses_detector.utils.flatten(items: Iterable[T | Iterable]) list[T]

Flatten a nested list.

This function takes any nested iterable and returns a flat list.

Parameters:

items (T | Iterable[T | Iterable]) – The nested iterable to flatten.

Returns:

The flattened list or the original items value if it is not an iterable or is of type str.

Return type:

T | list[T]

glasses_detector.utils.is_path_type(path: Any) TypeGuard[FilePath][source]#

Check if an object is a valid path type.

This function takes any object and checks if it is a valid path type. A valid path type is either a str, bytes or os.PathLike object.

Parameters:

path (Any) – The object to check.

Returns:

True if the object is a valid path type, False otherwise.

Return type:

TypeGuard[FilePath]

glasses_detector.utils.is_image_file(path: FilePath) bool[source]#

Check if a file is an image.

This function takes a file path and checks if it is an image file. This is done by checking if the file exists and if it has a valid image extension.

Parameters:

path (FilePath) – The path to the file.

Returns:

True if the file is an image, False otherwise.

Return type:

bool