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

Basic Object Detection

This example loads a complete object detection pipeline exported from the Hub.

from denkflow import Pipeline, ImageTensor

# --- Configuration ---
pat = "YOUR-PAT-TOKEN" # Replace with your actual Personal Access Token
denkflow_path = "path/to/your_exported_model.denkflow"
# For other ways to configure licensing (e.g., OneTimeLicenseSource, specific license IDs),
# refer to the "Configuration Options" section.

# --- Pipeline Creation ---
# The simplest way to load a .denkflow pipeline using a PAT:
pipeline = Pipeline.from_denkflow(denkflow_path, pat=pat)

# --- Inspection ---
print("Pipeline Structure:")
print(pipeline) # Inspect the graph structure and topic names

# --- Initialization ---
pipeline.initialize()

# --- Subscribe to Outputs (Replace with actual topic names from print(pipeline)) ---
# Example topic name, adjust based on your model's export
detection_topic = "bounding_box_filter_node/filtered_bounding_boxes"
detection_receiver = pipeline.subscribe_bounding_box_tensor(detection_topic)

# --- Publish Input Image (Replace with actual topic name from print(pipeline)) ---
# Example topic name, adjust based on your model's export
input_image_topic = "camera/image"
image_path = "path/to/your/image.jpg" # Define image_path closer to its use
image_tensor = ImageTensor.from_file(image_path)

# Alternative: For numpy arrays, use from_numpy_unsafe for significant speedups
# import numpy as np
# image_array = np.array(..., dtype=np.float32) # Your numpy array (B, 3, H, W) format, normalized by /255.0
# image_tensor = ImageTensor.from_numpy_unsafe(image_array)
# WARNING: Ensure image_array lives as long as image_tensor to avoid undefined behavior

pipeline.publish_image_tensor(input_image_topic, image_tensor)

# --- Run Pipeline ---
pipeline.run()

# --- Receive and Process Results ---
detection_tensor = detection_receiver.receive()
# Convert tensor to a list of BoundingBox objects
objects = detection_tensor.to_objects(0.5)

print(f"\nDetected {len(objects)} objects:")
for obj in objects:
print(f"- Class: {obj.class_label.name}, Confidence: {obj.confidence:.2f}")
print(f" BBox: ({obj.x1}, {obj.y1}), ({obj.x2}, {obj.y2})")