Text Detection & OCR
Module: Computer Vision | Difficulty: Intermediate
Scene Text Detection
EAST (Efficient and Accurate Scene Text Detector)
Outputs per-pixel confidence and geometry:
Geometry encoding: for rotated rectangles.
CRAFT (Character Region Awareness)
Detects character regions and affinities between characters.
CRNN Text Recognition
CTC loss handles alignment between predicted sequence and target:
import torch
import torch.nn as nn
class CRNN(nn.Module):
def __init__(self, num_chars=37):
super().__init__()
self.cnn = nn.Sequential(
nn.Conv2d(1, 64, 3, 1, 1), nn.ReLU(True), nn.MaxPool2d(2, 2),
nn.Conv2d(64, 128, 3, 1, 1), nn.ReLU(True), nn.MaxPool2d(2, 2),
nn.Conv2d(128, 256, 3, 1, 1), nn.BatchNorm2d(256), nn.ReLU(True),
nn.Conv2d(256, 256, 3, 1, 1), nn.ReLU(True), nn.MaxPool2d((2, 1), (2, 1)),
nn.Conv2d(256, 512, 3, 1, 1), nn.BatchNorm2d(512), nn.ReLU(True),
nn.Conv2d(512, 512, 3, 1, 1), nn.ReLU(True), nn.MaxPool2d((2, 1), (2, 1)),
nn.Conv2d(512, 512, 2, 1, 0), nn.BatchNorm2d(512), nn.ReLU(True),
)
self.rnn = nn.LSTM(512, 256, bidirectional=True, batch_first=True)
self.fc = nn.Linear(512, num_chars)
def forward(self, x):
conv = self.cnn(x)
b, c, h, w = conv.size()
conv = conv.squeeze(2).permute(0, 2, 1)
rnn_out, _ = self.rnn(conv)
return self.fc(rnn_out)
Key Takeaways
- EAST provides fast and accurate text detection
- CRNN + CTC handles variable-length text recognition
- End-to-end systems jointly detect and recognize text