This is a BentoML example project that demonstrates how to serve an OCR model. It accepts images as input and returns the text contained within. While the example uses EasyOCR, you can choose any other OCR model.
See here for a full list of BentoML example projects.
-
Make sure to install uv.
-
Clone the repo and install dependencies.
git clone https://github.com/bentoml/BentoOCR.git && cd BentoOCR # Recommend Python 3.11 pip install -r requirements.txt
Import the model into the BentoML Model Store.
python import_model.py
We have defined a BentoML Service in service.py
to serve the model. To start it, run:
bentoml serve
The server is now active at http://localhost:3000. It exposes two API endpoints:
detect
: Takes an image as input and returns a list of detected text regions. Each detection includes the extracted text and the bounding box coordinates of where the text was found in the image.classify
: Takes an image as input and returns a list with the extracted text and the confidence score for each text detection.
You can call these endpoints using the Swagger UI or in other different ways.
curl -X 'POST' \
'http://localhost:3000/detect' \
-H 'accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-F 'image=@sample-image.png;type=image/png'
import bentoml
from pathlib import Path
with bentoml.SyncHTTPClient("http://localhost:3000/") as client:
result = client.detect(
image=Path("image.jpg"),
)
After the Service is ready, you can deploy the application to BentoCloud for better management and scalability. Sign up if you haven't got a BentoCloud account.
Make sure you have logged in to BentoCloud.
bentoml cloud login
Deploy it to BentoCloud:
bentoml deploy
Once the application is up and running on BentoCloud, you can access it via the exposed URL.
BentoML has a thriving open source community where thousands of ML/AI practitioners are contributing to the project, helping other users and discussing the future of AI. π Pop into our Slack community!