This project contains the implementation of REST API empowered by Vision AI to detect the text available in the input image. The REST API services are being distributed through the docker image. You can pull the image from Docker Hub. The image comes as production-ready with unit tests and a standard algorithm implemented which can be used to expose the REST API to the web.
Following are the major contents to follow, you can jump to any section:
-
Ensure you have Python 3.7+ installed.
-
Create a new Python Conda environment:
conda create -n ocr python=3.10 # create ocr
conda activate ocr # activate ocr
OR
- Create a new Python virtual environment with pip:
virtualenv ocr --python=python3.10 # create ocr
source ocr/bin/activate # activate ocr
Install dependencies
pip install -r requirements.txt
or
conda env create -f environment.yaml
Clone the project
git clone https://github.com/Hassi34/Optical-Character-Recognition.git
Go to the project directory
cd Optical-Character-Recognition
Install dependencies
pip install -r requirements.txt
Start the server
uvicorn main:app --reload
To run the following sequence of commands, make sure you have the docker installed on your system.
In case you have not already pulled the image from the Docker Hub, you can use the following command:
docker pull hassi34/optical-character-recognition
Now once you have the docker image from the Docker Hub, you can now run the following commands to test and deploy the container to the web
docker images
Use the following command to run a docker container on your system:
docker run --name <CONTAINER NAME> -p 80:80 -d <IMAGE NAME OR ID>
Check if the container is running:
docker ps
If the container is running, then the API services will be available on all the network interfaces
Type localhost
in the browser and see if you get the success message from the API service.
- Perform Unit Tests
After when the API services are up and running, run the following command on the terminal to perform the unit test:
docker exec -it <CONTAINER NAME OR ID> pytest
- API documentation
The automatic API documentation will be available at http://localhost/docs
Use the following script as a reference to make a REST API request:
import requests
import base64
BASE64_STR = ""
ENDPOINT = "http://127.0.0.1/predict"
def encodeImageIntoBase64(IN_IMG_PATH):
with open(IN_IMG_PATH, "rb") as f:
return base64.b64encode(f.read())
if __name__ == '__main__':
response = requests.post(ENDPOINT, json={"base64_str":BASE64_STR})
if response.status_code == 200:
response = response.json()
print(response)
else :
print(response)
Copyright ยฉ 2023 Hasanain
Let's connect on LinkedIn