View Source YOLO.FrameScalers (YOLO v0.2.0)

Provides functions for resizing images while preserving aspect ratio and handling padding to reach target dimensions.

Available scalers

The following scaler implementations are currently provided:

# For Evision Mat images:
YOLO.detect(model, mat, frame_scaler: YOLO.FrameScalers.EvisionScaler)

# For Image library images:
YOLO.detect(model, image, frame_scaler: YOLO.FrameScalers.ImageScaler)

# For pre-sized Nx tensors:
YOLO.detect(model, tensor, frame_scaler: YOLO.FrameScalers.NxIdentityScaler)

Summary

Functions

Scales and pads an image to fit target dimensions. It's used in the model preprocess/3 callback.

Adjusts bounding box coordinates and sizes from the model input dimensions back to the original image dimensions. bboxes is a list of rows, where rows are lists of 6 elements [cx, cy, w, h, prob, class_idx].

Functions

fit(image, target_shape, scaler_module)

@spec fit(term(), {height :: integer(), width :: integer()}, frame_scaler :: module()) ::
  {Nx.Tensor.t(), YOLO.FrameScalers.ScalingConfig.t()}

Scales and pads an image to fit target dimensions. It's used in the model preprocess/3 callback.

Arguments

  • image - Input image in implementation's native format
  • target_shape - Target dimensions as {width, height} tuple
  • resizer_module - Module implementing the FrameScaler behaviour

Returns

  • {image_tensor, scaling_config} where image_tensor is the processed Nx tensor and scaling_config is a struct containing scaling and padding information

The returned image_tensor has shape {height, width, channels}.

scale_bboxes_to_original(bboxes, scaling_config)

@spec scale_bboxes_to_original([[float()]], YOLO.FrameScalers.ScalingConfig.t()) :: [
  [float()]
]

Adjusts bounding box coordinates and sizes from the model input dimensions back to the original image dimensions. bboxes is a list of rows, where rows are lists of 6 elements [cx, cy, w, h, prob, class_idx].

Returns a list of scaled bboxes.

Example

For a YOLOv8n model processing an image resized to 640x640, use the following to scale the bounding boxes back to the original 1920x1080 image:

scale_bboxes_to_original(bboxes, scaling_config)