|
1 |
| -# Flask REST API |
| 1 | +<a href="https://www.ultralytics.com/"><img src="https://raw.githubusercontent.com/ultralytics/assets/main/logo/Ultralytics_Logotype_Original.svg" width="320" alt="Ultralytics logo"></a> |
2 | 2 |
|
3 |
| -[REST](https://en.wikipedia.org/wiki/Representational_state_transfer) [API](https://en.wikipedia.org/wiki/API)s are commonly used to expose Machine Learning (ML) models to other services. This folder contains an example REST API created using Flask to expose the YOLOv5s model from [PyTorch Hub](https://pytorch.org/hub/ultralytics_yolov5/). |
| 3 | +# Flask REST API Example for YOLO Models |
4 | 4 |
|
5 |
| -## Requirements |
| 5 | +[Representational State Transfer (REST)](https://en.wikipedia.org/wiki/Representational_state_transfer) [Application Programming Interfaces (APIs)](https://developer.mozilla.org/en-US/docs/Web/API) are a standard way to expose [Machine Learning (ML)](https://www.ultralytics.com/glossary/machine-learning-ml) models, allowing other services or applications to interact with them over a network. This directory provides an example REST API built using the [Flask](https://palletsprojects.com/projects/flask/) microframework to serve predictions from an [Ultralytics YOLOv5s](https://docs.ultralytics.com/models/yolov5/) model loaded via [PyTorch Hub](https://pytorch.org/hub/ultralytics_yolov5/). |
6 | 6 |
|
7 |
| -[Flask](https://palletsprojects.com/projects/flask/) is required. Install with: |
| 7 | +Deploying models via APIs is a crucial step in [MLOps](https://www.ultralytics.com/glossary/machine-learning-operations-mlops) and enables integration into larger systems. You can explore various [model deployment options](https://docs.ultralytics.com/guides/model-deployment-options/) for different scenarios. |
| 8 | + |
| 9 | +## 🔧 Requirements |
| 10 | + |
| 11 | +Ensure you have the necessary Python packages installed. The primary requirement is Flask. |
| 12 | + |
| 13 | +Install Flask using pip: |
8 | 14 |
|
9 | 15 | ```shell
|
10 |
| -$ pip install Flask |
| 16 | +pip install Flask torch torchvision |
11 | 17 | ```
|
12 | 18 |
|
13 |
| -## Run |
| 19 | +_Note: `torch` and `torchvision` are required by the YOLOv5 model from PyTorch Hub._ |
| 20 | + |
| 21 | +## ▶️ Run the API |
14 | 22 |
|
15 |
| -After Flask installation run: |
| 23 | +Once Flask and dependencies are installed, you can start the API server. |
| 24 | + |
| 25 | +Execute the Python script: |
16 | 26 |
|
17 | 27 | ```shell
|
18 |
| -$ python3 restapi.py --port 5000 |
| 28 | +python restapi.py --port 5000 |
19 | 29 | ```
|
20 | 30 |
|
21 |
| -Then use [curl](https://curl.se/) to perform a request: |
| 31 | +The API server will start listening on the specified port (default is 5000). |
| 32 | + |
| 33 | +## 🚀 Make a Prediction Request |
| 34 | + |
| 35 | +You can send prediction requests to the running API using tools like [`curl`](https://curl.se/) or scripting languages. |
| 36 | + |
| 37 | +Send a POST request with an image file (`zidane.jpg` in this example) to the `/v1/object-detection/yolov5s` endpoint: |
22 | 38 |
|
23 | 39 | ```shell
|
24 |
| -$ curl -X POST -F [email protected] 'http://localhost:5000/v1/object-detection/yolov5s' |
| 40 | +curl -X POST -F [email protected] 'http://localhost:5000/v1/object-detection/yolov5s' |
25 | 41 | ```
|
26 | 42 |
|
27 |
| -The model inference results are returned as a JSON response: |
| 43 | +_Ensure `zidane.jpg` (or your test image) is present in the directory where you run the `curl` command._ |
| 44 | + |
| 45 | +## 📄 Understand the Response |
| 46 | + |
| 47 | +The API processes the image and returns the [object detection](https://www.ultralytics.com/glossary/object-detection) results in [JSON](https://www.ultralytics.com/glossary/json) format. Each object detected includes its class ID, confidence score, bounding box coordinates (normalized), and class name. |
| 48 | + |
| 49 | +Example JSON response: |
28 | 50 |
|
29 | 51 | ```json
|
30 | 52 | [
|
@@ -67,4 +89,8 @@ The model inference results are returned as a JSON response:
|
67 | 89 | ]
|
68 | 90 | ```
|
69 | 91 |
|
70 |
| -An example python script to perform inference using [requests](https://docs.python-requests.org/en/master/) is given in `example_request.py` |
| 92 | +An example Python script (`example_request.py`) demonstrating how to send requests using the popular [requests](https://requests.readthedocs.io/en/latest/) library is also included in this directory. |
| 93 | + |
| 94 | +## 🤝 Contributing |
| 95 | + |
| 96 | +Contributions to enhance this example or add support for other Ultralytics models are welcome! Please see the main Ultralytics [CONTRIBUTING](https://docs.ultralytics.com/help/contributing/) guide for more information on how to get involved. |
0 commit comments