module pixeltable.functions.vision
Pixeltable UDFs for Computer Vision. Example:udf bboxes_clip_to_canvas()
Signature
bboxes(pxt.Json[(Json): List of bounding boxes, each either specified with absolute pixel coordinates (int) or relative coordinates (float).format(Any): Format of the bounding box coordinates, one of ‘xyxy’, ‘xywh’, ‘cxcywh’.width(Any): Canvas width in absolute pixels. Required for absolute coordinates, must not be specified for relative.height(Any): Canvas height in absolute pixels. Required for absolute coordinates, must not be specified for relative.min_visibility(Any): Minimum fraction of the bounding box that must be visible after clipping. If the visibility is less than this value, returns None.min_area(Any): Minimum area of the bounding box after clipping. If the area is less than this value, returns None.
pxt.Json[(Json, ...)]: List of clipped bounding boxes in the same format as the input. Boxes that don’t meet the min_visibility or min_area thresholds are replaced with None.
udf bboxes_convert()
Signature
bboxes(pxt.Json[(Json): List of bounding boxes, each either specified with absolute pixel coordinates or relative coordinates in [0, 1].src_format(Any): Source format, one of ‘xyxy’, ‘xywh’, ‘cxcywh’.dst_format(Any): Destination format, one of ‘xyxy’, ‘xywh’, ‘cxcywh’.
pxt.Json[(Json, ...)]: List of bounding boxes in dst_format.
udf bboxes_crop_canvas()
Signature
bboxes(pxt.Json[(Json): List of bounding boxes, each either specified with absolute pixel coordinates or relative coordinates.format(Any): Format of the bounding box coordinates, one of ‘xyxy’, ‘xywh’, ‘cxcywh’.canvas_width(Any): Canvas width.canvas_height(Any): Canvas height.canvas_region(Any): Canvas region that was cropped, either specified with absolute pixel coordinates or relative coordinates, in the format specified bycanvas_region_format.canvas_region_format(Any): Format of thecanvas_regioncoordinates, one of ‘xyxy’, ‘xywh’, ‘cxcywh’.
pxt.Json[(Json, ...)]: List of adjusted bounding boxes in the same format as the input. They can extend beyond the canvas boundaries.
udf bboxes_draw()
Signature
str() and is hashable (e.g., strings, ints, etc.).
Colors can be specified as common HTML color names (e.g., ‘red’) supported by PIL’s
ImageColor module or as
RGB/RGBA hex codes (e.g., ‘#FF0000’, ‘#FF0000FF’). If opacity isn’t specified in the color string and
alpha/fill_alpha is None, defaults to 1.0 for box borders and 0.5 for filled boxes.
If no colors are specified, this function randomly assigns each label a specific color based on a hash of the label.
Parameters:
img(pxt.Image): The image on which to draw the bounding boxes.boxes(pxt.Json[(Json[(Int): List of bounding boxes, each represented as [xmin, ymin, xmax, ymax].labels(Any): List of labels for each bounding box.color(Any): Single color to be used for all bounding boxes and labels.box_colors(Any): List of colors, one per bounding box.alpha(Any): Opacity (0-1) of the bounding box borders and labels. If non-None, overrides any alpha incolor/box_colors.fill(Any): Whether to fill the bounding boxes with color.fill_alpha(Any): Opacity (0-1) of the bounding box fill. If non-None, overrides any alpha incolor/box_colors.width(Any): Width of the bounding box borders.font(Any): Name of a system font or path to a TrueType font file, as required byPIL.ImageFont.truetype(). IfNone, uses the default provided byPIL.ImageFont.load_default().font_size(Any): Size of the font used for labels in points. Only used in conjunction with non-Nonefontargument.
pxt.Image: The image with bounding boxes drawn on it.
udf bboxes_pad()
Signature
bboxes(pxt.Json[(Json): List of bounding boxes in absolute pixel coordinates.format(Any): Format of the bounding box coordinates, one of ‘xyxy’, ‘xywh’, ‘cxcywh’.top(Any): Amount to pad at the top, in absolute pixels.bottom(Any): Amount to pad at the bottom, in absolute pixels.left(Any): Amount to pad at the left, in absolute pixels.right(Any): Amount to pad at the right, in absolute pixels.x(Any): Amount to pad at the left and right, in absolute pixels.y(Any): Amount to pad at the top and bottom, in absolute pixels.
pxt.Json[(Json, ...)]: List of padded bounding boxes in the same format as the input.
udf bboxes_resize()
Signatures
- to a specified width or height (the other dimension is scaled to maintain the aspect ratio)
- to a specified aspect ratio
width, height, or aspect can be specified.
Parameters:
bboxes(Json[(Json, ...)]): List of bounding boxes, each either specified with absolute pixel coordinates or relative coordinates in [0, 1].format(String): Format of the bounding box coordinates, one of ‘xyxy’, ‘xywh’, ‘cxcywh’.width(Int | None, default:Literal(None)): Target width. Pass anintfor absolute pixels or afloatfor relative coordinates.height(Int | None, default:Literal(None)): Target height. Pass anintfor absolute pixels or afloatfor relative coordinates.aspect(String | None, default:Literal(None)): Target aspect ratio. Pass astrlike ‘16:9’ or afloatlike 1.78.aspect(String | None, default:Literal(None)): Target aspect ratio as a string ‘W:H’ (e.g., ‘16:9’) or afloat. Resizes either the width or height to match the specified aspect ratio, maintaining the other dimension. Requiresaspect_mode.aspect_mode(String | None, default:Literal(None)): Either ‘crop’ or ‘pad’. Required whenaspectis specified. Ifcrop, reduces the oversized dimension to match the aspect ratio. Ifpad, extends the undersized dimension to match the aspect ratio.
pxt.Json[(Json, ...)]: List of resized bounding boxes in the same format as the input.
udf bboxes_resize_canvas()
Signature
- as absolute pixel dimensions (requires canvas_width, canvas_height, new_canvas_width, new_canvas_height)
- as relative dimensions (requires at least one of canvas_scale, canvas_scale_x, canvas_scale_y)
bboxes(pxt.Json[(Json): List of bounding boxes in absolute pixel coordinates.format(Any): Format of the bounding box coordinates, one of ‘xyxy’, ‘xywh’, ‘cxcywh’.canvas_width(Any): Original canvas width in absolute pixels.canvas_height(Any): Original canvas height in absolute pixels.new_canvas_width(Any): New canvas width in absolute pixels. Requires canvas_width/canvas_height to be specified.new_canvas_height(Any): New canvas height in absolute pixels. Requires canvas_width/canvas_height to be specified.canvas_scale(Any): Scale factor to apply to both canvas dimensions.canvas_scale_x(Any): Scale factor to apply to the canvas width.canvas_scale_y(Any): Scale factor to apply to the canvas height.
pxt.Json[(Json, ...)]: List of adjusted bounding boxes in the same format as the input.
udf bboxes_scale()
Signature
bboxes(pxt.Json[(Json): List of bounding boxes, each either specified with absolute pixel coordinates or relative coordinates in [0, 1].format(Any): Format of the bounding box coordinates, one of ‘xyxy’, ‘xywh’, ‘cxcywh’.factor(Any): Scale factor to apply to both box dimensions.x_factor(Any): Scale factor to apply to the box width.y_factor(Any): Scale factor to apply to the box height.
pxt.Json[(Json, ...)]: List of scaled bounding boxes in the same format as the input.
udf eval_detections()
Signature
pred_bboxes(pxt.Json[(Json[(Int): List of predicted bounding boxes, each represented as [xmin, ymin, xmax, ymax].pred_labels(Any): List of predicted labels.pred_scores(Any): List of predicted scores.gt_bboxes(Any): List of ground truth bounding boxes, each represented as [xmin, ymin, xmax, ymax].gt_labels(Any): List of ground truth labels.min_iou(Any): Minimum intersection-over-union (IoU) threshold for a predicted bounding box to be considered a true positive.
pxt.Json[(Json, ...)]: A list of dictionaries, one per label class, with the following structure:
udf overlay_segmentation()
Signature
ImageColor module or as
RGB hex codes (e.g., ‘#FF0000’).
If no colors are specified, this function randomly assigns each segment a specific color based on a hash of its id.
Parameters:
img(pxt.Image): Input image.segmentation(pxt.Array[(None): 2D array of the same shape asimgwhere each pixel value is a segment id.alpha(Any): Blend factor for the overlay (0.0 = only original image, 1.0 = only segmentation colors).background(Any): Segment id to treat as background (not overlaid with color, showing the original image through).segment_colors(Any): List of colors, one per segment id. If the list is shorter than the number of segments, the remaining segments will be assigned colors automatically.draw_contours(Any): If True, draw contours around each segment with full opacity.contour_thickness(Any): Thickness of the contour lines in pixels.
pxt.Image: The image with the colored segmentation overlay.