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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@virtualscenery/greenscreenstream

v3.0.2

Published

Genereate new MediaStreams or Canvas elements based on MediaStreams (webcam) with any background image/video. Greenscreen your webcam and enable virtual backgrounds in your web applications.

Downloads

8

Readme

About

GreenScreenStream provides a wide range of options for manipulating Mediastreams.

Generate a new MediaStream for <canvas> & <video> elements based on provided MediaStreamTrack and a background image/video just using JavaScript.

After processed and "green screened" you can capture the stream and use it in your WebRTC calls for instance.

All rendering is done in realtime using a WebGL2 pixel shader (glsl) and optionally machine-learning.

Install

npm i @virtualscenery/greenscreenstream  

Examples

Below you find a few different examples of greenscreenstream.

ML based Virtual Background - Image Background

ML based Virtual Background - Video Background

ML based Virtual Background - WebGL Background

ML based Virtual Background - Hologram Effect

https://virtualscenery.github.io/greenscreenstream-examples/example/WebGL

Look here for implementation.

GreenScreenStream API

Contents

GreenScreenStream (Class)

GreenScreenMethod (Enum)

Vector2 (Class)

VideoResolution (Enum)

IGreenScreenConfig (Interface)

BodyPixMode (Enum)

IMaskSettings (Interface)

GreenScreenStream (Class)

Constructor

Creates an instance of GreenScreenStream

constructor(greenScreenMethod: GreenScreenMethod, resolution: VideoResolution | Vector2, canvas?: HTMLCanvasElement)

Methods

initialize

Initlializes the GreenScreenStream with the provided background (image or video) and settings.

initialize(backgroundUrl?: string, config?: GreenScreenConfig): Promise<boolean> 

addVideoTrack

Adds a MediaStreamTrack (i.e webcam)

addVideoTrack(track:  MediaStreamTrack):  Promise<void | any>;

start

Starts rendering the greenscreen. You can optionally set a fps maximum here

start(maxFps?: number): void

stop

Stops the rendering process. Optionally stop the media streams.
Stopping the streams works only if there are no references to them outside of greenscreenstream.

stop(stopMediaStreams?:boolean): void

captureStream

Capture the rendered result to a MediaStream that you apply to your <video> element.

captureStream(fps?:  number):  MediaStream;    

setBackground

Sets the virtual background to a new image or video. Can be done while GreenScreenStream is running.

 setBackground(src: string): Promise<HTMLImageElement | HTMLVideoElement | Error>

setBodyPixModel

Swaps out the currently used BodyPixModel used in ml mode (GreenScreenMethod.VirtualBackground) (See GreenScreenMethod (Enum))

setBodyPixModel(config: IGreenScreenConfig): Promise<void>

scaleImageToCanvas

Scales the passed in image to canvas size and returns a scaled copy of it. Gets called automatically everytime a new image background is set. The imageOptions defaults to the current size of the greenscreen canvas and high quality .

public async scaleImageToCanvas(image: HTMLImageElement, imageOptions?: ImageBitmapOptions): Promise<HTMLImageElement>

getColorsFromStream

Gets the most dominant color and a list (palette) of the colors most common in the provided MediaStreamTrack.

getColorsFromStream(): { palette: [number, number,number][], dominant: [number, number,number] } {

setChromaKey

Pass a mask (rgb), color to the shader , to use as a mask. Should be the dominant color, or on of the palette colors detected. See getColorsFromStream

setChromaKey(r: number, g: number, b: number, threshold?: number): void;

setRange

Range is used to decide the amount of color to be used from either foreground or background.Playing with this variable will decide how much the foreground and background blend together.

setMaskRange(x:number,y:number): void

dominant

Get the most dominant color based on imageData and number of pixels

dominant(imageData: ImageData, pixelCount: number): [number, number,number] {

palette

Get an Array of the most significant colors in the MediaTrack

pallette(imageData: ImageData, pixelCount: number): [number, number,number][] | null {

GreenScreenMethod (Enum)

Describes the method GreenScreenStream should use for applying a virtual background.
GreenScreenMethod.VirtualBackground uses a machine learning model (Tensorflow BodyPix)
GreenScreenMethod.VirtualBackgroundUsingGreenScreen works without a machine learning model and thus consumes much less performance,
but requires the user to have a green screen.

enum GreenScreenMethod {
    VirtualBackground, 
    VirtualBackgroundUsingGreenScreen
}

VideoResolution (enum)

Describes resolution presets GreenScreenStream should use.

enum VideoResolution {
    SD,
    HD,
    FullHD,
    WQHD,
    UHD
}

Vector2 (Class)

Describes a custom resolution that GreenScreenStream can use.

Constructor

Both values default to zero.

constructor(x?: number, y?: number)

Methods

toString

Returns the current values as a string (" x : y")

toString(): string 

toMediaConstraint

Returns the current value in a format that can be used as MediaTrackConstraints

toMediaConstraint(): MediaTrackConstraints

isValidVector2

Checks if the provided input is a valid vector2. Returns true if so.

static isValidVector2(input: any): boolean

IGreenScreenConfig (Interface)

Provides detailed configuration options for GreenScreenStream.
maskSettings can be uses to fine tune the virtual background appearance. (
bodyPixMode can be used to apply premade BodyPix configurations (see GreenScreenStreamBodyPixMode for more details),
while bodyPixConfig allows you to configure BodyPix as you see fit. If both are provided, bodyPixMode will be ignored.

IGreenScreenConfig {
    maskSettings?: IMaskSettings,
    bodyPixMode?: GreenScreenStreamBodyPixMode,
    bodyPixConfig?: IBodyPixConfig
}

BodyPixMode (Enum)

Determines which BodyPix Preset GreenStream should use.

Presets Standard or Precise are recommended for most use cases.
Fast is meant for really weak clients, is unprecise and causes flickering.
Maximum uses a more complex ML Model and thus causes much more network traffic & gpu + cpu load.\

enum BodyPixMode {
    Fast = 0,
    Standard = 1,
    Precise = 2,
    Maximum = 3
}

Preset Details:

Fast

architecture: 'MobileNetV1',
outputStride: 16,
multiplier: 0.5,
quantBytes: 1

Standard

architecture: 'MobileNetV1',
outputStride: 16,
multiplier: 0.75,
quantBytes: 2

Precise

architecture: 'MobileNetV1',
outputStride: 16,
multiplier: 1,
quantBytes: 2

Maximum

architecture: 'ResNet50',
outputStride: 32,
quantBytes: 2

IMaskSettings (Interface)

Description TBA

interface IMaskSettings {
    opacity?: number
    flipHorizontal?: boolean
    maskBlurAmount?: number
    foregroundColor?: RGBA
    backgroundColor?: RGBA
    segmentPerson?: {
        flipHorizontal?: boolean
        internalResolution?: string
        segmentationThreshold?: number
        maxDetections?: number
        quantBytes?: number
    }
};
export interface RGBA {
    r: number, g: number, b: number, a: number
}