OCR (Optical Character Recognition) converts text in images – scanned documents, invoices, number plates, signboards – into editable text. Python makes it easy with Tesseract, EasyOCR or PaddleOCR, and modern vision-language models can read and understand whole documents.
What You Will Learn
- Install and use Tesseract with pytesseract
- Preprocess images with OpenCV for better accuracy
- Read Hindi and Punjabi text
- EasyOCR for photos and scene text
- When to use document AI and vision LLMs
Prerequisites
Tesseract with pytesseract
# 1. Install the Tesseract engine (Windows installer / apt install tesseract-ocr / brew install tesseract)
# 2. pip install pytesseract opencv-python
import cv2, pytesseract
# Windows only: pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
img = cv2.imread("invoice.png")
text = pytesseract.image_to_string(img, lang="eng")
print(text)
Explanation: pytesseract is only a wrapper: the Tesseract program must be installed separately. image_to_string returns all text; image_to_data returns words with positions and confidence scores.
Preprocessing Matters
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.resize(gray, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC) # upscale small text
gray = cv2.medianBlur(gray, 3) # remove specks
bw = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 31, 10) # handle uneven lighting
text = pytesseract.image_to_string(bw, config="--oem 3 --psm 6")
Explanation: Tesseract works best on dark text on a white background at about 300 DPI (capital letters roughly 20–30 pixels tall). Upscaling, denoising and adaptive thresholding often fix poor results on phone photos. --psm sets the page layout mode: 6 = one block of text, 7 = one line, 11 = sparse text.
| –psm | Use for |
|---|---|
| 3 | Fully automatic page (default) |
| 6 | A single uniform block (receipts, paragraphs) |
| 7 | A single text line (number plates, labels) |
| 11 | Sparse scattered text |
Hindi and Punjabi
text = pytesseract.image_to_string(img, lang="hin") # Hindi (Devanagari)
text = pytesseract.image_to_string(img, lang="pan") # Punjabi (Gurmukhi)
text = pytesseract.image_to_string(img, lang="eng+hin") # mixed documents
Explanation: Install the language data first (e.g. tesseract-ocr-hin, tesseract-ocr-pan on Linux, or tick the languages in the Windows installer). tesseract --list-langs shows what is installed.
EasyOCR for Photos
# pip install easyocr
import easyocr
reader = easyocr.Reader(["en", "hi"]) # downloads models on first run
for box, text, conf in reader.readtext("shop_sign.jpg"):
print(text, round(conf, 2))
Explanation: EasyOCR uses deep learning for both text detection and recognition, so it handles signboards, curved text and photos better than Tesseract, at the cost of speed (a GPU helps). PaddleOCR is another strong option.
Choosing a Tool
| Tool | Best for | Notes |
|---|---|---|
| Tesseract | Clean scans, printed documents | Free, fast, 100+ languages |
| EasyOCR / PaddleOCR | Photos, scene text | Deep learning, GPU helps |
| Cloud OCR (Google Vision, Azure, AWS Textract) | Forms, tables, handwriting at scale | Paid; strong layout and table extraction |
| Vision LLMs (GPT, Claude, Gemini) | Understanding documents, extracting fields to JSON | Flexible; verify outputs and mind data privacy |
Project Ideas
- Invoice reader that extracts invoice number, date and total to a spreadsheet
- Number-plate reader: YOLO detection + OCR on the plate crop
- Digitise old Punjabi or Hindi printed books
- Business-card scanner that saves contacts
Series
Computer vision series: OpenCV Intro · Image Processing · Image Classification · Data Augmentation · CNN · Transfer Learning · Object Detection (YOLO) · Image Segmentation · OCR · Face Detection
Interview Questions
Q: What is OCR?
Converting images of text into machine-readable text.
Q: How can you improve Tesseract accuracy?
Increase resolution, convert to grayscale, denoise, binarise (adaptive threshold), deskew and choose the right page segmentation mode and language.
Practice Exercises
- OCR a photo of a printed page before and after preprocessing and compare the text.
- Extract the total amount from five receipts using OCR plus a regular expression.
Summary
Use Tesseract for clean printed text, preprocess with OpenCV, add language packs for Hindi and Punjabi, EasyOCR/PaddleOCR for photos and cloud or vision-LLM tools for complex documents.
Machine Learning Tutorials, Courses and Certifications