PipelineWrapper
This example runs an exported object-detection .denkflow through PipelineWrapper and draws annotated images. Results are returned in absolute pixel coordinates, ready to use without subscribe/receive or tensor decoding.
For the classic subscribe/receive flow, see Basic object detection.
- Python
- C / C++
import denkflow
pat = "YOUR-PAT"
denkflow_path = "path/to/model/file.denkflow"
image_path = "path/to/an/image.jpg"
confidence_threshold = 0.5
pipeline = denkflow.Pipeline.from_denkflow(denkflow_path, pat=pat)
wrapper = denkflow.PipelineWrapper(pipeline)
image_tensor = denkflow.ImageTensor.from_file(image_path)
results = wrapper.run(image_tensor, confidence_threshold=confidence_threshold)
for image_result in results:
for r in image_result.results:
if r.bounding_box:
bb = r.bounding_box
print(f"[{r.index}] {bb.class_label.name}: {bb.confidence:.2f}")
annotated = image_tensor.to_images_with_annotations(
results,
annotation_label_font_size=16.0,
)
# annotated: uint8 NumPy array with shape [batch, height, width, 3]
#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 = (char*)malloc(DENKFLOW_ERROR_BUFFER_SIZE);
denkflow_get_last_error(error_buffer);
printf(" (%s)\n", error_buffer);
free(error_buffer);
exit(EXIT_FAILURE);
}
printf("\n");
}
int main() {
DenkflowPipeline* pipeline = NULL;
DenkflowPipelineWrapper* wrapper = NULL;
DenkflowImageTensor* image_tensor = NULL;
DenkflowImageTensor* annotation_tensor = NULL;
DenkflowImageInferenceResults* inference_results = NULL;
DenkflowHubLicenseSource* hub_license_source = NULL;
uint8_t* annotated_buffer = NULL;
size_t annotated_batch = 0;
size_t annotated_height = 0;
size_t annotated_width = 0;
size_t annotated_channels = 0;
enum DenkflowResult r;
const char* pat = "YOUR-PAT";
const char* denkflow_path = "path/to/model/file.denkflow";
const char* image_path = "path/to/an/image.jpg";
const float confidence_threshold = 0.5f;
r = denkflow_hub_license_source_from_pat(&hub_license_source, pat, NULL, NULL);
handle_error(r, "denkflow_hub_license_source_from_pat");
r = denkflow_pipeline_from_denkflow(&pipeline, denkflow_path, (void*)hub_license_source);
handle_error(r, "denkflow_pipeline_from_denkflow");
r = denkflow_image_tensor_from_file(&image_tensor, image_path);
handle_error(r, "denkflow_image_tensor_from_file");
r = denkflow_pipeline_wrapper_new(&wrapper, &pipeline);
handle_error(r, "denkflow_pipeline_wrapper_new");
r = denkflow_pipeline_wrapper_run(
&inference_results, wrapper, &image_tensor, confidence_threshold);
handle_error(r, "denkflow_pipeline_wrapper_run");
for (size_t b = 0; b < inference_results->image_results_length; ++b) {
DenkflowImageInferenceResult* image_result =
&inference_results->image_results[b];
for (size_t i = 0; i < image_result->results_length; ++i) {
DenkflowInferenceResult* result = &image_result->results[i];
if (result->bounding_box != NULL) {
DenkflowBoundingBox* bb = result->bounding_box;
printf("[%zu] %s: %f\n",
result->index,
bb->class_label.name,
bb->confidence);
}
}
}
/* run() takes ownership of image_tensor; reload for annotation */
r = denkflow_image_tensor_from_file(&annotation_tensor, image_path);
handle_error(r, "denkflow_image_tensor_from_file");
r = denkflow_image_tensor_to_images_with_inference_results(
annotation_tensor,
inference_results,
false, /* draw_orientation_lines */
0.0f, /* segmentation_fill_alpha: 0 = outline segmentations */
16.0f, /* annotation_label_font_size: 0 = no labels */
&annotated_buffer,
&annotated_batch,
&annotated_height,
&annotated_width,
&annotated_channels);
handle_error(r, "denkflow_image_tensor_to_images_with_inference_results");
r = denkflow_image_buffer_free(
annotated_buffer,
annotated_batch * annotated_height * annotated_width * annotated_channels);
handle_error(r, "denkflow_image_buffer_free");
r = denkflow_inference_results_free(&inference_results);
handle_error(r, "denkflow_inference_results_free");
r = denkflow_image_tensor_free(&annotation_tensor);
handle_error(r, "denkflow_image_tensor_free");
r = denkflow_pipeline_wrapper_free(&wrapper);
handle_error(r, "denkflow_pipeline_wrapper_free");
r = denkflow_free_object((void**)&hub_license_source);
handle_error(r, "denkflow_free_object");
return 0;
}