অধ্যায়Phase 5 · ডিপ লার্নিং ফর ভিশন
5.7 20 মিনিট পড়া

OCR Systems

Tesseract ও EasyOCR।

🎬 গল্প দিয়ে শুরু
ছবিতে লেখা — মেশিন কীভাবে পড়বে? OCR (Optical Character Recognition) — ক্যামেরাকে চোখ থেকে পাঠক বানানোর প্রযুক্তি। ID card, রসিদ, বাংলা সাইনবোর্ড — সব এই অধ্যায়ের লক্ষ্য।

OCR pipeline

text
Image → Pre-process → Text Detection → Text Recognition → Post-process
        (denoise,    (where is text?)   (what does it say?)  (spell, format)
         deskew,
         binarize)

Tesseract — classic & free

bash
# Linux
sudo apt install tesseract-ocr tesseract-ocr-ben
pip install pytesseract
python
import cv2, pytesseract

img = cv2.imread("receipt.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.threshold(gray, 0, 255,
       cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

text = pytesseract.image_to_string(gray, lang="eng+ben")
print(text)

# word-level box
data = pytesseract.image_to_data(gray, output_type=pytesseract.Output.DICT)
Bengali support
ben.traineddata দরকার। Tesseract 5.x-এ বাংলা মোটামুটি ভালো, কিন্তু complex font/handwriting-এ EasyOCR/PaddleOCR বেশি ভালো।

EasyOCR — deep learning, 80+ language

python
# pip install easyocr
import easyocr

reader = easyocr.Reader(["en", "bn"], gpu=False)
results = reader.readtext("signboard.jpg")
for bbox, text, conf in results:
    print(f"{conf:.2f}  {text}")

PaddleOCR — fastest & most accurate (2024+)

bash
pip install paddleocr paddlepaddle
python
from paddleocr import PaddleOCR
ocr = PaddleOCR(use_angle_cls=True, lang="en")
res = ocr.ocr("doc.jpg", cls=True)
for line in res[0]:
    box, (text, conf) = line
    print(conf, text)

Pre-processing tricks (accuracy 2x)

  • Grayscale → adaptive threshold।
  • Deskew — minAreaRect দিয়ে কোণ ঠিক করা।
  • Border crop, noise blur।
  • Resize x2 — Tesseract ছোট লেখায় বেশি ভুল।
  • Document detect + perspective warp (CamScanner pattern)।
python
import cv2, numpy as np

def deskew(gray):
    coords = np.column_stack(np.where(gray > 0))
    angle = cv2.minAreaRect(coords)[-1]
    angle = -(90 + angle) if angle < -45 else -angle
    (h, w) = gray.shape
    M = cv2.getRotationMatrix2D((w//2, h//2), angle, 1.0)
    return cv2.warpAffine(gray, M, (w, h),
                          flags=cv2.INTER_CUBIC,
                          borderMode=cv2.BORDER_REPLICATE)

Real-world projects

  • NID/Passport scanner — field extraction।
  • Restaurant receipt parser — item, price।
  • License plate recognition (LPR)।
  • Handwritten form digitization।
  • Bengali signboard translator।
প্র্যাকটিস টাস্ক
  1. একটি receipt scan-এ Tesseract চালিয়ে text বের করুন।
  2. EasyOCR দিয়ে বাংলা signboard পড়ার script লিখুন।
  3. License plate region detect → crop → OCR — দুই-step pipeline বানান।

সারসংক্ষেপ

  • OCR = detect text region + recognize characters।
  • Tesseract সহজ; EasyOCR/PaddleOCR বেশি accurate, deep learning।
  • Pre-process (binarize, deskew, resize) অর্ধেক যুদ্ধ।
  • বাংলা OCR — EasyOCR best, কিন্তু font-নির্ভর।