anomalib.data.utils.image

Image Utils.

Module Contents

Functions

get_image_filenames(→ List[pathlib.Path])

Get image filenames.

duplicate_filename(→ pathlib.Path)

Check and duplicate filename.

generate_output_image_filename(→ pathlib.Path)

Generate an output filename to save the inference image.

read_image(→ numpy.ndarray)

Read image from disk in RGB format.

pad_nextpow2(→ torch.Tensor)

Compute required padding from input size and return padded images.

anomalib.data.utils.image.get_image_filenames(path: Union[str, pathlib.Path]) List[pathlib.Path][source]

Get image filenames.

Parameters

path (Union[str, Path]) – Path to image or image-folder.

Returns

List of image filenames

Return type

List[Path]

anomalib.data.utils.image.duplicate_filename(path: Union[str, pathlib.Path]) pathlib.Path[source]

Check and duplicate filename.

This function checks the path and adds a suffix if it already exists on the file system.

Parameters

path (Union[str, Path]) – Input Path

Examples

>>> path = Path("datasets/MVTec/bottle/test/broken_large/000.png")
>>> path.exists()
True

If we pass this to duplicate_filename function we would get the following: >>> duplicate_filename(path) PosixPath(‘datasets/MVTec/bottle/test/broken_large/000_1.png’)

Returns

Duplicated output path.

Return type

Path

anomalib.data.utils.image.generate_output_image_filename(input_path: Union[str, pathlib.Path], output_path: Union[str, pathlib.Path]) pathlib.Path[source]

Generate an output filename to save the inference image.

This function generates an output filaname by checking the input and output filenames. Input path is the input to infer, and output path is the path to save the output predictions specified by the user.

The function expects input_path to always be a file, not a directory. output_path could be a filename or directory. If it is a filename, the function checks if the specified filename exists on the file system. If yes, the function calls duplicate_filename to duplicate the filename to avoid overwriting the existing file. If output_path is a directory, this function adds the parent and filenames of input_path to output_path.

Parameters
  • input_path (Union[str, Path]) – Path to the input image to infer.

  • output_path (Union[str, Path]) – Path to output to save the predictions. Could be a filename or a directory.

Examples

>>> input_path = Path("datasets/MVTec/bottle/test/broken_large/000.png")
>>> output_path = Path("datasets/MVTec/bottle/test/broken_large/000.png")
>>> generate_output_image_filename(input_path, output_path)
PosixPath('datasets/MVTec/bottle/test/broken_large/000_1.png')
>>> input_path = Path("datasets/MVTec/bottle/test/broken_large/000.png")
>>> output_path = Path("results/images")
>>> generate_output_image_filename(input_path, output_path)
PosixPath('results/images/broken_large/000.png')
Raises

ValueError – When the input_path is not a file.

Returns

The output filename to save the output predictions from the inferencer.

Return type

Path

anomalib.data.utils.image.read_image(path: Union[str, pathlib.Path]) numpy.ndarray[source]

Read image from disk in RGB format.

Parameters

path (str, Path) – path to the image file

Example

>>> image = read_image("test_image.jpg")
Returns

image as numpy array

anomalib.data.utils.image.pad_nextpow2(batch: torch.Tensor) torch.Tensor[source]

Compute required padding from input size and return padded images.

Finds the largest dimension and computes a square image of dimensions that are of the power of 2. In case the image dimension is odd, it returns the image with an extra padding on one side.

Parameters

batch (Tensor) – Input images

Returns

Padded batch

Return type

batch