/web-to-ai-ml-integrations

Stream Audio or Video to ML Model from Web App

Stream audio/video to an ML model from your web app using our step-by-step guide for seamless integration and optimal performance.

Book a free  consultation
4.9
Clutch rating 🌟
600+
Happy partners
17+
Countries served
190+
Team members
Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

Stream Audio or Video to ML Model from Web App

Capturing Media in the Browser and Preparing It for Streaming

 
  • Utilize the browser’s MediaDevices.getUserMedia() API to capture audio or video. This API provides access to the device’s camera and microphone and returns a stream that can be processed in real time.
  • Depending on your requirements, you might capture raw frames for video or audio chunks if processing sound. Each captured segment can later be packaged and sent over a persistent connection.
  • Here is an example of capturing video from the user and converting it into data chunks ready for transmission:

// Access the user's camera
navigator.mediaDevices.getUserMedia({ video: true, audio: true })
  .then(function(stream) {
    // Display the stream locally
    const videoElement = document.querySelector('video');
    videoElement.srcObject = stream;

    // Create a MediaRecorder to capture stream chunks
    const recorder = new MediaRecorder(stream);

    // Send each recorded chunk to server via WebSocket when available
    recorder.ondataavailable = function(event) {
      if (event.data && event.data.size > 0) {
        // Send binary data (blob) over an established WebSocket connection
        websocket.send(event.data);
      }
    };

    // Start recording with a timeslice of 1000 ms
    recorder.start(1000);
  })
  .catch(function(error) {
    console.error('Error accessing media devices.', error);
  });

 

Establishing a Reliable Communication Channel Using WebSockets

 
  • Implement WebSockets to maintain a persistent, low-latency connection between the web application and the backend. This technology is ideal for streaming as it supports real-time, bidirectional data transfer.
  • The client (browser) establishes a connection and sends recorded data, while the backend receives and queues the incoming chunks for processing.
  • Below is a sample implementation of a simple WebSocket client. It demonstrates connection setup and binary data transmission:

// Create a new WebSocket connection
const websocket = new WebSocket('ws://localhost:8000/stream');

// Log successful connection
websocket.onopen = function(event) {
  console.log('WebSocket connection established.');
};

// Handle errors on the client side
websocket.onerror = function(error) {
  console.error('WebSocket error: ', error);
};

// Optionally handle messages from the server (e.g., for acknowledgments)
websocket.onmessage = function(event) {
  console.log('Message from server: ', event.data);
};

 

Receiving Streaming Data and Connecting to an ML Model

 
  • Create a server that receives data over WebSocket connections. Languages like Python with libraries such as websockets, Flask-SocketIO, or FastAPI serve well for this.
  • The server should buffer incoming data and, if dealing with video, extract frames at a desired rate for ML inference. For audio, you can accumulate samples for feature extraction (e.g., MFCCs).
  • Load your pre-trained machine learning model using frameworks like TensorFlow, PyTorch, or ONNX. The loaded model should be capable of processing the input data format.
  • Decide on a strategy: either process every incoming chunk as a standalone inference or aggregate several chunks to improve prediction quality.

import asyncio
import websockets
import io
from PIL import Image
import numpy as np
import tensorflow as tf  // Assuming TensorFlow is used

// Load your pre-trained ML model
model = tf.keras.models.load\_model('path/to/your/model.h5')

// Function to process a video frame (if using video streaming)
def process_frame(image_data):
    // Convert raw bytes into an image
    image = Image.open(io.BytesIO(image\_data))
    image = image.resize((224, 224))  // Resize as per model requirement
    image\_array = np.array(image) / 255.0
    image_array = np.expand_dims(image\_array, axis=0)
    prediction = model.predict(image\_array)
    return prediction

async def handler(websocket, path):
    async for message in websocket:
        // When receiving a binary chunk, process it
        if isinstance(message, bytes):
            try:
                result = process\_frame(message)
                // Optionally, send the prediction results back to client
                await websocket.send(str(result))
            except Exception as e:
                print("Error processing frame:", e)
        // Extend similar logic for audio stream processing if needed

start\_server = websockets.serve(handler, 'localhost', 8000)

asyncio.get_event_loop().run_until_complete(start\_server)
asyncio.get_event_loop().run\_forever()

 

Integrating ML Predictions Back to the Web Application

 
  • After the server processes the streaming data and generates predictions, these results can be sent back to the client in real time.
  • The client can display predictions (visual overlays on the video, alerts, or graphical analytics) to the user. This closes the feedback loop, creating an interactive experience.
  • On the client side, listening to messages from the WebSocket server can be implemented as follows:

websocket.onmessage = function(event) {
  // Display or process the ML prediction results received from the server
  console.log('Prediction from ML model:', event.data);
  // You might update the UI with the prediction data here
};

 

Additional Technical Considerations

 
  • Data Serialization: Instead of sending raw binary blobs, consider serializing data (e.g., using JSON for metadata and Base64 encoding for binary data) if needed. However, this can introduce extra overhead for real-time applications.
  • Latency Management: For real-time performance, carefully design your buffering, chunk size, and processing parallelism to minimize latency.
  • Error Handling: Incorporate robust error handling on both client and server sides to handle network interruptions or decoding errors gracefully.
  • Security: Use secure WebSocket connections (wss://) in production to encrypt the data stream, protecting sensitive media content.
  • Scalability: If the solution is deployed at scale, consider using message queues for managing high volumes of streamed data and distributed processing for ML inference.
 


Recognized by the best

Trusted by 600+ businesses globally

From startups to enterprises and everything in between, see for yourself our incredible impact.

RapidDev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with.

They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

Arkady
CPO, Praction
Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost.

He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Donald Muir
Co-Founder, Arc
RapidDev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space.

They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Mat Westergreen-Thorne
Co-CEO, Grantify
RapidDev is an excellent developer for custom-code solutions.

We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Emmanuel Brown
Co-Founder, Church Real Estate Marketplace
Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 

This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Samantha Fekete
Production Manager, Media Production Company
The pSEO strategy executed by RapidDev is clearly driving meaningful results.

Working with RapidDev has delivered measurable, year-over-year growth. Comparing the same period, clicks increased by 129%, impressions grew by 196%, and average position improved by 14.6%. Most importantly, qualified contact form submissions rose 350%, excluding spam.

Appreciation as well to Matt Graham for championing the collaboration!

Michael W. Hammond
Principal Owner, OCD Tech

We put the rapid in RapidDev

Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We’ll discuss your project and provide a custom quote at no cost.Â