Prediction

class Prediction(annotation_json, source=None, model_meta=None, name=None, path=None, url=None, project_id=None, dataset_id=None, image_id=None, video_id=None, frame_index=None, api=None, **kwargs)[source]

Bases: object

A single prediction result.

The prediction is primarily represented by annotation_json (Supervisely annotation format). If you provide model_meta (a ProjectMeta or its JSON), you can access the parsed Annotation via annotation.

Parameters:
annotation_json

Prediction in Supervisely annotation JSON format.

source : str or int, optional

Optional source descriptor (path, id, etc.), used as a hint for loading/visualization.

model_meta=None

Output meta of the model, required to construct an Annotation.

name : str, optional

Optional name for this prediction (used in visualization filenames).

path : str, optional

Local path to the source image/video.

url : str, optional

URL to the source image/video.

project_id : int, optional

Project id associated with the source.

dataset_id : int, optional

Dataset id associated with the source.

image_id : int, optional

Image id associated with the source.

video_id : int, optional

Video id associated with the source (for frame predictions).

frame_index : int, optional

Frame index for video predictions.

api=None

API client used to download image/frame by id.

Methods

from_json

Create Prediction from a JSON dict.

load_image

Load the source image (or video frame) into a NumPy array.

to_json

Serialize prediction to a JSON-compatible dict.

visualize

Render prediction on top of the source image/frame.

Attributes

annotation

Get the annotation of the prediction.

boxes

Get the bounding boxes of the prediction.

class_idxs

Get the class indexes of the prediction.

classes

Get the classes of the prediction.

masks

Get the masks of the prediction.

scores

Get the scores of the prediction.

track_ids

Get track IDs for each detection.

classmethod from_json(json_data, **kwargs)[source]

Create Prediction from a JSON dict.

This helper accepts both styles of keys coming from different backends: annotation_json or annotation.

Parameters:
json_data : dict

Source JSON.

kwargs : dict

Additional fields to override/extend JSON (e.g. api, model_meta).

Returns:

Prediction instance.

Return type:

Prediction

Raises:

ValueError – If annotation payload is missing.

load_image()[source]

Load the source image (or video frame) into a NumPy array.

The loader uses the first available source in the following order: pathurlimage_id (requires api or environment config) → video_id + frame_index → video path/url + frame_index.

Returns:

Image (or frame) as H×W×C NumPy array.

Return type:

numpy.ndarray

to_json()[source]

Serialize prediction to a JSON-compatible dict.

Returns:

JSON dict.

Return type:

dict

visualize(save_path=None, save_dir=None, color=None, thickness=None, opacity=0.5, draw_tags=False, fill_rectangles=True)[source]

Render prediction on top of the source image/frame.

If save_path (file or directory) or save_dir is provided, the visualization is saved. Otherwise the rendered image is returned.

Parameters:
save_path : str, optional

Output file path or directory.

save_dir : str, optional

Output directory (alias for directory save_path).

color : List[int], optional

Optional RGB color for drawing.

thickness : int, optional

Optional line thickness.

opacity : float, optional

Bitmap opacity.

draw_tags : bool, optional

If True, draws tags on objects.

fill_rectangles : bool, optional

If True, fills rectangles.

Returns:

Rendered image.

Return type:

numpy.ndarray

property annotation : supervisely.annotation.annotation.Annotation

Get the annotation of the prediction.

property boxes

Get the bounding boxes of the prediction.

property class_idxs : numpy.ndarray

Get the class indexes of the prediction.

property classes

Get the classes of the prediction.

property masks

Get the masks of the prediction.

property scores

Get the scores of the prediction.

property track_ids

Get track IDs for each detection. Returns None for detections without tracking.

Returns:

Track IDs for each detection.

Return type:

np.ndarray