Utils#

class model_api.models.utils.OutputTransform(input_size, output_resolution)#

Bases: object

compute_resolution(input_size)#
resize(image)#
scale(inputs)#
class model_api.models.utils.ResizeMetadata(inverted_scale_x, inverted_scale_y, pad_left=0, pad_top=0)#

Bases: object

Image resize transformation metadata.

Contains parameters needed to transform coordinates (e.g., bounding boxes, keypoints) from model input space back to the original image space. It handles different resize strategies including standard resize, fit-to-window, and letterbox modes.

inverted_scale_x#

Scale factor to multiply x-coordinates to map from model to original space.

inverted_scale_y#

Scale factor to multiply y-coordinates to map from model to original space.

pad_left#

Left padding added during letterbox resize (0 for other resize types).

pad_top#

Top padding added during letterbox resize (0 for other resize types).

classmethod compute(original_width, original_height, model_width, model_height, resize_type)#

Compute resize metadata for coordinate transformation.

Parameters:
  • original_width (int) – Width of the original input image.

  • original_height (int) – Height of the original input image.

  • model_width (int) – Width of the model input (after resize).

  • model_height (int) – Height of the model input (after resize).

  • resize_type (str) – Type of resize applied (“standard”, “fit_to_window”, “fit_to_window_letterbox”).

Return type:

ResizeMetadata

Returns:

ResizeMetadata instance with computed scale factors and padding.

classmethod from_dict(data)#

Create from dictionary (e.g., from metadata).

Parameters:

data (dict[str, float | int]) – Dictionary with resize info keys.

Return type:

ResizeMetadata

Returns:

ResizeMetadata instance.

to_dict()#

Convert to dictionary for storage in metadata.

Return type:

dict[str, float | int]

Returns:

Dictionary with keys matching the legacy resize_info format.

inverted_scale_x: float#
inverted_scale_y: float#
pad_left: int = 0#
pad_top: int = 0#
class model_api.models.utils.topk_namedtuple(values, indices)#

Bases: tuple

Create new instance of topk_namedtuple(values, indices)

indices#

Alias for field number 1

values#

Alias for field number 0

model_api.models.utils.add_rotated_rects(inst_seg_result)#
Return type:

RotatedSegmentationResult

model_api.models.utils.calculate_nms(boxes, scores, labels, iou_threshold=0.45, max_predictions=200, include_boundaries=False, agnostic_nms=False, execute_nms=False)#
Return type:

list[int]

model_api.models.utils.clip_detections(detections, size)#

Clip bounding boxes to image size.

Parameters:
  • detections (DetectionResult) – detection results including boxes, labels and scores.

  • size (tuple[int, int]) – image size of format (height, width).

model_api.models.utils.get_contours(seg_result)#
Return type:

list[Contour]

model_api.models.utils.is_softmaxed(array, axis, atol=1e-05)#

Check if the input array is softmaxed along the specified axis.

Return type:

bool

model_api.models.utils.load_labels(label_file)#
model_api.models.utils.multiclass_nms(boxes, scores, labels, iou_threshold=0.45, max_predictions=200, include_boundaries=False)#

Multi-class NMS.

strategy: in order to perform NMS independently per class, we add an offset to all the boxes. The offset is dependent only on the class idx, and is large enough so that boxes from different classes do not overlap

Parameters:
  • boxes (ndarray) – boxes in form x1, y1, x2, y2

  • scores (ndarray) – detection scores

  • labels (ndarray) – label indices for each box

  • iou_threshold (float) – IoU threshold. Defaults to 0.45.

  • max_predictions (int) – Max number of objects filter. Zero means no limit. Defaults to 200.

  • include_boundaries (bool) – Whether to include box boundaries in IoU calculation. Defaults to False.

Return type:

list[int]

Returns:

indices of kept boxes.

model_api.models.utils.nms(boxes, scores, iou_threshold, include_boundaries=False, max_predictions=0)#
Return type:

list[int]

model_api.models.utils.softmax(logits, eps=1e-09, axis=None, keepdims=False)#
Return type:

ndarray

model_api.models.utils.top_k(array, k, axis)#

Returns the top k values and their indices along the specified axis.

Return type:

topk_namedtuple