This is a demo FastAPI app that allows a user to upload image(s), perform inference using a pretrained YOLOv5 model, and receive results in JSON format. This repo also includes Jinja2 HTML templates, so you can access this interface through a web browser at localhost:8000
Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7 (per https://github.com/ultralytics/yolov5).
To install run:
pip install -r requirements.txt
You can initialize the server with python server.py
or uvicorn server:app --reload
You can test the server a couple of ways:
- Using
client.py
- this is a basic example of using the Requests library to upload a batch of images + model name tolocalhost:8000/detect/
and receive JSON inference results. - Open
localhost:8000
in your web browser, use the web form to upload image(s) and select a model, then click submit. You should see inference results displayed in the web browser shortly. - Open
localhost:8000/docs
in your web browser, clicking on "POST /detect/ Detect Via API" and then click the "Try It Out" button. You should be able to upload files and enter a YOLOv5 model here as well.
Models will automatically be downloaded the first time they are used and are cached on disc.
Full Swagger API endpoint documentation is auto-generated in localhost:8000/docs
. The general idea is that humans use the "/" route (HTML form + inference results displayed in the browser) and programs use the "/detect/" API route to receive JSON inference results.
See the fastapi_yolov5_minimal_client_server_example
folder for a minimal client/server wrapper of YOLOv5 with FastAPI and HTML forms.
Again, you can try this out by:
- Running the server with
python server_minimal.py
oruvicorn server_minimal:app --reload
- Test the server with
python client_minimal.py
, navigating tolocalhost:8000
in your web browser orlocalhost:8000/docs -> POST Request -> Try It Out
This repository is a wrapper around YOLOv5 from Ultralytics: https://github.com/ultralytics/yolov5
Also grabbed some code/ideas from: https://gist.github.com/decent-engineer-decent-datascientist/81e04ad86e102eb083416e28150aa2a1