Surya OCR Tutorial: Document AI for Text Detection, OCR, and Layout Analysis

# Tutorial Surya OCR: Document AI untuk Text Detection, OCR, dan Layout Analysis Surya adalah library Document AI open-source yang powerful untuk melakukan OCR (Optical Character Recognition), text d...

By Ruby Abdullah · · tutorial
SuryaOCRDocument AIComputer VisionPython

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}")

Related Articles

Florence-2: Microsoft's Multi-Task Vision Foundation Model

Florence-2: Model Vision Multi-Task dari Microsoft Daftar Isi Pendahuluan Prasyarat Instalasi Memuat Model Florence-2

Supervision: Computer Vision Toolkit by Roboflow

Supervision: Toolkit Computer Vision dari Roboflow Dalam proyek computer vision, setelah model mendeteksi objek, Anda ma...

OpenCV + Deep Learning Tutorial: Modern Image Processing with Python

OpenCV + Deep Learning: Tutorial Komprehensif Daftar Isi Pendahuluan Prasyarat Dasar-Dasar Preprocessing Gambar [T...

Complete Label Studio Tutorial: Data Labeling for Machine Learning

Tutorial Lengkap Label Studio: Data Labeling untuk Machine Learning Label Studio adalah platform data labeling open-sour...