Downloads · 30 days
155
30% of all-time downloads
qandeelasim13/urdu-ocr-trocr-si26
urdu-ocr-trocr-si26 is a machine learning model from qandeelasim13. Use it for the machine learning task on the model card, and read the license before you ship it in a product. The card lists the license as apache-2.0.
A TrOCR model fine-tuned end-to-end to read printed Urdu (Nastaliq-style) text from images and output editable Unicode text.
Downloads · 30 days
155
30% of all-time downloads
All-time downloads
515
Public
Parameters
334M
1.3 GB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors1.3 GB · 100%
From the Hugging Face model README
A TrOCR model fine-tuned end-to-end to read printed Urdu (Nastaliq-style) text from images and output editable Unicode text.
Try the live demo: Streamlit app
Optical Character Recognition for Urdu lags far behind Latin-script OCR — Urdu's cursive, context-dependent Nastaliq script and the scarcity of labeled datasets make off-the-shelf tools like Tesseract perform poorly out of the box (a baseline test on this project's own data measured only a 2.1% word-recovery rate from Tesseract). This model fine-tunes TrOCR specifically on Urdu text so it can read real-world Urdu images — for example, digitizing scanned documents, signboards, or book pages.
TrOCR pairs a vision encoder (which "looks" at the image) with a text decoder (which "writes out" what it reads). Its decoder's tokenizer is a byte-level BPE tokenizer that can already represent any Unicode text — including Urdu — via UTF-8 byte-level fallback tokens. This model fine-tunes the full pretrained checkpoint, encoder and decoder together, rather than training a decoder from scratch, so it only has to learn Urdu rather than language modeling from zero.
Extracting Urdu text from clean, printed, single-line images — scanned documents, signboards, book pages, or screenshots of printed text. Not intended for handwriting, multi-line paragraphs, or heavily degraded/noisy images.
from transformers import VisionEncoderDecoderModel, TrOCRProcessor
from PIL import Image
import torch
repo_id = "qandeelasim13/urdu-ocr-trocr-si26"
processor = TrOCRProcessor.from_pretrained(repo_id)
model = VisionEncoderDecoderModel.from_pretrained(repo_id)
model.eval()
image = Image.open("your_urdu_image.png").convert("RGB")
pixel_values = processor(images=image, return_tensors="pt").pixel_values
with torch.no_grad():
generated_ids = model.generate(pixel_values, max_length=319, num_beams=4)
text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(text)
| Metric | Score |
|---|---|
| Character Error Rate (CER) | 0.52 |
| Character-level accuracy | 47.66% |
| Training loss | 5.00 → 0.18 (avg per epoch, 15 epochs) |
Sample predictions showed exact or near-exact matches on shorter sentences, with more errors on longer, more complex sentences.
Frameworks: PyTorch, Hugging Face transformers (VisionEncoderDecoderModel, Seq2SeqTrainer), evaluate + jiwer for CER, trained on a Google Colab GPU runtime (Tesla T4).
Qandeel Asim — built during the Code Saviours ML/AI Internship, Batch SI-26.