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

Using NumPy Arrays

This example loads a complete classification pipeline file (.denkflow) exported from the Hub. Instead of reading the image data from a file, it is instead read via OpenCV, which produces a NumPy array. This array is then converted to an ImageTensor using the from_numpy_opencv method. NumPy arrays produced by OpenCV have the shape [Height, Width, Channels], which is what the function expects. You can also use the from_numpy method, which expects a NumPy array of dtype float32 with the values normalized to the range [0, 1] and the shape [Batch Size, Channels, Height, Width].

import denkflow
import cv2

pat = "YOUR-PAT"

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

input_topic = "camera/image"
output_topic = "classification_node/output"

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

pipeline.initialize()

# Read the image file into an OpenCV object. This produces a NumPy array.
img = cv2.imread(image_path, flags=cv2.IMREAD_COLOR_RGB)

# Convert the NumPy array to an ImageTensor.
image_tensor = denkflow.ImageTensor.from_numpy_opencv(img)

pipeline.publish_image_tensor(input_topic, image_tensor)

results_receiver = pipeline.subscribe(output_topic)

pipeline.run()

tensor = results_receiver.receive_scalar_tensor()

results_per_image = tensor.to_objects()

print("Classification Results:")
for image_results in results_per_image:
for class_label_result in image_results:
print(f"{class_label_result.class_label}: {class_label_result.value:.2f}")