2019-07-03 15:38:25 +00:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import { load } from '@tensorflow-models/body-pix';
|
2019-09-24 13:50:11 +00:00
|
|
|
import * as tfc from '@tensorflow/tfjs-core';
|
2019-07-03 15:38:25 +00:00
|
|
|
import JitsiStreamBlurEffect from './JitsiStreamBlurEffect';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This promise represents the loading of the BodyPix model that is used
|
|
|
|
* to extract person segmentation. A multiplier of 0.25 is used to for
|
|
|
|
* improved performance on a larger range of CPUs.
|
|
|
|
*/
|
|
|
|
const bpModelPromise = load(0.25);
|
|
|
|
|
2019-09-24 13:50:11 +00:00
|
|
|
/**
|
|
|
|
* Configure the Tensor Flow model to use the webgl backend which is the
|
|
|
|
* most powerful backend for the browser.
|
|
|
|
*/
|
|
|
|
const webGlBackend = 'webgl';
|
|
|
|
|
2019-07-03 15:38:25 +00:00
|
|
|
/**
|
|
|
|
* Creates a new instance of JitsiStreamBlurEffect.
|
|
|
|
*
|
|
|
|
* @returns {Promise<JitsiStreamBlurEffect>}
|
|
|
|
*/
|
|
|
|
export function createBlurEffect() {
|
|
|
|
if (!MediaStreamTrack.prototype.getSettings && !MediaStreamTrack.prototype.getConstraints) {
|
|
|
|
return Promise.reject(new Error('JitsiStreamBlurEffect not supported!'));
|
|
|
|
}
|
|
|
|
|
2019-09-24 13:50:11 +00:00
|
|
|
const setBackendPromise = new Promise((resolve, reject) => {
|
|
|
|
if (tfc.getBackend() === webGlBackend) {
|
|
|
|
resolve();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tfc.setBackend(webGlBackend)
|
|
|
|
.then(resolve, reject);
|
|
|
|
});
|
|
|
|
|
|
|
|
return setBackendPromise
|
|
|
|
.then(() => bpModelPromise)
|
|
|
|
.then(bpmodel => new JitsiStreamBlurEffect(bpmodel));
|
2019-07-03 15:38:25 +00:00
|
|
|
}
|