A FastAPI-based simple web service that accepts images, runs optical character recognition (OCR) on them and returns the extracted text.
Before you begin, make sure you have the following prerequisites installed:
- Python 3.10
- Pip (Python package installer)
- Docker
- fastapi
- uvicorn
- python-dotenv
- pyyaml
- python-multipart
- pytesseract
- Pillow
-
Clone the repository:
git clone https://github.com/99sbr/OCR-Service-Restapi.git
-
Navigate to the project directory:
cd OCR-Service-Restapi
To run the web service in a Docker container:
- Build the Docker image:
docker build -t ocrservice-image .
- Run the Docker container:
docker run -p 80:80 ocrservice-image
Visit http://localhost:80/docs
to access the Swagger documentation and interact with the API.
- POST /imgsync
- Description: Synchronously extracts text from an image.
- Request Body: Provide a JSON object with the base64-encoded image data.
- Response: Returns a JSON object with the extracted text.
- POST /imgasync
- Description: Asynchronously extracts text from an image.
- Request Body: Provide a JSON object with the base64-encoded image data.
- Response: Returns a JSON object with a job_id for tracking the asynchronous task.
- GET /ocr_text
- Description: Asynchronously gets OCR text from Job ID.
- Request Body: Provide a JSON object with the job-ID data.
- Response: Returns a JSON object with the extracted text.
-
api_template:
Contains all the API-related Code Base.manage.py:
Only entry point for API. Contains no logic..env:
Most important file for our API and contains global configs. Avoid using application/variable level configs here.application:
It contains all our API-related codes and test modules. I prefer keeping the application folder global.logs
: Logs are self-explanatory. FYI it will not contain any configuration information, just raw logs. Feel free to move according to your comfort but not inside the application folder.settings:
Logger/DataBase/Model global settings files in yaml/json format.
-
application:
main:
priority folder of all your application-related code.๐ฎ routers:
API routers and they strictly do not contain any business logic๐ก services:
All processing and business logic for routers here at the service layerโ utility:
logger
Logging module for applicationmanager
A manager utility contains workers and handlers for Data Related Tasks which can be common for different services.
initializer.py:
Preload/Initialisation of Models and Module common across applications. The preloading model improves inferencing.