Basic Object Detection
This example loads a complete object detection pipeline file (*.denkflow) exported from the Hub.
- Python
- C
import denkflow
pat = "YOUR-PAT"
denkflow_path = "path/to/model/file.denkflow"
image_path = "path/to/an/image.jpg"
input_topic = "camera/image"
output_topic = "bounding_box_filter_node/filtered_bounding_boxes"
confidence_threshold = 0.5
pipeline = denkflow.Pipeline.from_denkflow(
denkflow_path,
pat=pat,
)
pipeline.initialize()
receiver = pipeline.subscribe(output_topic)
image_tensor = denkflow.ImageTensor.from_file(image_path)
pipeline.publish_image_tensor(input_topic, image_tensor)
pipeline.run()
tensor = receiver.receive_bounding_box_tensor()
results = tensor.to_objects(confidence_threshold)
print("Object Detection Results:")
for bounding_box in results:
print(f"{bounding_box.class_label.name}: {bounding_box.confidence:.2f}")
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "denkflow.h"
void handle_error(enum DenkflowResult error_code, const char* function_name) {
printf("%s: %d", function_name, (int32_t)error_code);
if (error_code != DenkflowResult_Ok) {
char error_buffer[ERROR_BUFFER_SIZE];
memset(error_buffer, 0, ERROR_BUFFER_SIZE);
get_last_error(error_buffer);
printf(" (%s)\n", error_buffer);
exit(EXIT_FAILURE);
}
printf("\n");
}
int main() {
Pipeline* pipeline = NULL;
InitializedPipeline* initialized_pipeline = NULL;
ImageTensor* image_tensor = NULL;
Receiver_Tensor* receiver = NULL;
BoundingBoxTensor* tensor = NULL;
BoundingBoxResults* results = NULL;
HubLicenseSource* hub_license_source = NULL;
enum DenkflowResult r;
const uint64_t timeout_ms = 8000;
const char* pat = "YOUR-PAT";
const char* denkflow_path = "path/to/model/file.denkflow";
const char* image_path = "path/to/an/image.jpg";
const char* input_topic = "camera/image";
const char* output_topic = "bounding_box_filter_node/filtered_bounding_boxes";
const char* endpoint = "https://hub-staging.denkweit.ai/hasura/v1/graphql";
const float confidence_threshold = 0.5;
r = hub_license_source_from_pat(&hub_license_source, pat, NULL_BYTE, endpoint);
handle_error(r, "hub_license_source_from_pat");
r = pipeline_from_denkflow(&pipeline, denkflow_path, (void*)hub_license_source);
handle_error(r, "pipeline_from_denkflow");
r = initialize_pipeline(&initialized_pipeline, &pipeline);
handle_error(r, "initialize_pipeline");
r = initialized_pipeline_subscribe(&receiver, initialized_pipeline, output_topic);
handle_error(r, "initialized_pipeline_subscribe");
r = image_tensor_from_file(&image_tensor, image_path);
handle_error(r, "image_tensor_from_file");
r = initialized_pipeline_publish_image_tensor(initialized_pipeline, input_topic, &image_tensor);
handle_error(r, "initialized_pipeline_publish_image_tensor");
r = initialized_pipeline_run(initialized_pipeline, timeout_ms);
handle_error(r, "initialized_pipeline_run");
r = receiver_receive_bounding_box_tensor(&tensor, receiver);
handle_error(r, "receiver_receive_bounding_box_tensor");
r = bounding_box_tensor_to_objects(&results, tensor, confidence_threshold);
handle_error(r, "bounding_box_tensor_to_objects");
if (r == DenkflowResult_Ok) {
for (int b = 0; b < results->bounding_boxes_length; b++) {
printf(" %s: %f\n",
results->bounding_boxes[b].class_label.name,
results->bounding_boxes[b].confidence
);
}
}
r = free_object((void**)&hub_license_source);
handle_error(r, "free_object");
r = free_object((void**)&initialized_pipeline);
handle_error(r, "free_object");
r = free_object((void**)&receiver);
handle_error(r, "free_object");
r = free_object((void**)&tensor);
handle_error(r, "free_object");
r = free_object((void**)&results);
handle_error(r, "free_object");
return 0;
}