How to broadcast predictions from YOLO-NAS Over MQTT

You can broadcast predictions from

YOLO-NAS Over MQTT

. This is useful for sending the results of a model to a central system for further processing or logging.

In this guide, we will demonstrate how to broadcast predictions from

YOLO-NAS Over MQTT

.

‍In this guide, we will demonstrate how to broadcast predictions fromWe will:

1. Install supervision
2. Load data
3. Plot predictions with a supervision Annotator

Without further ado, let's get started!

Step #1: Install supervision

First, install the supervision pip package:

pip install supervision


Once you have installed supervision, you are ready to load your data and start writing logic to filter detections.

Step #2: Load Model and Send Data

For this guide, you will need to have a broker set up that can accept predictions. Once you have a broker, you can stream predictions from your model that is running on a video, camera feed, or RTSP stream feed.



You can load the model and send data using the following code:


import json

import paho.mqtt.client as mqtt
from inference import InferencePipeline
from inference.core.interfaces.stream.sinks import render_boxes

client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1)

client.connect(
    host="localhost", # update with the URL of your MQTT broker
    port=1883, # update with the port over which your broker runs
)


def on_prediction(predictions, video_frame):
    render_boxes(predictions=predictions, video_frame=video_frame)
    predictions = json.dumps(predictions)
    client.publish(
        topic="bottle-cap-integrity", payload=predictions, qos=0, retain=False
    )


pipeline = InferencePipeline.init(
    model_id="bottle-cap-integrity/7", # add your Roboflow YOLO-NAS model ID here
    video_reference=0,
    on_prediction=on_prediction,
    confidence=0.3,
)

pipeline.start()
pipeline.join()


Once you make the necessary substitutions above and run the script, you should see data being received by your broker for any further processing you implement.

Next steps

supervision provides an extensive range of functionalities for working with computer vision models. With supervision, you can:

1. Process and filter detections and segmentation masks from a range of popular models (YOLOv5, Ultralytics YOLOv8, MMDetection, and more).
2. Process and filter classifications.
3. Compute confusion matrices.

And more! To learn about the full range of functionality in supervision, check out the supervision documentation.