InstanceSegmentation

class InstanceSegmentationPrediction(box, mask, contours, hierarchy, confidence, label, index)

A single prediction from InstanceSegmentation.

Parameters
  • box (BoundingBox) – The bounding box around the detected object.

  • mask (ndarray) – The mask of the detected instance of the object.

  • contours (list) – The contours of the mask.

  • hierarchy (list) – Hierarchy of contours

  • confidence (float) – The confidence of this prediction.

  • label (str) – The label describing this prediction result.

  • index (int) – The index of this result in the master label list.

property label

The label describing this prediction result.

Return type

str

property index

The index of this result in the master label list.

Return type

int

property mask

The mask of this detected instance of the object.

Return type

ndarray

property contours

The contours generated for the mask of the detected instance of the object

Return type

list

property hierarchy

The hierarchy of contours generated for the mask of the detected instance of the object

Return type

list

property box

The bounding box around the object.

Return type

BoundingBox

property confidence

The confidence of this prediction.

Return type

float

class InstanceSegmentationResults(predictions, duration, image, **kwargs)

All the results of instance segmentation from :class: InstanceSegmentation.

Predictions are stored in sorted order, with descending order of confidence.

Parameters
  • predictions (List[InstanceSegmentationPrediction]) – The boxes and masks for each prediction.

  • duration (float) – The duration of the inference.

  • image (ndarray) – The image that the inference was performed on.

property duration

The duration of the inference in seconds.

Return type

float

property predictions

The list of predictions.

Return type

List[InstanceSegmentationPrediction]

property image

The image the results were processed on.

Return type

ndarray

class InstanceSegmentation(model_id, model_config=None)

Detect, segment and classify individual objects in an image.

Typical usage:

instance_segmentation = edgeiq.InstanceSegmentation(
        'alwaysai/mask_rcnn')
instance_segmentation.load(engine=edgeiq.Engine.DNN)

<get image>
results = instance_segmentation.segment_image(image, confidence_level=.5)
image = instance_segmentation.markup_image(
        image, results.predictions)

for prediction in results.predictions:
        text.append("{}: {:2.2f}%".format(
            prediction.label, prediction.confidence * 100))
Parameters

model_id (str) – The ID of the model you want to use for instance segmentation.

segment_image(image, confidence_level=0.3)

Detect, segment and classify individual objects in an image.

Parameters
  • image (ndarray) – The image to analyze.

  • confidence_level (float) – The minimum confidence level required to successfully accept a detection.

Return type

InstanceSegmentationResults

property accelerator

The accelerator being used.

Return type

Optional[Accelerator]

property colors

The auto-generated colors for the loaded model.

Note: Initialized to None when the model doesn’t have any labels. Note: To update, the new colors list must be same length as the label list.

Return type

Optional[ndarray]

property engine

The engine being used.

Return type

Optional[Engine]

property labels

The labels for the loaded model.

Note: Initialized to None when the model doesn’t have any labels.

Return type

Optional[List[str]]

load(engine=<Engine.DNN: 'DNN'>, accelerator=<Accelerator.DEFAULT: 'DEFAULT'>)

Load the model to an engine and accelerator.

Parameters
  • engine (Engine) – The engine to load the model to

  • accelerator (Accelerator) – The accelerator to load the model to

markup_image(image, predictions, show_labels=True, show_confidences=True, show_masks=True, colors=None, line_thickness=2, font_size=0.5, font_thickness=2)

Draw boxes, masks, labels, and confidences on the specified image.

Parameters
  • image (ndarray) – The image to draw on.

  • predictions (List[InstanceSegmentationPrediction]) – The list of prediction results.

  • show_labels (bool) – Indicates whether to show the label of the prediction.

  • show_confidences (bool) – Indicates whether to show the confidences of the prediction.

  • show_masks (bool) – Indicates whether to show the masks of the prediction.

  • colors (Optional[List[Tuple[int, int, int]]]) – A custom color list to use for the bounding boxes. The index of the color will be matched with a label index.

  • line_thickness (int) – The thickness of the lines that make up the bounding box.

  • font_size (float) – The scale factor for the text.

  • font_thickness (int) – The thickness of the lines used to draw the text.

Return type

ndarray

property model_config

The configuration of the model that was loaded

Return type

ModelConfig

property model_id

The ID of the loaded model.

Return type

str

property model_purpose

The purpose of the model being used.

Return type

str

publish_analytics(results, tag=None)

Publish Instance Segmentation results to the alwaysAI Analytics Service

Parameters
  • results (InstanceSegmentationResults) – The results to publish.

  • tag (Optional[Any]) – Additional information to assist in querying and visualizations.

Raises

ConnectionBlockedError when using connection to the alwaysAI Device Agent and resources are at capacity,

Raises

PacketRateError when publish rate exceeds current limit,

Raises

PacketSizeError when packet size exceeds current limit. Packet publish size and rate limits will be provided in the error message.