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

Basic Classification

This example loads a complete classification pipeline file (.denkflow) exported from the Hub.

import denkflow

# A personal access token (PAT) is used for authentication.
pat = "YOUR-PAT"

# These are the files that will be used in this example.
denkflow_path = "path/to/model/file.denkflow"
image_path = "path/to/an/image.jpg"

# To make data available to the pipeline, it needs to be put into the
# input topic via publishing. After running the pipeline, the results
# can be received from the output topic. While the name of the inut
# topic is typically the same for every pipeline, the pipelines for
# different model types might deviate in the names of their output
# topics.
input_topic = "camera/image"
output_topic = "classification_node/output"

# Load the .denkflow file and create the pipeline from it.
pipeline = denkflow.Pipeline.from_denkflow(
denkflow_path,
pat=pat,
)

# This function initializes the pipeline. After this step, the pipeline
# can no longer be modified. Subscribing and publishing only work after
# initialization.
pipeline.initialize()

# The subscribe-function produces a receiver that can be used to gather
# output data from the pipeline.
receiver = pipeline.subscribe(output_topic)

# The image file needs to be converted to an ImageTensor before it can be
# fed into the pipeline.
image_tensor = denkflow.ImageTensor.from_file(image_path)

# The publishing step puts the data into the input topic, where it can be
# accessed by the pipeline.
pipeline.publish_image_tensor(input_topic, image_tensor)

# Upon calling the run-function, the pipeline takes the published image
# and processes it. The results are written into the output topic, where
# they can be received using the previously created receiver.
pipeline.run()

# This step will take the data from the output topic. The output data type
# for classification models is the scalar tensor.
tensor = receiver.receive_scalar_tensor()

# Every output tensor will have a to_objects-function which will produce
# objects that can be easily printed or used for further data processing.
results_per_batch = tensor.to_objects()

# For this example, the results of the classification are simply printed
# to the console.
print("Classification Results:")
for results_per_class_label in results_per_batch:
for result_for_class_label in results_per_class_label:
print(f"{result_for_class_label.class_label}: {result_for_class_label.value:.2f}")