Optical character recognition (OCR)
An OCR pipeline detects text regions and recognizes their content. Each detected region is a top-level InferenceResult with a bounding_box, and the recognized text is carried on its sub_results. See SimplifiedPipeline for the shared setup.
- Python
- C#
- C / C++
import denkflow
pipeline = denkflow.Pipeline.from_denkflow("path/to/model.denkflow", pat="YOUR-PAT")
simplified = denkflow.SimplifiedPipeline(pipeline)
results = simplified.run("path/to/image.jpg", confidence_threshold=0.5)
for image_result in results:
for r in image_result.results:
for sub in r.sub_results:
if sub.text is not None:
print(sub.text)
using DenkFlow;
using var license = HubLicenseSource.FromPat("YOUR-PAT");
using var pipeline = Pipeline.FromDenkflow("path/to/model.denkflow", license);
using var simplified = new SimplifiedPipeline(pipeline);
using ImageInferenceResults results =
simplified.Run("path/to/image.jpg", confidenceThreshold: 0.5f);
foreach (ImageInferenceResult imageResult in results)
{
foreach (InferenceResult result in imageResult.Results)
{
foreach (InferenceResult subResult in result.SubResults)
{
if (subResult.Text is string text)
{
Console.WriteLine(text);
}
}
}
}
DenkflowPipeline* pipeline = NULL;
DenkflowSimplifiedPipeline* simplified = NULL;
DenkflowImageInferenceResults* results = NULL;
DenkflowHubLicenseSource* license = NULL;
handle_error(denkflow_hub_license_source_from_pat(&license, "YOUR-PAT", NULL, NULL),
"denkflow_hub_license_source_from_pat");
handle_error(denkflow_pipeline_from_denkflow(&pipeline, "path/to/model.denkflow", (void*)license),
"denkflow_pipeline_from_denkflow");
handle_error(denkflow_simplified_pipeline_new(&simplified, &pipeline),
"denkflow_simplified_pipeline_new");
handle_error(denkflow_simplified_pipeline_run_from_file(&results, simplified, "path/to/image.jpg", 0.5f),
"denkflow_simplified_pipeline_run_from_file");
for (size_t b = 0; b < results->image_results_length; ++b) {
DenkflowImageInferenceResult* image_result = &results->image_results[b];
for (size_t i = 0; i < image_result->results_length; ++i) {
DenkflowInferenceResult* r = &image_result->results[i];
for (size_t s = 0; s < r->sub_results_length; ++s) {
if (r->sub_results[s].text != NULL) {
printf("%s\n", r->sub_results[s].text);
}
}
}
}
denkflow_inference_results_free(&results);
denkflow_simplified_pipeline_free(&simplified);
denkflow_free_object((void**)&license);
Draw OCR text
Enable annotation labels to draw recognized OCR text as neutral chips in the
top-left corner. Text associated with a detected region is also included on
that region's label. Set annotation_label_font_size in Python or
LabelFontSize in C# to a positive value; use the corresponding C annotation
function's annotation_label_font_size argument. See
Drawing results.