Surya OCR Tutorial: Document AI for Text Detection, OCR, and Layout Analysis
Surya is a powerful open-source Document AI library for OCR (Optical Character Recognition), text detection, layout analysis, reading order detection, and table recognition. Developed by Vik Paruchuri (creator of Marker), Surya supports over 90 languages and delivers accuracy that is highly competitive with commercial solutions like Google Cloud Vision.
In this tutorial, we will learn how to use Surya for various document AI tasks, from installation to advanced usage for processing complex documents.
Why Surya?
Before we begin, here are some reasons why Surya is worth considering:
- Multi-language: Supports 90+ languages including CJK, Arabic, Indonesian, and many more
- High accuracy: Benchmarks show accuracy comparable to or better than Google Cloud Vision across many languages
- Open-source: GPL-3.0 license, free for non-commercial use
- Feature-complete: OCR, text detection, layout analysis, table recognition, and reading order detection in one package
- GPU acceleration: CUDA support for faster processing
- Easy to use: Simple and intuitive API
Installation
System Requirements
- Python 3.9 or newer
- PyTorch (installed automatically)
- NVIDIA GPU with CUDA (optional but highly recommended for performance)
Installation via pip
pip install surya-ocr
Installation from Source (for development)
git clone https://github.com/VikParuchuri/surya.git
cd surya
pip install -e .
Verify Installation
import surya
print(surya.version)
Additional Dependencies
To process PDF files, also install:
pip install pypdfium2
Basic Usage
1. Text Detection
Text detection is the first step in the OCR pipeline. Surya detects areas where text is present in an image.
from surya.detection import DetectionPredictor
from PIL import Image
Load image
image = Image.open("document.png")
Initialize detector
detector = DetectionPredictor()
Detect text
predictions = detector([image])
View detection results
for prediction in predictions:
for bbox in prediction.bboxes:
print(f"Bounding box: {bbox.bbox}")
print(f"Confidence: {bbox.confidence:.4f}")
print(f"Polygon: {bbox.polygon}")
print()
2. OCR (Optical Character Recognition)
After detecting text areas, we can perform OCR to extract text from images.
from surya.recognition import RecognitionPredictor
from surya.detection import DetectionPredictor
from surya.ocr import OCRPredictor
from PIL import Image
Load image
image = Image.open("document.png")
Initialize predictors
detpredictor = DetectionPredictor()
recpredictor = RecognitionPredictor()
ocrpredictor = OCRPredictor(detpredictor, recpredictor)
Run OCR
predictions = ocrpredictor([image])
Display results
for prediction in predictions:
for textline in prediction.textlines:
print(f"Text: {textline.text}")
print(f"Confidence: {textline.confidence:.4f}")
print(f"BBox: {textline.bbox}")
print()
3. Specifying Languages
Surya can detect languages automatically, but you can also specify languages explicitly for more accurate results:
from surya.recognition import RecognitionPredictor
from surya.detection import DetectionPredictor
from surya.ocr import OCRPredictor
from PIL import Image
image = Image.open("documentmixed.png")
detpredictor = DetectionPredictor()
recpredictor = RecognitionPredictor()
ocrpredictor = OCRPredictor(detpredictor, recpredictor)
Specify language: English
predictions = ocrpredictor([image], langs=[["en"]])
for prediction in predictions:
for textline in prediction.textlines:
print(f"{textline.text}")