Setup OCR API Using FastAPI
Learn and practice often used Redis commands to store data in sets in the Redis data store.

Hi, I'm Harsh, a software developer at Springworks, and an Ex-TCSer.
I am a coding instructor and mentor and have been creating multiple online courses to get people comfortable learning how to code and help them get better opportunities.
I have been coding since I was 15 when I created a static website for a school project. I was given positive feedback on this project, which pushed me to major in computer science with a specialization in Artificial Intelligence.
For me, "The day is not over if I have not done any coding. I usually try to solve Competitive Programming problems, which helps me to improve my problem-solving skills. Every day I try to learn something new."
As a Software Developer for Tata Consultancy Services Limited, I have built scalable backend services using Node.js and Microsoft Azure. Apart from this, I am also an author of 6 courses at Educative.io and have been building courses on the latest technologies.
I’m familiar with various programming languages, including JavaScript, Python, and a bunch of other technical areas like System Design, Databases. I’m always adding new skills to my repertoire.
I've been Microsoft Certified in Azure Fundamentals and Azure AI Fundamentals. I am also now a Microsoft Certified Azure AI Engineer Associate.
I have delivered over 20 one-on-one sessions. If you want to talk more about coding, interview preparation, software development, or just want any career guidance, especially from the technical domain, hit me up or just connect with me at: topmate.io/harsh_jain
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
venvpackage:Start the Command Prompt.
If you do not have
venvinstalled, 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
APIshould 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
asyncfunctionread_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-exceptblock because if any error occurs, we wanted to handle those things in ourAPIas well.We performed the
OCRusing the same statement that was used in the previous article in the series.We paused the execution for just
2seconds. 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.



