5.6.0 • Published 4 months ago

@personifyinc/psyseg-sdk v5.6.0

Weekly downloads
-
License
ISC
Repository
github
Last release
4 months ago

PsySeg SDK for JavaScript

The Personify Segmentation JavaScript Software Development Kit (PsySeg JS SDK) is a library that utilizes our state-of-the-art machine learning models and algorithms to perform user segmentation and apply special effects on the background.

Prerequisites

This project requires NodeJS and NPM. Node and NPM are really easy to install. To make sure you have them available on your machine, try running the following command.

$ npm -v && node -v
6.4.1
v8.16.0

Getting Started

Installation

To install and set up the latest sdk, run:

$ npm install @personifyinc/psyseg-sdk

To install and set up specific sdk version, run:

$ npm install @personifyinc/psyseg-sdk@x.x.x

Usage

To use the SDK interfaces, simply import @personifyinc/psyseg-sdk as you normally would.

import PsySeg, { IPsySegResponses, IPsySegEffectParams, IPsySegProps } from '@personifyinc/psyseg-sdk';

To use the SDK class, simply import @personifyinc/psyseg-sdk as you normally would.

import PsySeg from '@personifyinc/psyseg-sdk';

const psySegInstance = new PsySeg()

Interfaces

IPsySegResponses

export interface IPsySegResponses {
    status: boolean
    licenseControl?: string
    data?: ImageData
    modelSize?: {
        width: number
        height: number
    }
}

IPsySegEffectParams

export interface IPsySegEffectParams {
    mode?: string
    erode?: number
    enhanceFrame?: boolean
    gamma?: number
    isSkipAlpha?: boolean
    blur?: number
    dstSize?: {
        width: number,
        height: number
    }
    overlaySize?: number
    overlayPosition?: number
    shape?: string
    isRemoveBlob?: boolean
}

IPsySegProps

export interface IPsySegProps {
    psySegWasmURL?: string
    tfliteWasmURL?: string
    tfliteSimdWasmURL?: string
    segmentationModelURL?: string
    license?: string
    accessToken?: string
    accessKey?: string
}

APIs

version

Get the current version of the installed SDK.

const version = psySegInstance.version()
//=> v3.0.0

init

It is required because it initializes the wasm file and validates the license.

const psySegProps: IPsySegProps = {
    psySegWasmURL: '',
    tfliteWasmURL: '',
    tfliteSimdWasmURL: '',
    segmentationModelURL: '',
    license: '',
    accessToken: '',
    accessKey: ''
}
await psySegInstance.init(psySegProps)
AttributeTypeRequiredDefaultDescription
psySegWasmURLstringnoemptyWhere to host the wasm file, if this attribute empty it should be hosted from our CDN https://psyjs-cdn.personifyinc.com/core/wasm/psyseg-x.x.x.wasm
tfliteWasmURLstringnoemptyWhere to host the wasm file, if this attribute empty it should be hosted from our CDN https://psyjs-cdn.personifyinc.com/core/tflite/wasm/tflite-20221011.wasm
tfliteSimdWasmURLstringnoemptyWhere to host the wasm file, if this attribute empty it should be hosted from our CDN https://psyjs-cdn.personifyinc.com/core/tflite/wasm/tflite-simd-20221011.wasm
segmentationModelURLstringnoemptyWhere to host the model tflite file, if this attribute empty it should be hosted from our CDN https://psyjs-cdn.personifyinc.com/core/tflite/model/psysegmentation-112x192-20230419_e36.asset
licensestringnoemptyFor getting more information, please contact PersonifyInc
accessTokenstringnoemptyFor getting more information, please contact PersonifyInc
accessKeystringnoemptyFor getting more information, please contact PersonifyInc

replaceBackground

Extract user from background and replace it with desired ones.

const inputColor: ImageData = inputImageData
const inputBackground: ImageData = backgroundImageData
const effectParams: IPsySegEffectParams = {
    mode: 'full',
    erode: 1,
    enhanceFrame: false,
    gamma: 0,
    isSkipAlpha: false,
    overlaySize: 1,
    overlayPosition: 0.5,
    isRemoveBlob: true,
}

const result: IPsySegResponses = await psySegInstance.replaceBackground(
    inputColor,
    inputBackground,
    effectParams
)
AttributeTypeRequiredDefaultDescription
inputColorImageDatayesThe ImageData of Video Frame or Image Element
inputBackgroundImageDatayesThe ImageData of Image Element
effectParamsIPsySegEffectParamsno{}The advanced configuration
effectParams.modestringnofullSetting mode for PsySeg to reach the best performance or quality based on different purposes of customer.By default, full mode is selected. This option provides the best quality, but it has a tradeoff for performance
effectParams.erodenumberno1Erode level for edge smoothing
effectParams.enhanceFramebooleannofalseEnable lighting feature
effectParams.gammanumberno0Gamma value for lighting feature
effectParams.isSkipAlphabooleannofalseEnable skip alpha feature
effectParams.overlaySizenumberno1Overlay size of user extraction, 0 < overlaySize <= 1, step is 0.1
effectParams.overlayPositionnumberno0.5Overlay position from left to right for user extraction, 0 < overlayPosition <= 1, step is 0.1
effectParams.isRemoveBlobbooleannotrueRemove the blobs surrounding the user

blurBackground

Extracting user from background and making a background blurred to hidden unnecessary object.

const inputColor: ImageData = inputImageData
const effectParams: IPsySegEffectParams = {
    mode: 'full',
    erode: 1,
    enhanceFrame: false,
    gamma: 0,
    isSkipAlpha: false,
    blur: 10,
}

const result: IPsySegResponses = await psySegInstance.blurBackground(
    inputColor,
    effectParams
)
AttributeTypeRequiredDefaultDescription
inputColorImageDatayesThe ImageData of Video Frame or Image Element
effectParamsIPsySegEffectParamsno{}The advanced configuration
effectParams.modestringnofullSetting mode for PsySeg to reach the best performance or quality based on different purposes of customer.By default, full mode is selected. This option provides the best quality, but it has a tradeoff for performance
effectParams.erodenumberno1Erode level for edge smoothing
effectParams.enhanceFramebooleannofalseEnable lighting feature
effectParams.gammanumberno0Gamma value for lighting feature
effectParams.isSkipAlphabooleannofalseEnable skip alpha feature
effectParams.blurnumberno10Blur level

removeBackground

Extract only user from background.

const inputColor: ImageData = inputImageData
const effectParams: IPsySegEffectParams = {
    mode: 'full',
    erode: 1,
    enhanceFrame: false,
    gamma: 0,
    isSkipAlpha: false,
}

const result: IPsySegResponses = await psySegInstance.removeBackground(
    inputColor,
    effectParams
)
AttributeTypeRequiredDefaultDescription
inputColorImageDatayesThe ImageData of Video Frame or Image Element
effectParamsIPsySegEffectParamsno{}The advanced configuration
effectParams.modestringnofullSetting mode for PsySeg to reach the best performance or quality based on different purposes of customer.By default, full mode is selected. This option provides the best quality, but it has a tradeoff for performance
effectParams.erodenumberno1Erode level for edge smoothing
effectParams.enhanceFramebooleannofalseEnable lighting feature
effectParams.gammanumberno0Gamma value for lighting feature
effectParams.isSkipAlphabooleannofalseEnable skip alpha feature

runAlpha

Extract alpha mask that separate persona(transparent) and background(black).

const inputColor: ImageData = inputImageData
const effectParams: IPsySegEffectParams = {
    isSkipAlpha: false,
}

const result: IPsySegResponses = await psySegInstance.runAlpha(
    inputColor,
    effectParams
)
AttributeTypeRequiredDefaultDescription
inputColorImageDatayesThe ImageData of Video Frame or Image Element
effectParamsIPsySegEffectParamsno{}The advanced configuration
effectParams.isSkipAlphabooleannofalseEnable skip alpha feature

toMask

Extract alpha mask that separate persona(white) and background(transparent).

const inputColor: ImageData = inputImageData
const effectParams: IPsySegEffectParams = {
    isSkipAlpha: false,
    dstSize: {
        width: 320,
        height: 240,
    },
    erode: 1,
}

const result: IPsySegResponses = await psySegInstance.toMask(
    inputColor,
    effectParams
)
AttributeTypeRequiredDefaultDescription
inputColorImageDatayesThe ImageData of Video Frame or Image Element
effectParamsIPsySegEffectParamsno{}The advanced configuration
effectParams.isSkipAlphabooleannofalseEnable skip alpha feature
effectParams.dstSizeobjectno{ width: 160, height: 96 }Destination size of output
effectParams.erodenumberno1Erode level for edge smoothing

shapeForeground

Ability to present user in a circle/square shape and merge into a background image

const inputColor: ImageData = inputImageData
const inputBackground: ImageData = backgroundImageData
const effectParams: IPsySegEffectParams = {
    shape: 'circle'
}

const result: IPsySegResponses = await psySegInstance.shapeForeground(
    inputColor,
    inputBackground,
    effectParams
)
AttributeTypeRequiredDefaultDescription
inputColorImageDatayesThe ImageData of Video Frame or Image Element
inputBackgroundImageDatayesThe ImageData of Image Element
effectParamsIPsySegEffectParamsno{}The advanced configuration
effectParams.shapestringnocirclePresent user in a border shape, it should be "circle"/"square"

maskShapeBackground

Make a mask has circle/square shape into center of background image

const inputBackground: ImageData = backgroundImageData
const effectParams: IPsySegEffectParams = {
    shape: 'circle'
}

const result: IPsySegResponses = await psySegInstance.maskShapeBackground(
    inputBackground,
    effectParams
)
AttributeTypeRequiredDefaultDescription
inputBackgroundImageDatayesThe ImageData of Image Element
effectParamsIPsySegEffectParamsno{}The advanced configuration
effectParams.shapestringnocircleMake mask in a border shape, it should be "circle"/"square"

Examples

Create Image from URL

export const createImage = async (
    imgSrc: string,
    resolution: _IResolution
): Promise<HTMLImageElement> => {
    return new Promise(resolve => {
        const img = new Image(resolution.width, resolution.height)
        img.crossOrigin = 'Anonymous'
        img.onload = () => {
            resolve(img)
        }
        img.src = imgSrc
    })
}

Get ImageData from Video Frame or Image

export const getImageData = (
    sourceInput: HTMLVideoElement | HTMLImageElement | null,
    resolution: _IResolution
) => {
    const inputCanvas = document.createElement('canvas')
    inputCanvas.width = resolution.width
    inputCanvas.height = resolution.height
    const canvasContext = inputCanvas.getContext('2d')

    if (canvasContext && sourceInput) {
        canvasContext.drawImage(
            sourceInput,
            0,
            0,
            resolution.width,
            resolution.height
        )
        return canvasContext.getImageData(0, 0, resolution.width, resolution.height)
    }
}

Render the Image Data into the Canvas Output

export const drawCanvas = (
    outputBuf: ImageData | undefined,
    ctxOutput: CanvasRenderingContext2D,
    options: _IDrawnCanvasOptions
) => {
    if (outputBuf && ctxOutput) {
        const {canvasWidth, canvasHeight} = options

        ctxOutput.clearRect(0, 0, canvasWidth, canvasHeight)
        ctxOutput.putImageData(outputBuf, 0, 0)

    }
}

How to use PsySeg SDK

// Initialize PsySeg SDK instance
const psySegInstance = new PsySeg()
console.info('PsySeg Version: ', psySegInstance.version())
const psySegInstanceInit = await psySegInstance.init({
    license,
    accessToken,
    accessKey,
})
console.info('PsySeg Initiation: ', psySegInstanceInit)

// Get Background Image Data
const imageBackground = await createImage('https://psyjs-cdn.personifyinc.com/assets/psyseg2-js-demo-background.png', {
    width: 320,
    height: 240,
})
const inputBackground = getImageData(imageBackground, {
    width: 320,
    height: 240,
}) as ImageData

// Get Video Element & Canvas Element
const videoElement = document.getElementById('#video')
const canvasOutput = document.getElementById('#canvas')
canvasOutput.width = 320
canvasOutput.height = 240
const ctxOutput = canvasOutput.getContext(
    '2d'
) as CanvasRenderingContext2D

// Running function
const runEffect = async (): Promise<void> => {
    try {
        const inputColor = getImageData(
            videoElement as HTMLVideoElement,
            {width: 320, height: 240}
        ) as ImageData

        const output = await psySegInstance.replaceBackground(
            inputColor,
            inputBackground as ImageData,
            {
                mode: 'full',
                erode: 1,
                enhanceFrame: false,
                gamma: 0,
                isSkipAlpha: false,
                overlaySize: 1,
                overlayPosition: 0.5,
            }
        )

        if (output?.status) {
            drawCanvas(
                output.data,
                ctxOutput,
                {
                    canvasWidth: 320,
                    canvasHeight: 240,
                }
            )
        }

        // Timeout will affect to the FPS and CPU usage
        const timeout = 1000 / 30

        // Recursive call
        setTimeout(runEffect, timeout)
    } catch (e) {
        console.error(e)
    }
}

runEffect()

Notes: For Real-Time Messaging Protocol (RTMP), we should use insertable streams instead of setTimeout or requestAnimationFrame to keep web application working in inactive mode. Please take a look at https://developer.chrome.com/articles/mediastreamtrack-insertable-media-processing/ and https://mediastreamtrack.glitch.me/

Demo

Please take a look our demo

Contact

For getting more information, please contact PersonifyInc

Changelog

Release npm package @personifyinc/psyseg-sdk - v5.6.0

  • Main Features:
    • Add a parameter to toggle the removal of the box blob
    • Ensure license validation prior to invoking APIs
    • Add an erosion value to the toMask api for the ability to adjust the edges of the body

Release npm package @personifyinc/psyseg-sdk - v5.5.1

  • Bug Fixes:
    • Fix issue: module parse failed: Unexpected character '#' while integrating with react-script 4

Release npm package @personifyinc/psyseg-sdk - v5.5.0

  • Main Features:
    • Present user in a circle/square shape and merge into a background image
    • Make a mask has circle/square shape into center of background image

Release npm package @personifyinc/psyseg-sdk - v5.4.0

  • Main Features:
    • Reducing the SDK startup time
    • Update api replaceBackground to scale and move UE
    • Improve Psyseg model
    • Return the model size for api init()

Release npm package @personifyinc/psyseg-sdk - v5.3.0

  • Main Features:
    • Re-provide the api runAlpha

Release npm package @personifyinc/psyseg-sdk - v5.2.0

  • Main Features:
    • Apply license validation to the api toMask
    • Remove online license validation
    • Remove api runAlpha
    • Reduce CPU and memory usage

Release npm package @personifyinc/psyseg-sdk - v5.0.0

  • Main Features:
    • Separate the tflite model from the wasm file to reduce capacity and easily develop other models with different sizes in the future
    • Update the tflite model to improve segmentation and reduce CPU & Memory usage
    • Remove unnecessary logs

Release npm package @personifyinc/psyseg-sdk - v4.0.0

  • Main Features:
    • Implement API toMask to extract alpha mask that separate persona(white) and background(transparent).
    • Use reject statement instead of resolve statement for catching error
    • Update API version() to return value v4.0.0

Release npm package @personifyinc/psyseg-sdk - v3.0.0

  • Main Features:
    • Restructure the entire SDK to make it simpler and easier for developers to integrate.
    • Add an init() method to wait for the SDK to initialize.
    • Remove some useless packages to reduce the size of SDK.
    • Remove some redundant code as well as some unnecessary processing to reduce CPU usage.
  • Bug Fixes:
    • Fix issue: Bypass license control by blocking the api checkLicense
5.6.0

4 months ago

5.5.1

6 months ago

5.5.0

7 months ago

5.4.0

1 year ago

5.3.0-trial.1

1 year ago

5.3.0

1 year ago

5.2.0

1 year ago

5.2.0-beta.1

1 year ago

5.0.0

2 years ago

5.0.0-beta.1

2 years ago

4.0.0

2 years ago

3.0.0

2 years ago