Skip to content

Read text with OCR ​

The OCR node reads the text of an image on the controller itself: it finds each line of text, reads it and tells how sure it is. It needs no training. In this guide you read a garment care label and pick its lot code out of everything it says.

What you will learn

  • How to add the OCR node to a flow and choose its task
  • How to see the text it finds, drawn on the image
  • How to read its result and use it in the flow

Before you start

You need a flow that brings images. Here it reads a photo of a label from the controller's disk, /tmp/line-check/care-label.jpg, with rp-image-in into msg.image; see Load and transform an image.

1. Add an OCR node ​

Type ocr in «filter nodes» and drag the node after the one that brings the image, then wire them together.

Add an OCR node

2. Check where it reads and writes ​

Double-click the node. «Input field» is where it finds the image, msg.image as it comes, which is where the node before it left the label. «Output field» is where it leaves the text, msg.inference.ocr.

Check where it reads and writes

3. Choose the task ​

«Task» says what the node does with the image:

  • OCR (Detection + Recognition), as it comes: finds every line of text and reads it.
  • Detection Only: finds where the text is, without reading it, for example to crop it.
  • Recognition Only: reads an image that is already cropped to one line of text.
  • VLM (PaddleOCR-VL): a larger model that reads harder text, more slowly.
  • Document Rotation: tells how far the image is turned, to straighten it first.

4. Draw what it reads ​

Open «Debug Image», tick «Show debug image» and give it a width, here 200 pixels. The node then shows the image under itself with a box around each line of text, its text and how sure it is.

Draw what it reads

5. Look at the advanced settings ​

The «Advanced settings» tab holds the models: PP-OCRv5 to find the text and PP-OCRv5 to read it, the most accurate ones. use_doc_orientation_classify turns a whole image that is upside down or on its side before reading it, and use_textline_orientation does the same line by line. «Restore default config» brings back the settings of the task.

Look at the advanced settings

6. Show the result in the debug panel ​

Click «Done». Add a debug node after the OCR node, open it and set «Output» to msg.inference.ocr, so that the debug panel shows what the node read.

7. Deploy and read the label ​

Click «Deploy». The first time, the controller loads the OCR models, which takes a few seconds: wait until the node says «Ready» and press the button of the inject node. Under the node you see the label with each line it read, and how long it took.

Deploy and read the label

8. Read the result ​

Expand the message in the debug panel. msg.inference.ocr holds:

  • text: everything the label says, line after line;
  • texts: one entry per line, with its text, its score (how sure the node is, from 0 to 1) and coords, the four corners of its box, from 0 to 1;
  • text_largest_box, text_highest_score and text_most_centric: the line in the largest box, the surest one and the one nearest the centre, for labels where that is the one you need;
  • metadata: how far the image was turned before reading it.

Read the result

9. Pick the lot code ​

The label says much more than the lot code. Add a function node after the OCR node and write a rule that finds it among the lines, here the one made of eight letters and digits that starts with a digit:

js
const line = msg.inference.ocr.texts.find((t) => /^\d[A-Z0-9]{7}$/.test(t.text));
msg.payload = line ? line.text : "no lot code";
return msg;

Pick the lot code

10. Read the lot code ​

Click «Done», add a debug node after the function node and deploy. Press the inject node: the new debug node shows the lot code, ready to compare with the one expected, to save or to send to a panel.

Read the lot code

Good to know ​

  • One node, one setup. Each OCR node with its own task and models keeps them loaded on the controller while the node exists. Disable or delete the OCR nodes you no longer use.
  • Turned images. A label that may arrive on its side is read better with use_doc_orientation_classify on, or straightened first with the «Document Rotation» task and rp-rotate.
  • Only part of the image. For a code always printed in the same place, crop the image to it with rp-crop first: reading less text is faster and leaves less to choose from.
  • Expected text. Comparing what was read with what was expected, such as the lot of the batch in production, is a rule like the one above: see Decide pass or fail from the predictions.

Next steps ​

Recorded on Rosepetal release 1.8.15, Node-RED 5.0.10.