Tutorial: Building an AI Microservice with Python & FastAPI to Extract Text from Images

Posted by


In this tutorial, we will learn how to create an AI microservice using Python and FastAPI to extract text from images. We will use the Tesseract OCR library to perform optical character recognition on images and extract text from them. FastAPI will allow us to create a web API for our microservice.

To get started, make sure you have Python installed on your system. You can download and install Python from the official website.

Next, let’s create a new Python virtual environment for our project. Open a terminal and run the following command:

$ python -m venv myenv

Activate the virtual environment by running the following command:

$ source myenv/bin/activate

Now, install FastAPI and other required libraries by running the following command:

$ pip install fastapi uvicorn python-multipart pytesseract Pillow

Next, we need to install Tesseract OCR on our system. You can download and install Tesseract OCR from the official website. Make sure to add the installation directory to the PATH environment variable.

Now, let’s create a new Python file named app.py and add the following code:

from fastapi import FastAPI, File, UploadFile
from PIL import Image
import pytesseract
from io import BytesIO
from starlette.responses import JSONResponse

app = FastAPI()

@app.post("/extract_text_from_image/")
async def extract_text_from_image(file: UploadFile = File(...)):
    img = Image.open(BytesIO(await file.read()))
    text = pytesseract.image_to_string(img)
    return JSONResponse(content={"text": text})

In this code, we have defined a FastAPI application with a single POST endpoint /extract_text_from_image/. This endpoint accepts an image file as input, processes it using Tesseract OCR, and returns the extracted text in JSON format.

Now, let’s run our FastAPI application using the following command:

$ uvicorn app:app --reload

You should see a message indicating that the FastAPI application is running on http://127.0.0.1:8000.

To test our AI microservice, you can use tools like Postman or curl to send a POST request with an image file. For example, you can use the following curl command to test the microservice:

$ curl -X POST http://127.0.0.1:8000/extract_text_from_image/ -F "file=@/path/to/image.jpg"

Replace /path/to/image.jpg with the path to the image file you want to process. You should receive a JSON response containing the extracted text from the image.

In this tutorial, we have learned how to create an AI microservice using Python and FastAPI to extract text from images. We used the Tesseract OCR library for optical character recognition and FastAPI to create a web API for the microservice. Feel free to customize and expand upon this tutorial to suit your needs. Happy coding!

0 0 votes
Article Rating
35 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@MrRahul15937
1 month ago

Thank you very much sir

@arturkabitcher
1 month ago

Man, that's awesome! Very helpful for those who do not know how their GitHub repos should look like

@kolosafogaming1502
1 month ago

The main purpose of this tutorial could have taken 45mins – 1hr to fulfil. A lot of irrelevant things damn!

@tekefsane127
1 month ago

The github and pre-commit section of this was not needed damn

@perschonca
1 month ago

This code has Tegridy

@ninolindenberg9726
1 month ago

Sorry, but where is the example of the microservice architecture?

@fahrenheit2109
1 month ago

gem 💎

@ffgaming-fe3cx
1 month ago

liked subscribed shared

@fangshengxu5462
1 month ago

You are a F***ing legend!

@bumblebee2012able
1 month ago

Hello CodingEntrepreneurs, Do you have any course related to building real world Microservices using Python web framework (such as Django/FastAPI) in Udemy? If yes, please let me know, thanks

@momodoubjallow2585
1 month ago

I wonder why your videos are getting this less number of views. Your content is exceptionally awesome.

@codihuntsinger3698
1 month ago

Dude, what'r hell a u doing? Virtual env should be separated from source code in own directory, not in root of app.
Then just add "venv" to your .gitignore file. U teach a bad practices, and all newbies just follow ur steps, even don't realize that.
And it's just a one peace of bad practices u've shown.

@goodwish1543
1 month ago

Yes, we'd like to have more FastAPI general web application talk.

@goodwish1543
1 month ago

How to test the HTML response text? with the whole string ?

@goodwish1543
1 month ago

Wonderful! Thanks for covering all the topic in the suitable complex level for me. ^_^

@sajibmondol862
1 month ago

Great tutorial, thanks!!!
But build time in digital ocean is sucks, simple app tooks 25 minute LOL

@FahadAli-ot5kn
1 month ago

Killer video

@skyblaze6687
1 month ago

U should rename it cause it not django
Its pure python

@Joseph20077
1 month ago

Great content

@timthegreatone
1 month ago

Justin is really angry. My PC has been feeling the heat for days.