This repository (and Docker image) provides a Flask-based web service for performing object detection using Ultralytics YOLO. The service runs a single YOLO model “hot” in a background worker thread, handling multiple HTTP endpoints to accept image data in different formats.
A prebuilt Docker image is available at:
ghcr.io/aughey/yolo_flask_service:latest
To run the service as a Docker container:
docker run --rm -it -p 5000:5000 ghcr.io/aughey/yolo_flask_service:latestThis will start the Flask service on port 5000. You can then make requests to the various endpoints (described below).
docker run --rm -it --entrypoint python -v /c/folder:/folder ghcr.io/aughey/yolo_flask_service:latest videoprocessor.py --video /folder/video.mov --output /folder/video_skip10.json --weights yolo12m.pt --skip_frames 10- Purpose: Returns metadata describing the raw format expected by the
/predict_rawendpoint. - Response: JSON containing
color_order,height,width,channels,dtype, etc.
Example (assuming the container is running locally on port 5000):
curl http://localhost:5000/infoSample JSON Response:
{
"color_order": "BGR",
"height": 640,
"width": 640,
"channels": 3,
"dtype": "float32",
"pixel_range": "[0..1]",
"layout": "row-major (H x W x C)",
"note": "Send exactly 640 x 640 x 3 BGR float32 pixels..."
}- Purpose: Accepts raw bytes in the format described by
/info(e.g., a shape of640×640×3,float32, BGR). - Behavior:
- No decoding or color conversions occur.
- The data is sent directly to the YOLO model.
- Request Body: The raw bytes of a NumPy array in the exact shape and datatype returned by
/info.
import requests
import numpy as np
# Suppose you have a BGR float32 array of shape (640,640,3) in [0..1]
fake_image = np.random.rand(640,640,3).astype("float32")
resp = requests.post(
"http://localhost:5000/predict_raw",
data=fake_image.tobytes()
)
print(resp.status_code, resp.json())- Purpose: Accepts encoded image data (PNG/JPEG) in the request body.
- Behavior:
- The service decodes the image with OpenCV (
cv2) into BGR format. - This is the standard approach if you have, for example, a PNG or JPEG file.
- The service decodes the image with OpenCV (
curl -X POST "http://localhost:5000/predict" \
-H "Content-Type: image/png" \
--data-binary "@/path/to/local_image.png"Sample Response (JSON):
{
"boxes": [
{
"x1": 77.11,
"y1": 52.22,
"x2": 100.89,
"y2": 120.01,
"confidence": 0.891,
"class_id": 0,
"class_name": "person"
},
...
]
}- Purpose: Fetches the image from a remote URL, decodes it, and performs YOLO inference.
- Query Param:
?url=<IMAGE_URL> - Usage:
curl "http://localhost:5000/predict_url?url=https://ultralytics.com/images/bus.jpg"-
YOLO Model
The script loads a single YOLO model (YOLO("yolo11n.pt")by default) at startup. This model remains in memory (the “hot” model) and is shared by all requests. -
Worker Thread
A dedicated worker thread (worker_thread()) continuously monitors a queue (prediction_queue) for incoming tasks. Each task has atask_idand a NumPy array representing the image.- The worker runs
model.predict(...)and stores results in a globalresults_dict[task_id]. - The Flask endpoints enqueue tasks, then wait (
queue.join()) until they’re processed.
- The worker runs
-
Endpoints
/info: Describes the raw format for/predict_raw./predict_raw: Expects raw NumPy bytes (no decoding)./predict: Standard PNG/JPEG decode using OpenCV./predict_url: Fetches an image from a remote URL for inference.
-
Color Space
By default,IDEAL_COLOR_SPACEis “BGR” to align with typical OpenCV usage. If your model was trained on RGB images (without conversion), you can setIDEAL_COLOR_SPACE = "RGB"and adjust your inference pipeline accordingly. -
Supported Python Packages
Flaskfor the web serverultralyticsfor YOLOnumpyandopencv-python(cv2) for image handlingrequeststo fetch images by URL
-
Pull the Docker Image:
docker pull ghcr.io/aughey/yolo_flask_service:latest
-
Run the Container:
docker run -p 5000:5000 ghcr.io/aughey/yolo_flask_service:latest
The service starts on port
5000. -
Check the Raw Input Info:
curl http://localhost:5000/info
-
Predict from a Remote URL:
curl "http://localhost:5000/predict_url?url=https://ultralytics.com/images/bus.jpg" -
Predict from Raw Data (Python snippet):
import requests import numpy as np # Create random test data data = np.random.rand(640, 640, 3).astype("float32") r = requests.post("http://localhost:5000/predict_raw", data=data.tobytes()) print(r.status_code, r.json())
-
Predict from a Local PNG/JPEG:
curl -X POST "http://localhost:5000/predict" \ -H "Content-Type: image/png" \ --data-binary "@/path/to/local_image.png"
- Memory Constraints: Large images or many concurrent requests can impact memory usage.
- Thread Safety: Currently, the script uses one YOLO model instance in one worker thread. This handles requests sequentially. If you need parallel inference, you can launch multiple containers or manage multiple model instances.
- Timeouts: The example uses
requests.get(image_url, timeout=10)in/predict_url. Adjust as needed. - Production Readiness: This is a reference design. For production, consider rate limiting, request size limits, and more robust error handling.
- Ultralytics YOLO is licensed under the GPL-3.0 license.
- The Docker image is provided as-is, and you should ensure compliance with all respective licenses in your production environment.