Optical character recognition (OCR)
This example shows how to load a pre-built OCR pipeline from a .denkflow file.
- Python
- C / C++
from denkflow import Pipeline, ImageTensor
# --- Configuration ---
pat = "YOUR-PAT"
denkflow_path = "path/to/your_exported_ocr.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)
# --- Initialization ---
pipeline.initialize()
# --- Subscribe to Outputs (Adjust topic names based on print(pipeline)) ---
# Example topic names
bbox_topic = "bounding_box_filter_node/filtered_bounding_boxes" # Boxes for text regions. Coordinates are normalize in [0.0, 1.0].
ocr_topic = "ocr_node/output" # OCR text output
detection_receiver = pipeline.subscribe(bbox_topic)
text_receiver = pipeline.subscribe(ocr_topic)
# --- Publish Input Image (Adjust topic name based on print(pipeline)) ---
# Example topic name
input_image_topic = "camera/image"
image_path = "path/to/your/image.jpg" # Define image_path closer to its use
image = ImageTensor.from_file(image_path)
# For in-memory image data such as NumPy arrays, see the Creating ImageTensors guide:
# ../creating-image-tensors.md
pipeline.publish_image_tensor(input_image_topic, image)
# --- Run Pipeline ---
pipeline.run()
# --- Receive and Process Results ---
detections = detection_receiver.receive_bounding_box_tensor().to_objects(0.5)
ocr_results = text_receiver.receive_ocr_tensor().to_objects() # List of strings
print(f"\nDetected {len(ocr_results)} text regions:")
for bbox, text in zip(detections, ocr_results):
print(f"- Text: '{text}'")
print(f" At BBox: ({bbox.x1}, {bbox.y1}), ({bbox.x2}, {bbox.y2})")
#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() {
DenkflowHubLicenseSource* hub_license_source = NULL;
DenkflowPipeline* pipeline = NULL;
DenkflowInitializedPipeline* initialized_pipeline = NULL;
DenkflowImageTensor* image_tensor = NULL;
DenkflowReceiverTensor* receiver = NULL;
DenkflowOcrTensor* ocr_tensor = NULL;
DenkflowOcrResults* ocr_results = NULL;
enum DenkflowResult r;
const uint64_t timeout_ms = 3000;
const char* pat = "personal_access_token";
const char* model_file = "path/to/model/file.denkflow";
const char* image_path = "path/to/an/image.jpg";
const char* input_topic = "camera/image";
const char* output_topic = "ocr_node/output";
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, model_file, (void*)hub_license_source);
handle_error(r, "denkflow_pipeline_from_denkflow");
r = denkflow_initialize_pipeline(&initialized_pipeline, &pipeline);
handle_error(r, "denkflow_initialize_pipeline");
r = denkflow_initialized_pipeline_subscribe(&receiver, initialized_pipeline, output_topic);
handle_error(r, "denkflow_initialized_pipeline_subscribe");
r = denkflow_image_tensor_from_file(&image_tensor, image_path);
handle_error(r, "denkflow_image_tensor_from_file");
r = denkflow_initialized_pipeline_publish_image_tensor(initialized_pipeline, input_topic, &image_tensor);
handle_error(r, "denkflow_initialized_pipeline_publish_image_tensor");
r = denkflow_initialized_pipeline_run(initialized_pipeline, timeout_ms);
handle_error(r, "denkflow_initialized_pipeline_run");
r = denkflow_receiver_receive_ocr_tensor(&ocr_tensor, receiver);
handle_error(r, "denkflow_receiver_receive_ocr_tensor");
r = denkflow_ocr_tensor_to_objects(&ocr_results, ocr_tensor);
handle_error(r, "denkflow_ocr_tensor_to_objects");
if (r == DenkflowResult_Ok) {
for (int i = 0; i < ocr_results->ocr_strings_length; i++) {
printf("String %d: %s\n", i, ocr_results->ocr_strings[i].c_string);
}
}
r = denkflow_free_object((void**)&hub_license_source);
handle_error(r, "denkflow_free_object");
r = denkflow_free_object((void**)&initialized_pipeline);
handle_error(r, "denkflow_free_object");
r = denkflow_free_object((void**)&receiver);
handle_error(r, "denkflow_free_object");
r = denkflow_free_object((void**)&ocr_tensor);
handle_error(r, "denkflow_free_object");
r = denkflow_free_object((void**)&ocr_results);
handle_error(r, "denkflow_free_object");
return 0;
}