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.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // @flow
  2. import * as wasmCheck from 'wasm-check';
  3. import JitsiStreamBlurEffect from './JitsiStreamBlurEffect';
  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 JitsiStreamBlurEffect. This loads the bodyPix model that is used to
  22. * extract person segmentation.
  23. *
  24. * @returns {Promise<JitsiStreamBlurEffect>}
  25. */
  26. export async function createBlurEffect() {
  27. if (!MediaStreamTrack.prototype.getSettings && !MediaStreamTrack.prototype.getConstraints) {
  28. throw new Error('JitsiStreamBlurEffect not supported!');
  29. }
  30. let tflite;
  31. if (wasmCheck.feature.simd) {
  32. tflite = await createTFLiteSIMDModule();
  33. } else {
  34. tflite = await createTFLiteModule();
  35. }
  36. const modelBufferOffset = tflite._getModelBufferMemoryOffset();
  37. const modelResponse = await fetch(
  38. wasmCheck.feature.simd ? models.model144 : models.model96
  39. );
  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 = wasmCheck.feature.simd ? segmentationDimensions.model144 : segmentationDimensions.model96;
  47. return new JitsiStreamBlurEffect(tflite, options);
  48. }