/ocr-api

This API uses a pre-trained model from hugging face to convert captcha images into text. It is based on Python and uses FastApi, Docker and Docker Compose in order to provide a simple, fast and efficient way to convert images into text.

Primary LanguagePython

OCR - API documentation

This API uses a pre-trained model from hugging face to convert captcha images into text. It is based on Python and uses FastApi, Docker and Docker Compose in order to provide a simple, fast and efficient way to convert images into text.

assets/ocr.png

Setup Project

install git lfs (git large file system)

Ubuntu

curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
sudo apt-get install git-lfs

update token in .env file

go to api/ directory and clone model

git clone https://huggingface.co/microsoft/trocr-small-printed

Build Docker images

docker compose build

Run Docker images with docker compose

docker compose up --force-recreate --build
  • use -d flag if want to detached after containers up
  • use --force-recreate flag for re create docker containers
  • use --build flag for re create docker images

For Example ** Build and deployment **

docker compose up -d --build --force-recreate
docker compose --compatibility up -d --build --force-recreate 

Connect to docker container

docker exec -ti ocr-api_ocr-api bash 

use NAME to specify container name, normally docker container name is:

manage docker containers with GUI

Setup portainer docker container

docker run -d  -p 2400:9000 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:latest

then go to http://localhost:2400 and setup admin credentials

for always started up use this roles:

docker update --restart unless-stopped portainer

project structure example

api
├── sub
│   ├── router.py
│   ├── schemas.py  # pydantic models
│   ├── models.py  # db models
│   ├── dependencies.py
│   ├── config.py  # local configs
│   ├── tasks.py  # celery tasks
│   ├── constants.py
│   ├── exceptions.py
│   ├── service.py
│   └── utils.py
│   │   
│   ├── config.py  # global configs
│   ├── models.py  # global models
│   ├── schemas.py  # pydantic models
│   ├── exceptions.py  # global exceptions
│   ├── pagination.py  # global module e.g. pagination
│   ├── database.py  # db connection related stuff
│   └── main.py
├── .env
|-- .dockerignore
└── .gitignore

Example Base64 Image

import requests

url = "http://localhost:9090/ocr"

payload = {"image": "iVBORw0KGgoAAAANSUhEUgAAAFUAAAAhCAYAAACoRueNAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv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jZ3ebihvPxTU88ekOr2RwikYz5NmQDnt6qS5knqaXi6o6oUCKWKhc8eweuM//IfjKHhjjEfuU+0xvw5jdafIhUgvs2gT4crehqVDjPNqdRdwycjtuAW9M6STap2/uDsZMwhcoiXZ6ItOPLUwyU9vHjhxfnXyaL3U5W6T4gHYns72DTfMwhyDym6v2sYhVQfBfVLpLL4gFRaXxSu2AqVijPHqZQWDHs4w68CXK7mLBea0FgKidhsCOPHe8gJQrPNTXXOFqqotKTzFK5Y6qfkcsMlAVL7p0hNxGt/21BFEwUWIwanZmkKsWI1AmswSqkAYYTFLPKeHCoHL3uklJHj1wpNkVqzP92vJj7nr1TuAZXKP7lqHMaQWoPRau0g3LGttd8F/+XQmIUCazpLIfk15XBSp9hfI3WK/HiZh1ulTgTKvFTgFJ0dQ2rN/tqdwbFu/61Sa6ExCwWuo3OacjipU+yvkTpFfrxEzGb/AE4qfAH7P9o7AAAAAElFTkSuQmCC"}
headers = {
    "token": "USE_ENV_TOKEN",
    "Content-Type": "application/json"
}

response = requests.request("POST", url, json=payload, headers=headers)

print(response.text)