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.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(detections, iou_threshold=0.45, max_num=200)#

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:
  • detections (np.ndarray) – labels, scores and boxes

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

  • max_num (int, optional) – Max number of objects filter. Defaults to 200.

Returns:

(dets, indices), Dets are boxes with scores. Indices are indices of kept boxes.

Return type:

tuple

model_api.models.utils.nms(x1, y1, x2, y2, scores, thresh, include_boundaries=False, keep_top_k=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