Setup OCR API Using FastAPI
Learn and practice often used Redis commands to store data in sets in the Redis data store.
Create a virtual environment for our API
It is a best practice to create a virtual environment whenever you create a new project. You can follow one of the two options below:
Using the Anaconda Prompt:
Start the Anaconda Prompt.
Run the following command:
conda create -n env_name python=3.7
You can specify any env_name
you like and the desired Python
version.
- Activate your virtual environment by running the following command:
conda activate env_name
- Your environment is now activated and you can install whichever packages you need in this virtual environment.
Using
venv
package:Start the Command Prompt.
If you do not have
venv
installed, you need to first install it by running the command shown below:
pip install venv
- Then, run the following command to create the virtual environment:
python3 -m venv path\env_name
You can specify any env_name
you like. You need to specify the path where you want to store the files for your environment. By default, it will take the current directory.
- Activate your virtual environment by running the following command:
path\env_name\Scripts\activate
- Your environment is now activated and you can install whichever packages you need in this virtual environment.
Create the default GET
route
The first step in creating our REST API
is to define a default (home) route that performs a GET
operation. Whenever anyone tries to go to the URL:
http://localhost:8000/
We will just give a JSON response as shown below:
{
"message" : "Visit the endpoint: /api/v1/extract_text to perform OCR."
}
This means that the actual OCR
operation is going to perform at the URL:
http://localhost:8000/api/v1/extract_text
This endpoint will accept a request body containing the multipart/form-data
, i.e., the images.
Note that we are going to pass multiple images in a single call, and our
API
should give the response in the form shown below:
{
"image_name": "extracted_text"
}
Let’s first create the default GET
route.
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def home():
return {"message": "Visit the endpoint: /api/v1/extract_text to perform OCR."}
We are returning a response if anyone tries to access the route /
then the API is going to provide this message to the user.
Create the read_image()
function
Now that our GET
route is done, we should create a function that will accept the image path and the language, and then perform the OCR
operation.
We will be uploading the images on the server and then taking that image location (on the server) to perform
OCR
.
Let’s create the function and name it read_image()
.
import pytesseract
import asyncio
async def read_image(img_path, lang='eng'):
try:
text = pytesseract.image_to_string(img_path, lang=lang)
await asyncio.sleep(2)
return text
except:
return "[ERROR] Unable to process file: {0}".format(img_path)
Explanation
We defined an
async
functionread_image()
that accepts the image path (which is uploaded on the server) and the language of the text present in the images.We created a
try-except
block because if any error occurs, we wanted to handle those things in ourAPI
as well.We performed the
OCR
using the same statement that was used in the previous article in the series.We paused the execution for just
2
seconds. At this point, your complex IO operations can also take place. This is the point where the coroutine switching will take place.Finally, we returned our extracted text.
We also return any error that might have occurred during the extraction of text.
We will continue the next steps in our next article in this series.
This series is just a snapshot of the Build a REST API Using Python and Deploy it to Microsoft Azure course which covers a lot more things like FastAPI, Microsoft Azure, Deploying FastAPI applications to Azure, Monitoring the applications using Azure, and more projects. Do check it out and let me know if you have any questions.