/tesseract-container

Dockerized Tesseract OCR (Version 5.x) service with REST endpoints.

Primary LanguagePython

CI Worklfow

Tesseract Container

This repository contains a FastAPI application that uses Tesseract OCR to extract text from images and PDFs. The application exposes several endpoints to upload files, retrieve analysis results, generate searchable PDFs, and check the health of the service.

✨ Features

  • ✅ OCR on Images and PDFs
  • ✅ REST Endpoints
  • ✅ Sync / Async Support
  • ✅ Create searchable PDFs
  • ✅ Add more languages with ease

📖 Contents

1. Pull Image

docker pull jannichorst/tesseract-ocr:latest

2. Run Container

docker run -d -p 8000:8000 jannichorst/tesseract-ocr:latest

3. Usage

Access the Swagger documentation under http://localhost:8000/docs.

import requests

url = "http://localhost:8000/ocr/"
file_path = "path_to_your_image_or_pdf.jpg"

with open(file_path, "rb") as file:
    files = {"file": file}
    response = requests.post(url, files=files)

print("OCR Result:", response.json())

Note

Check out more examples in the demo.iypnb notebook

Perform OCR [SYNC]

  • URL: /ocr/
  • Method: POST
  • Request: Multipart/form-data with a file, language (default: "eng"), DPI (optional), config (optional), and PSM (default: 3)
  • Response: JSON containing OCR results and job information

Example using curl:

curl -X POST "http://localhost:8000/ocr/" -F "file=@path_to_your_file"

Start OCR Processing [ASYNC]

  • URL: /start_ocr/
  • Method: POST
  • Request: Multipart/form-data with a file, language (default: "eng"), DPI (optional), config (optional), and PSM (default: 3)
  • Response: JSON containing a task ID

Example using curl:

curl -X POST "http://localhost:8000/start_ocr/" -F "file=@path_to_your_file"

Get OCR Results

  • URL: /results/{task_id}
  • Method: GET
  • Request: Path parameter with the task ID
  • Response: JSON containing the OCR results or the status of the task

Example using curl:

curl "http://localhost:8000/results/{task_id}"

Create Searchable PDF [SYNC]

  • URL: /create_searchable/
  • Method: POST
  • Request: Multipart/form-data with a file, language (default: "eng"), DPI (optional), PSM (default: 3), and config (optional)
  • Response: Searchable PDF file

Example using curl:

curl -X POST "http://localhost:8000/create_searchable/" -F "file=@path_to_your_file" --output output_ocr.pdf

Get Jobs

  • URL: /jobs
  • Method: GET
  • Request: Query parameter for status (default: "pending")
  • Response: JSON containing job information

Example using curl:

curl "http://localhost:8000/jobs?status=all"

Get System Information

  • URL: /info
  • Method: GET
  • Response: JSON containing system information

Example using curl:

curl "http://localhost:8000/info"

Health Check

  • URL: /health
  • Method: GET
  • Response: JSON indicating the health status of the application

Example using curl:

curl "http://localhost:8000/health"

Swagger Documentation

  • URL: /docs
  • Method: GET
  • Response: Autogenerated documentation & testing area

Note

For the full guide on how to add more languages see: How to add lmore anguages

  1. Clone the repository:

    git clone https://github.com/jannichorst/tesseract-container.git
    cd tesseract-container
  2. To build and run the Docker container in one step:

    ./scripts/build_and_run.sh
  3. Access the Swagger documentation under http://localhost:8000/docs.

  4. Check out the examples in demo.ipynb and install the required packages with:

    pip install -r requirements.txt

The main application code is located in src/app/main.py. The Dockerfile and scripts for building and running the container are located in the root directory and the scripts directory, respectively. Under tests you find a Postman collection that can be run with run-postman-collection.sh (needs Newman CLI).

Directory Structure

tesseract-container
├── Dockerfile
├── README.md
├── requirements.txt
├── examples
│   └── demo.ipynb
├── scripts
│   ├── build.sh
│   ├── run.sh
│   ├── build_and_run.sh
│   └── run-postman-collection.sh
├── src
│   ├── requirements.txt
│   └── app
│       ├── main.py
│       └── ...
└── tests
    ├── postman_collection.json
    ├── test-image.jpg
    └── ...