WebRTC Streaming
Stream video to an Inference server over WebRTC and receive live predictions, using a model ID or a Workflow, from webcams, RTSP cameras, video files, or manually sent frames.
pip install "inference-sdk[webrtc]"Stream a model
import cv2
import supervision as sv
from inference_sdk import InferenceHTTPClient
from inference_sdk.webrtc import WebcamSource
# Replace ROBOFLOW_API_KEY with your Roboflow API Key
client = InferenceHTTPClient(
api_url="http://localhost:9001",
api_key="ROBOFLOW_API_KEY",
)
session = client.webrtc.stream(
source=WebcamSource(),
model_id="rfdetr-nano",
)
box_annotator = sv.BoxAnnotator()
@session.on_frame
def show(frame, data):
# data is the raw predictions dict, exactly as returned by the server
# (None when predictions are unavailable for this frame)
if data is None:
return
detections = sv.Detections.from_inference(data)
annotated = box_annotator.annotate(frame.copy(), detections)
cv2.imshow("Preview", annotated)
if cv2.waitKey(1) & 0xFF == ord("q"):
session.close()
session.run() # blocks until the stream ends or session.close() is calledStream a Workflow
Video sources
WebcamSource
RTSPSource
LocalStreamSource
MJPEGSource
VideoFileSource
ManualSource
Consuming results
The session lifecycle
Receiving frames: on_frame and video()
Receiving data: on_data
Handling errors: on_error
Frame metadata
Attribute
Description
StreamConfig
Field
Default
Description
Runnable examples
Last updated
Was this helpful?