OCR Server used to scan text on any images.
Find a file
2025-11-13 18:33:08 +02:00
src fix: Use default alphabet for the recognition to work properly 2025-11-13 18:33:08 +02:00
.gitignore feat: Init 2025-03-05 17:18:57 +02:00
Cargo.lock chore(deps): Upgrade deps to the latest versions 2025-11-13 18:32:45 +02:00
Cargo.toml chore(deps): Upgrade deps to the latest versions 2025-11-13 18:32:45 +02:00
README.md docs: Update README with better examples 2025-03-05 19:48:31 +02:00

OCR-Server

Optical character recognition server written in Rust.

Development

Always use release mode; debug target won't work well with ocrs library.

Building:

cargo build --release

Running:

cargo run --release

Usage

Here are a few examples of how to use the server.

CLI

curl -X POST -F "file=@image.png" https://ocr.pkg.rs/v1/recognize

Python

import asyncio
import io
import typing

import aiohttp
from PIL import Image

async def run_remote_ocr(image: str | bytes | Image.Image) -> list[str]:
    """Connect to the remote OCR service and extract text from the image."""
    if isinstance(image, str):
        with open(image, "rb") as f:
            image_bytes = f.read()
    elif isinstance(image, Image.Image):
        with io.BytesIO() as output:
            image.save(output, format="PNG")
            image_bytes = output.getvalue()
    elif isinstance(image, bytes):
        image_bytes = image
    else:
        raise ValueError("Unsupported image type")

    url = "https://ocr.pkg.rs/v1/recognize"

    async with aiohttp.ClientSession() as session:
        form_data = aiohttp.FormData()
        form_data.add_field(
            "file",
            image_bytes,
            filename="image.png",
            content_type="image/png"
        )

        async with session.post(url, data=form_data) as response:
            data = await response.json()

            if response.status == 200 and data.get("status") == 200:
                return data.get("data", [])
            else:
                raise RuntimeError(data.get(
                    "message", "Remote OCR failed to process the image"
                ))

async def main():
    image_path = "image.png"
    result = await run_remote_ocr(image_path)
    print(result)

if __name__ == "__main__":
    asyncio.run(main())