You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // @flow
  2. import * as wasmCheck from 'wasm-check';
  3. import JitsiStreamBackgroundEffect from './JitsiStreamBackgroundEffect';
  4. import createTFLiteModule from './vendor/tflite/tflite';
  5. import createTFLiteSIMDModule from './vendor/tflite/tflite-simd';
  6. const models = {
  7. model96: 'libs/segm_lite_v681.tflite',
  8. model144: 'libs/segm_full_v679.tflite'
  9. };
  10. const segmentationDimensions = {
  11. model96: {
  12. height: 96,
  13. width: 160
  14. },
  15. model144: {
  16. height: 144,
  17. width: 256
  18. }
  19. };
  20. /**
  21. * Creates a new instance of JitsiStreamBackgroundEffect. This loads the Meet background model that is used to
  22. * extract person segmentation.
  23. *
  24. * @param {Object} virtualBackground - The virtual object that contains the background image source and
  25. * the isVirtualBackground flag that indicates if virtual image is activated.
  26. * @returns {Promise<JitsiStreamBackgroundEffect>}
  27. */
  28. export async function createVirtualBackgroundEffect(virtualBackground: Object) {
  29. if (!MediaStreamTrack.prototype.getSettings && !MediaStreamTrack.prototype.getConstraints) {
  30. throw new Error('JitsiStreamBackgroundEffect not supported!');
  31. }
  32. let tflite;
  33. if (wasmCheck.feature.simd) {
  34. tflite = await createTFLiteSIMDModule();
  35. } else {
  36. tflite = await createTFLiteModule();
  37. }
  38. const modelBufferOffset = tflite._getModelBufferMemoryOffset();
  39. const modelResponse = await fetch(wasmCheck.feature.simd ? models.model144 : models.model96);
  40. if (!modelResponse.ok) {
  41. throw new Error('Failed to download tflite model!');
  42. }
  43. const model = await modelResponse.arrayBuffer();
  44. tflite.HEAPU8.set(new Uint8Array(model), modelBufferOffset);
  45. tflite._loadModel(model.byteLength);
  46. const options = {
  47. ...wasmCheck.feature.simd ? segmentationDimensions.model144 : segmentationDimensions.model96,
  48. virtualBackground
  49. };
  50. return new JitsiStreamBackgroundEffect(tflite, options);
  51. }