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!
Thank you very much sir
Man, that's awesome! Very helpful for those who do not know how their GitHub repos should look like
The main purpose of this tutorial could have taken 45mins – 1hr to fulfil. A lot of irrelevant things damn!
The github and pre-commit section of this was not needed damn
This code has Tegridy
Sorry, but where is the example of the microservice architecture?
gem 💎
liked subscribed shared
You are a F***ing legend!
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
I wonder why your videos are getting this less number of views. Your content is exceptionally awesome.
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.
Yes, we'd like to have more FastAPI general web application talk.
How to test the HTML response text? with the whole string ?
Wonderful! Thanks for covering all the topic in the suitable complex level for me. ^_^
Great tutorial, thanks!!!
But build time in digital ocean is sucks, simple app tooks 25 minute LOL
Killer video
U should rename it cause it not django
Its pure python
Great content
Justin is really angry. My PC has been feeling the heat for days.