Skip to main content
Version: 0.7.x [Latest Alpha]

Basic Instance Segmentation

This example loads a complete instance segmentation pipeline file (.denkflow) exported from the Hub. An instance segmentation network returns bounding box tensors and segmentation tensors, which need to be combined in the to_objects-function.

import denkflow

pat = "YOUR-PAT"

denkflow_path = "path/to/model/file.denkflow"
image_path = "path/to/an/image.jpg"

input_topic = "camera/image"
bounding_box_output_topic = "bounding_box_filter_node/filtered_bounding_boxes"
segmentation_output_topic = "instance_segmentation_node/output_segmentations"

confidence_threshold = 0.5
segmentation_threshold = 0.5

pipeline = denkflow.Pipeline.from_denkflow(
denkflow_path,
pat=pat
)

pipeline.initialize()

# Instance segmentation returns both a bounding box tensor and an instance segmentation mask tensor
bounding_box_results_receiver = pipeline.subscribe(bounding_box_output_topic)
segmentation_results_receiver = pipeline.subscribe(segmentation_output_topic)

image_tensor = denkflow.ImageTensor.from_file(image_path)

pipeline.publish_image_tensor(input_topic, image_tensor)

pipeline.run()

bounding_box_tensor = bounding_box_results_receiver.receive_bounding_box_tensor()
segmentation_tensor = segmentation_results_receiver.receive_instance_segmentation_mask_tensor()

results_per_image = segmentation_tensor.to_objects(bounding_box_tensor, confidence_threshold, segmentation_threshold)

print("Instance Segmentation Results:")
for results_per_class_label in results_per_image:
for result_for_class_label in results_per_class_label:
print(f"{result_for_class_label.class_label.name}:")
for object in result_for_class_label.objects:
print(f" {object.confidence}")