Skip to main content
Version: 0.4.x

Ready-To-Use OCR Package

This is a ready-to-use example. All authentication, models, images and example Dockerfile are included. Download it here:

Download OCR Package

Creating a Personal Access Token (PAT) for a Service User

It runs the following script:

from denkflow import Pipeline, ImageTensor
import cv2
import os

## constants
image = "image.bmp"
model_file = "ocr.denkflow" # "ocr_jetson_or_nvidiagpu.denkflow" <- for jetson or nvidia gpu usage
pat = "YOUR-PAT-TOKEN"

### pipeline init and subscribe
pipeline = Pipeline.from_denkflow(
model_file,
pat=pat,
)

print(pipeline)
pipeline.initialize()

detection_receiver = pipeline.subscribe_bounding_box_tensor(
"bounding_box_filter_node/filtered_bounding_boxes",
)
text_receiver = pipeline.subscribe_ocr_tensor("ocr_node/output")

### pipeline run
print(f"image: {image}")
image_tensor = ImageTensor.from_file(image)
pipeline.publish_image_tensor("camera/image", image_tensor)
pipeline.run()
## receive detections and texts
boxes = detection_receiver.receive().to_objects(0.5)
texts = text_receiver.receive().to_objects()

## draw
image_np = cv2.imread(image, 1)
height, width = image_np.shape[:2]
for box, text in zip(boxes, texts):
x1 = int(box.x1 * width)
y1 = int(box.y1 * height)
x2 = int(box.x2 * width)
y2 = int(box.y2 * height)
conf = box.confidence
cv2.rectangle(image_np, (x1, y1), (x2, y2), (0, 0, 255), 2)
cv2.putText(
image_np,
f"{text} {conf * 100:.2f}%",
(x1, y1 - 10 if y1 > 20 else y1 + 20),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
(0, 0, 255),
1,
)

# Save the image to the output folder
os.makedirs("output", exist_ok=True)
output_path = f"output/{image.split('/')[-1]}"
cv2.imwrite(output_path, image_np)
print(f"Successfully wrote output to {output_path}")