Web inference-sdk
Run realtime video inference from your browser, running on the Roboflow cloud, with inference-sdk
Last updated
Was this helpful?
Was this helpful?
import { connectors, webrtc, streams } from '@roboflow/inference-sdk';
// ⚠️ Use withApiKey for development only
// ⚠️ Do not use this in production, because it will expose your API key
// For production, use a backend proxy (see next section)
const connector = connectors.withApiKey("your-api-key");
// Get camera stream
const stream = await streams.useCamera({
video: {
facingMode: { ideal: "environment" },
width: { ideal: 640 },
height: { ideal: 480 }
}
});
// Start WebRTC connection
const connection = await webrtc.useStream({
source: stream,
connector,
wrtcParams: {
workspaceName: "your-workspace",
workflowId: "your-workflow",
imageInputName: "image",
streamOutputNames: ["output"],
dataOutputNames: ["predictions"]
},
onData: (data) => {
console.log("Inference results:", data);
}
});
// Display processed video
const videoElement = document.getElementById('video');
videoElement.srcObject = await connection.remoteStream();
// Clean up when done
await connection.cleanup();import { connectors, webrtc, streams } from '@roboflow/inference-sdk';
// Use proxy endpoint instead of direct API key
const connector = connectors.withProxyUrl('/api/init-webrtc');
const stream = await streams.useCamera({ video: true });
const connection = await webrtc.useStream({
source: stream,
connector,
wrtcParams: { /* ... */ }
});import { InferenceHTTPClient } from '@roboflow/inference-sdk/api';
app.post('/api/init-webrtc', async (req, res) => {
const { offer, wrtcparams } = req.body;
// API key stays secure on the server
const client = InferenceHTTPClient.init({
apiKey: process.env.ROBOFLOW_API_KEY
});
const answer = await client.initializeWebrtcWorker({
offer,
workspaceName: wrtcparams.workspaceName,
workflowId: wrtcparams.workflowId,
config: {
imageInputName: wrtcparams.imageInputName,
streamOutputNames: wrtcparams.streamOutputNames,
dataOutputNames: wrtcparams.dataOutputNames
}
});
res.json(answer);
});// Switch to different visualization
connection.reconfigureOutputs({
streamOutput: ["blur_visualization"]
});
// Enable all data outputs
connection.reconfigureOutputs({
dataOutput: ["*"]
});
// Change both at once
connection.reconfigureOutputs({
streamOutput: ["annotated_image"],
dataOutput: ["predictions", "counts"]
});