infer.py

Generate optical flow with one of the available models.

This script can display and save optical flow estimated by any of the available models. It accepts multiple types of inputs, including: individual images, a folder of images, a video, or a webcam stream.

infer.infer(args: Namespace, model: BaseModel) None[source]

Perform the inference.

Parameters:
modelBaseModel

The model to be used for inference.

argsNamespace

Arguments to configure the model and the inference.

See also

ptlflow.models.base_model.base_model.BaseModel

The parent class of the available models.

infer.init_input(input_path: str | List[str]) Tuple[VideoCapture, List[Path], int, ndarray][source]

Initialize the required variable to start loading the inputs.

This function will detect which type of input_path was given (list of images, folder of images, video, or webcam). Then it will establish its length and also get the first frame of the input.

Parameters:
input_pathstr

The path to the input(s).

Returns:
tuple[cv.VideoCapture, List[Path], int, np.ndarray]

The initialized variables - a cv.VideoCapture if the input is a video OR - a list of paths to the images otherwise, - the maximum number of images, and - the first image.

infer.show_outputs(img1: ndarray, img2: ndarray, preds_npy: Dict[str, ndarray], auto_forward: bool, max_show_side: int) int[source]

Show the images on the screen.

Parameters:
img1np.ndarray

First image for estimating the optical flow.

img2np.ndarray

Second image for estimating the optical flow.

preds_npydict[str, np.ndarray]

The model predictions converted to numpy format.

auto_forwardbool

If false, the user needs to press a key to move to the next image.

max_show_sideint

If max(height, width) of the image is larger than this value, then it is downscaled before showing.

Returns:
int

A value representing which key the user pressed.

See also

ptlflow.utils.utils.tensor_dict_to_numpy

This function can generate preds_npy.

infer.write_outputs(preds_npy: Dict[str, ndarray], output_dir: str, img_name: str, flow_format: str, img_dir_name: str | None = None) None[source]

Show the images on the screen.

Parameters:
preds_npydict[str, np.ndarray]

The model predictions converted to numpy format.

output_dirstr

The path to the root dir where the outputs will be saved.

img_namestr

The name to be used to save each image (without extension).

flow_formatstr

The format (extension) of the flow file to be saved. It can one of {flo, png}.

See also

ptlflow.utils.utils.tensor_dict_to_numpy

This function can generate preds_npy.