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

Leave a Reply

35 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@MrRahul15937
2 hours ago

Thank you very much sir

@arturkabitcher
2 hours ago

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

@kolosafogaming1502
2 hours ago

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

@tekefsane127
2 hours ago

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

@perschonca
2 hours ago

This code has Tegridy

@ninolindenberg9726
2 hours ago

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

@fahrenheit2109
2 hours ago

gem 💎

@ffgaming-fe3cx
2 hours ago

liked subscribed shared

@fangshengxu5462
2 hours ago

You are a F***ing legend!

@bumblebee2012able
2 hours 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
2 hours ago

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

@codihuntsinger3698
2 hours 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
2 hours ago

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

@goodwish1543
2 hours ago

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

@goodwish1543
2 hours ago

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

@sajibmondol862
2 hours ago

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

@FahadAli-ot5kn
2 hours ago

Killer video

@skyblaze6687
2 hours ago

U should rename it cause it not django
Its pure python

@Joseph20077
2 hours ago

Great content

@timthegreatone
2 hours ago

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

35
0
Would love your thoughts, please comment.x
()
x