Optical Character Recognition (OCR)
This example shows how to load a pre-built OCR pipeline from a .denkflow
file.
- Python
- C
from denkflow import Pipeline, ImageTensor
# --- Configuration ---
pat = "YOUR-PAT-TOKEN"
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_bounding_box_tensor(bbox_topic)
text_receiver = pipeline.subscribe_ocr_tensor(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)
pipeline.publish_image_tensor(input_image_topic, image)
# --- Run Pipeline ---
pipeline.run()
# --- Receive and Process Results ---
detections = detection_receiver.receive().to_objects(0.5)
ocr_results = text_receiver.receive().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 <iostream>
#include <vector>
#include "denkflow.h"
// --- Helper Function to Interpret Error Codes ---
void process_return(DenkflowResult return_value, std::string function_name) {
std::string error_message = get_last_error();
std::cout << function_name << " returned " << (int32_t)return_value;
if (error_message.size() > 0) {
std::cout << " [" << error_message << "]";
}
std::cout << std::endl;
}
const char NULL_BYTE [1] = {'\0'};
DenkflowResult r;
HubLicenseSource* hub_license_source = nullptr;
Pipeline* pipeline = nullptr;
InitializedPipeline* initialized_pipeline = nullptr;
ImageTensor* image_tensor = nullptr;
Receiver<OcrTensor>* receiver = nullptr;
OcrTensor* ocr_tensor = nullptr;
OcrResults* ocr_results = nullptr;
// Fill these with custom values
std::string model_file = "path/to/model/file.denkflow";
std::string pat = "personal_access_token";
std::string image_path = "path/to/an/image.jpg";
// Default Values for OCR
std::string input_topic = "camera/image";
std::string output_topic = "ocr_node/output";
// --- Create License Source ---
r = hub_license_source_from_pat(&hub_license_source, pat.c_str(), NULL_BYTE, NULL_BYTE);
process_return(r, "hub_license_source_from_pat");
// --- Read Model File ---
r = pipeline_from_denkflow(&pipeline, model_file.c_str(), (void**)&hub_license_source);
process_return(r, "pipeline_from_denkflow");
// --- Initialization ---
r = initialize_pipeline(&initialized_pipeline, &pipeline);
process_return(r, "initialize_pipeline");
// --- Subscribe to Outputs ---
r = initialized_pipeline_subscribe_ocr_tensor(&receiver, initialized_pipeline, output_topic.c_str());
process_return(r, "initialized_pipeline_subscribe_ocr_tensor");
// --- Send Image into Pipeline ---
r = image_tensor_from_file(&image_tensor, image_path.c_str());
process_return(r, "image_tensor_from_file");
r = initialized_pipeline_publish_image_tensor(initialized_pipeline, input_topic.c_str(), &image_tensor);
process_return(r, "initialized_pipeline_publish_image_tensor");
// --- Run Pipeline ---
r = initialized_pipeline_run(initialized_pipeline, 3000);
process_return(r, "initialized_pipeline_run");
// --- Receive and Process Results ---
r = receiver_receive_ocr_tensor(&ocr_tensor, receiver);
process_return(r, "receiver_receive_ocr_tensor");
r = ocr_tensor_to_objects(&ocr_results, ocr_tensor);
process_return(r, "ocr_tensor_to_objects");
if (r == DenkflowResult::Ok) {
for (int i = 0; i < ocr_results->ocr_strings_length; i++) {
std::cout
<< "String "
<< i
<< ": "
<< ocr_results->ocr_strings[i].c_string
<< std::endl;
}
}
// --- Free Allocated Objects ---
r = free_all_objects();
process_return(r, "free_all_objects");