yolov5/utils/flask_rest_api
Glenn Jocher 1953d47255
Update YOLOv5 READMEs (#13554)
* Update YOLOv5 READMEs

* Update YOLOv5 READMEs

* Auto-format by https://ultralytics.com/actions

---------

Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
2025-03-28 21:58:22 +01:00
..
README.md Update YOLOv5 READMEs (#13554) 2025-03-28 21:58:22 +01:00
example_request.py Standardize license headers in Python files (#13490) 2025-01-15 15:31:19 +01:00
restapi.py Standardize license headers in Python files (#13490) 2025-01-15 15:31:19 +01:00

README.md

Ultralytics logo

Flask REST API for YOLOv5

Representational State Transfer (REST) Application Programming Interfaces (APIs) provide a standardized way to expose Machine Learning (ML) models for use by other services or applications. This directory contains an example REST API built with the Flask web framework to serve the Ultralytics YOLOv5s model, loaded directly from PyTorch Hub. This setup allows you to easily integrate YOLOv5 object detection capabilities into your web applications or microservices, aligning with common model deployment options.

💻 Requirements

The primary requirement is the Flask web framework. You can install it using pip:

pip install Flask

You will also need torch and yolov5. These are implicitly handled by the script when it loads the model from PyTorch Hub. Ensure you have a functioning Python environment set up.

▶️ Run the API

Once Flask is installed, you can start the API server using the following command:

python restapi.py --port 5000

The server will begin listening on the specified port (defaulting to 5000). You can then send inference requests to the API endpoint using tools like curl or any other HTTP client.

To test the API with a local image file (e.g., zidane.jpg located in the yolov5/data/images directory relative to the script):

curl -X POST -F image=@../data/images/zidane.jpg 'http://localhost:5000/v1/object-detection/yolov5s'

The API processes the submitted image using the YOLOv5s model and returns the detection results in JSON format. Each object within the JSON array represents a detected item, including its class ID, confidence score, normalized bounding box coordinates (xcenter, ycenter, width, height), and class name.

[
  {
    "class": 0,
    "confidence": 0.8900438547,
    "height": 0.9318675399,
    "name": "person",
    "width": 0.3264600933,
    "xcenter": 0.7438579798,
    "ycenter": 0.5207948685
  },
  {
    "class": 0,
    "confidence": 0.8440024257,
    "height": 0.7155083418,
    "name": "person",
    "width": 0.6546785235,
    "xcenter": 0.427829951,
    "ycenter": 0.6334488392
  },
  {
    "class": 27,
    "confidence": 0.3771208823,
    "height": 0.3902671337,
    "name": "tie",
    "width": 0.0696444362,
    "xcenter": 0.3675483763,
    "ycenter": 0.7991207838
  },
  {
    "class": 27,
    "confidence": 0.3527112305,
    "height": 0.1540903747,
    "name": "tie",
    "width": 0.0336618312,
    "xcenter": 0.7814827561,
    "ycenter": 0.5065554976
  }
]

An example Python script, example_request.py, is included to demonstrate how to perform inference using the popular requests library. This script offers a straightforward method for interacting with the running API programmatically.

🤝 Contribute

Contributions to enhance this Flask API example are highly encouraged! Whether you're interested in adding support for different YOLO models, improving error handling, or implementing new features, please feel free to fork the repository, apply your changes, and submit a pull request. For more comprehensive contribution guidelines, please refer to the main Ultralytics YOLOv5 repository and the general Ultralytics documentation.