AI Hints
Home›Computer Vision Tutorial›Faces, Pose, and Text
Computer Vision · Lesson 26 of 111

Reading Text with OCR

Machines that read.

⏱ 2 min read▶ Video at 44:09🎓 Free
Watch this lesson in the Computer Vision courseWatch on YouTube ↗Full Computer Vision course

Every crate at Orchard Vision has a printed label, with a batch number and a date. Staff type these in by hand. Mistakes cost money when food has to be recalled. Reading text in images is called optical character recognition, and it's one of the oldest dreams in the field.

Reading Text with OCR
From the video: Reading Text with OCR
  • Every crate has a label
  • Typed in by hand
  • Mistakes cost money

How modern OCR works

Modern OCR has two stages. First, a detector finds regions of text. Then a recogniser reads each region, often using a convolutional network followed by a sequence model. Good results depend on clean input. Straighten the page, boost the contrast, and keep the text large enough. All of the classic tools that you learned earlier help here.

Detectfind the text regions
Recogniseread each region
Preparestraighten and clean
Checkverify with rules

Reading a page

Here's a real engine, reading a scanned page. It gives each line of text, its position, and a confidence score. Run it. It's reading a page with old printing and uneven light, and it does well. The confidence score matters, because you can send low confidence lines to a person.

ocr.pypython
import cv2
from rapidocr_onnxruntime import RapidOCR
from skimage import data

engine = RapidOCR()
image = data.page()

result, _ = engine(cv2.cvtColor(image, cv2.COLOR_GRAY2BGR))

print("Lines read:", len(result))
for box, text, score in result[:4]:
    print(f"{float(score):.2f}  {text[:60]}")
Output
Lines read: 5
0.94  Region-basedsegmentation
0.92  Let us first determine markers of the coins and the
0.89  background. These markers are pixels that we can label
0.91  unambiguously as either object or background. Here,

Reading a crate label

Now AppleEye reads a label. We draw one, read it, then use regular expressions to pull out the batch and date. Run it. Clean, structured data, straight from an image. Combining OCR with strict patterns is powerful, because the pattern catches many reading errors. A batch number that doesn't match gets flagged for a human.

label.pypython
import cv2
import numpy as np
from rapidocr_onnxruntime import RapidOCR

img = np.full((180, 520, 3), 245, np.uint8)
cv2.putText(img, "BATCH 2291", (20, 70), cv2.FONT_HERSHEY_SIMPLEX, 1.4, (20, 20, 20), 3)
cv2.putText(img, "BEST BY 12/2026", (20, 140), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (20, 20, 20), 3)

result, _ = RapidOCR()(img)
text = " ".join(line[1] for line in result)
print("Read:", text)

import re
print("Batch:", re.findall(r"BATCH\s*(\d+)", text))
print("Date: ", re.findall(r"\d{2}/\d{4}", text))
Output
Read: BATCH  2291 BEST BY 12/2026
Batch: ['2291']
Date:  ['12/2026']

Summary

Detect the text, read it, and verify it with rules. AppleEye can now see, measure, count, and read. Next, the newest ideas in vision. Models that don't just recognise things, but understand images and words together.

Key takeaways
  • Two stages: detect, recognise
  • Clean the image first
  • Verify with patterns
Next up: Vision Transformers — attention comes to vision