npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

virtual-background-ai

v1.0.0

Published

AI-powered virtual background replacement using TensorFlow.js and Robust Video Matting

Readme

Virtual Background AI

AI-powered virtual background replacement using TensorFlow.js and Robust Video Matting. This package provides easy-to-use methods for applying virtual backgrounds to video elements in real-time.

🚀 Features

  • Real-time Background Replacement: Instant AI-powered background removal and replacement
  • Multiple Background Types: Color backgrounds, image backgrounds, and blur effects
  • TypeScript Support: Full TypeScript definitions included
  • Memory Efficient: Proper tensor disposal and resource management
  • Easy Integration: Simple API for quick implementation

📦 Installation

npm install virtual-background-ai

Note: This package requires @tensorflow/tfjs as a peer dependency. Make sure to install it:

npm install @tensorflow/tfjs

🎯 Quick Start

<!DOCTYPE html>
<html>
<head>
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js"></script>
</head>
<body>
    <video id="video" autoplay muted playsinline></video>
    <div id="output"></div>

    <script type="module">
        import { applyColorBackground } from 'virtual-background-ai';

        const video = document.getElementById('video');
        const output = document.getElementById('output');

        // Get webcam stream
        navigator.mediaDevices.getUserMedia({ video: true })
            .then(stream => {
                video.srcObject = stream;
                
                // Apply red background after video loads
                video.onloadedmetadata = async () => {
                    const canvas = await applyColorBackground(video, '#ff0000');
                    output.appendChild(canvas);
                };
            });
    </script>
</body>
</html>

📚 API Reference

Class: VirtualBackgroundAI

Main class for managing virtual background operations.

Constructor

new VirtualBackgroundAI(options?: VirtualBackgroundOptions)

Options:

  • modelPath?: string - Path to the model files (default: 'model/model.json')
  • downsampleRatio?: number - Downsample ratio for processing (default: 0.5)

Methods

initialize(): Promise<void>

Initialize the AI model. Called automatically when needed.

applyColorBackground(videoElement: HTMLVideoElement, color: string): Promise<HTMLCanvasElement>

Apply a solid color background.

Parameters:

  • videoElement - HTML video element
  • color - Color in hex format (e.g., '#ff0000') or rgb format (e.g., 'rgb(255, 0, 0)')

Returns: Promise resolving to canvas with applied background

applyImageBackground(videoElement: HTMLVideoElement, imageUrl: string): Promise<HTMLCanvasElement>

Apply an image background.

Parameters:

  • videoElement - HTML video element
  • imageUrl - URL of the background image

Returns: Promise resolving to canvas with applied background

applyBlurBackground(videoElement: HTMLVideoElement): Promise<HTMLCanvasElement>

Apply a blur background effect.

Parameters:

  • videoElement - HTML video element

Returns: Promise resolving to canvas with applied blur background

dispose(): void

Clean up resources and dispose of tensors.

Convenience Functions

For one-time operations, you can use these convenience functions:

applyColorBackground(videoElement, color, options?)

import { applyColorBackground } from 'virtual-background-ai';

const canvas = await applyColorBackground(video, '#00ff00');

applyImageBackground(videoElement, imageUrl, options?)

import { applyImageBackground } from 'virtual-background-ai';

const canvas = await applyImageBackground(video, 'https://example.com/background.jpg');

applyBlurBackground(videoElement, options?)

import { applyBlurBackground } from 'virtual-background-ai';

const canvas = await applyBlurBackground(video);

🔧 Advanced Usage

Reusable Instance

For multiple operations, create a reusable instance:

import VirtualBackgroundAI from 'virtual-background-ai';

const vbg = new VirtualBackgroundAI({
    modelPath: '/path/to/model',
    downsampleRatio: 0.5
});

// Initialize once
await vbg.initialize();

// Apply different backgrounds
const redCanvas = await vbg.applyColorBackground(video, '#ff0000');
const imageCanvas = await vbg.applyImageBackground(video, 'background.jpg');
const blurCanvas = await vbg.applyBlurBackground(video);

// Clean up when done
vbg.dispose();

Real-time Processing

import VirtualBackgroundAI from 'virtual-background-ai';

const vbg = new VirtualBackgroundAI();
await vbg.initialize();

const outputCanvas = document.getElementById('output');

async function processFrame() {
    const canvas = await vbg.applyColorBackground(video, '#00ff00');
    
    // Replace previous canvas
    outputCanvas.innerHTML = '';
    outputCanvas.appendChild(canvas);
    
    // Continue processing
    requestAnimationFrame(processFrame);
}

processFrame();

📁 Model Files

This package requires the Robust Video Matting model files. You need to provide:

  • model.json - Model configuration
  • group1-shard1of1.bin - Model weights

Place these files in a model/ directory relative to your HTML file, or specify a custom path in the options.

🌐 Browser Support

  • Chrome 67+
  • Firefox 60+
  • Safari 11+
  • Edge 79+

📄 License

MIT License

🙏 Acknowledgments

  • PeterL1n for the original RobustVideoMatting TensorFlow.js implementation
  • This package is based on the RobustVideoMatting TensorFlow.js implementation by PeterL1n. The code and pre-trained model are sourced from the original repository.
  • TensorFlow.js team for the amazing framework

👨‍💻 Developer

Developed by Rajat Shrivastava