{"id":13459605,"url":"https://github.com/Volcomix/virtual-background","last_synced_at":"2025-03-24T18:31:02.273Z","repository":{"id":38690410,"uuid":"327555293","full_name":"Volcomix/virtual-background","owner":"Volcomix","description":"Demo on adding virtual background to a live video stream in the browser","archived":false,"fork":false,"pushed_at":"2024-04-07T10:52:19.000Z","size":80843,"stargazers_count":473,"open_issues_count":24,"forks_count":122,"subscribers_count":22,"default_branch":"main","last_synced_at":"2024-08-01T10:19:13.984Z","etag":null,"topics":["background","bodypix","demo","mediapipe","mlkit","react","segmentation","selfie","shaders","simd","stream","tensorflow","tfjs","tflite","typescript","video","wasm","webgl"],"latest_commit_sha":null,"homepage":"https://volcomix.github.io/virtual-background","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Volcomix.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2021-01-07T08:53:48.000Z","updated_at":"2024-07-25T01:32:54.000Z","dependencies_parsed_at":"2024-04-07T10:46:04.597Z","dependency_job_id":null,"html_url":"https://github.com/Volcomix/virtual-background","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Volcomix%2Fvirtual-background","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Volcomix%2Fvirtual-background/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Volcomix%2Fvirtual-background/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Volcomix%2Fvirtual-background/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Volcomix","download_url":"https://codeload.github.com/Volcomix/virtual-background/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221995732,"owners_count":16913552,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["background","bodypix","demo","mediapipe","mlkit","react","segmentation","selfie","shaders","simd","stream","tensorflow","tfjs","tflite","typescript","video","wasm","webgl"],"created_at":"2024-07-31T10:00:22.179Z","updated_at":"2024-10-29T05:31:09.895Z","avatar_url":"https://github.com/Volcomix.png","language":"TypeScript","funding_links":[],"categories":["语言资源库","react"],"sub_categories":["typescript"],"readme":"# Virtual Background\n\n\u003e Demo on adding virtual background to a live video stream in the browser.\n\n:point_right: [Try it live here!](https://volcomix.github.io/virtual-background)\n\n[![](./docs/screenshot.png)](https://volcomix.github.io/virtual-background)\n\n## Table of contents\n\n- [Implementation details](#implementation-details)\n  - [BodyPix](#bodypix)\n  - [MediaPipe Meet Segmentation](#mediapipe-meet-segmentation)\n    - [Building TFLite to WebAssembly](#building-tflite-to-webassembly)\n    - [Canvas 2D + CPU](#canvas-2d--cpu)\n    - [WebGL 2](#webgl-2)\n  - [ML Kit Selfie Segmentation](#ml-kit-selfie-segmentation)\n- [Performance](#performance)\n- [Possible improvements](#possible-improvements)\n- [Related work](#related-work)\n- [Running locally](#running-locally)\n- [Building TensorFlow Lite tool](#building-tensorflow-lite-tool)\n\n## Implementation details\n\nIn this demo you can switch between 3 different ML pre-trained segmentation models:\n\n- [BodyPix](https://github.com/tensorflow/tfjs-models/blob/master/body-pix)\n- [MediaPipe Meet Segmentation](https://mediapipe.page.link/meet-mc)\n- [ML Kit Selfie Segmentation](https://developers.google.com/ml-kit/vision/selfie-segmentation)\n\n### BodyPix\n\nThe drawing utils provided in BodyPix are not optimized for the simple background image use case of this demo. That's why I haven't used [toMask](https://github.com/tensorflow/tfjs-models/tree/master/body-pix#bodypixtomask) nor [drawMask](https://github.com/tensorflow/tfjs-models/tree/master/body-pix#bodypixdrawmask) methods from the API to get a higher framerate.\n\nThe [drawBokehEffect](https://github.com/tensorflow/tfjs-models/tree/master/body-pix#bodypixdrawbokeheffect) method from BodyPix API is not used. Instead, [CanvasRenderingContext2D.filter](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/filter) property is configured with [blur](\u003chttps://developer.mozilla.org/en-US/docs/Web/CSS/filter#blur()\u003e) and [CanvasRenderingContext2D.globalCompositeOperation](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation) is setup to blend the different layers according to the segmentation mask.\n\nThe result provides an interesting framerate on laptop but is not really usable on mobile (see [Performance](#performance) for more details). On both devices, the segmentation lacks precision compared to Meet segmentation model.\n\n**Note: BodyPix relies on the default TensorFlow.js backend for your device (i.e. `webgl` usually). The [WASM backend](https://github.com/tensorflow/tfjs/tree/master/tfjs-backend-wasm) seems to be slower for this model, at least on MacBook Pro.**\n\n### MediaPipe Meet Segmentation\n\nMeet segmentation model is only available as a [TensorFlow Lite](https://www.tensorflow.org/lite) model file. Few approaches are discussed in [this issue](https://github.com/tensorflow/tfjs/issues/4177) to convert and use it with TensorFlow.js but I decided to try implementing something closer to Google original approach described in [this post](https://ai.googleblog.com/2020/10/background-features-in-google-meet.html). Hence the demo relies on a small WebAssembly tool built on top of [TFLite](https://blog.tensorflow.org/2020/07/accelerating-tensorflow-lite-xnnpack-integration.html) along with [XNNPACK](https://github.com/google/XNNPACK) delegate and [SIMD](https://github.com/WebAssembly/simd) support.\n\n**Note: [Meet segmentation model card](https://mediapipe.page.link/meet-mc) was initially released under [Apache 2.0](http://www.apache.org/licenses/LICENSE-2.0) license (read more [here](https://github.com/tensorflow/tfjs/issues/4177) and [here](https://github.com/google/mediapipe/issues/1460)) but seems to be switched to [Google Terms of Service](https://policies.google.com/terms?hl=en-US) since Jan 21, 2021. Not sure what it means for this demo. [Here](docs/meet-segmentation-model-card-2020-10-12.pdf) is a copy of the model card matching the model files used in this demo**\n\n#### Building TFLite to WebAssembly\n\nYou can find the source of the TFLite inference tool in the [tflite directory](./tflite) of this repository. Instructions to build TFLite using Docker are described in a dedicated section: [Building TensorFlow Lite tool](#building-tensorflow-lite-tool).\n\n- The Bazel WORKSPACE configuration is inspired from [MediaPipe repository](https://github.com/google/mediapipe/blob/master/WORKSPACE).\n- Emscripten toolchain for Bazel was setup following [Emsdk repository instructions](https://github.com/emscripten-core/emsdk/tree/master/bazel) and changed to match [XNNPACK build config](https://github.com/google/XNNPACK/blob/ec0bf144ad55cde0698083f12e0a20d4525689a6/BUILD.bazel#L7340).\n- TensorFlow source [is patched](tflite/entrypoint.sh#L7) to match Emscripten toolchain and WASM CPU.\n- C++ functions are [called directly from JavaScript](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#call-compiled-c-c-code-directly-from-javascript) to achieve the best performance.\n- [Memory is accessed directly](https://emscripten.org/docs/porting/emscripten-runtime-environment.html#emscripten-memory-representation) from JavaScript through pointer offsets to exchange image data with WASM.\n\n#### Canvas 2D + CPU\n\nThis rendering pipeline is pretty much the same as for BodyPix. It relies on Canvas compositing properties to blend rendering layers according to the segmentation mask.\n\nInteractions with TFLite inference tool are executed on CPU to convert from UInt8 to Float32 for the model input and to apply softmax on the model output.\n\n#### WebGL 2\n\nThe WebGL 2 rendering pipeline relies entirely on `webgl2` canvas context and GLSL shaders for:\n\n- Resizing inputs to fit the segmentation model (there are still CPU operations to copy from RGBA UInt8Array to RGB Float32Array in TFLite WASM memory).\n- [Softmax](https://en.wikipedia.org/wiki/Softmax_function) on segmentation model output to get the probability of each pixel to be a person.\n- Joint bilateral filter to smooth the segmentation mask and to preserve edges from the original input frame (implementation based on [MediaPipe repository](https://github.com/google/mediapipe/blob/master/mediapipe/calculators/image/bilateral_filter_calculator.cc)).\n- Blending background image with [light wrapping](https://www.imaging-resource.com/news/2016/02/11/create-natural-looking-composite-images-using-light-wrapping-technique).\n- Original input frame background blur. Great articles [here](https://rastergrid.com/blog/2010/09/efficient-gaussian-blur-with-linear-sampling/) and [here](https://software.intel.com/content/www/us/en/develop/blogs/an-investigation-of-fast-real-time-gpu-based-image-blur-algorithms.html).\n\n### ML Kit Selfie Segmentation\n\n[Thanks to @RemarkableGuy for pointing out this model.](https://github.com/Volcomix/virtual-background/issues/2)\n\n[Selfie segmentation](https://developers.google.com/ml-kit/vision/selfie-segmentation) model's architecture is very close to the one of Meet segmentation and they both seem to be generated from the same Keras model (see [this issue](https://github.com/Volcomix/virtual-background/issues/2) for more details). It is released under [Apache 2.0](http://www.apache.org/licenses/LICENSE-2.0) and you can find in this repo a [copy of the model card](docs/mlkit-selfie-model-card-2021-02-16.pdf) matching the model used in this demo (here is the [original current model card](https://developers.google.com/ml-kit/images/vision/selfie-segmentation/selfie-model-card.pdf)). The model was extracted from its [official artifact](https://mvnrepository.com/artifact/com.google.mlkit/segmentation-selfie/16.0.0-beta1).\n\nUnlike what is described in the model card, the output of the model is a single channel allowing to get a float value of the segmentation mask. Besides that, the model is inferred using the exact same pipeline as Meet segmentation. However, the model does not perform as well as Meet segmentation because of its higher input resolution (see [Performance](#performance) for more details), even though it still offers better quality segmentation than BodyPix.\n\n## Performance\n\nHere are the performance observed for the whole rendering pipelines, including inference and post-processing, when using the device camera on smartphone **Pixel 3 (Chrome)**.\n\n| Model   | Input resolution | Backend          | Pipeline        | FPS |\n| ------- | ---------------- | ---------------- | --------------- | --- |\n| BodyPix | 640x360          | WebGL            | Canvas 2D + CPU | 11  |\n| ML Kit  | 256x256          | WebAssembly      | Canvas 2D + CPU | 9   |\n| ML Kit  | 256x256          | WebAssembly      | WebGL 2         | 9   |\n| ML Kit  | 256x256          | WebAssembly SIMD | Canvas 2D + CPU | 17  |\n| ML Kit  | 256x256          | WebAssembly SIMD | WebGL 2         | 19  |\n| Meet    | 256x144          | WebAssembly      | Canvas 2D + CPU | 14  |\n| Meet    | 256x144          | WebAssembly      | WebGL 2         | 16  |\n| Meet    | 256x144          | WebAssembly SIMD | Canvas 2D + CPU | 26  |\n| Meet    | 256x144          | WebAssembly SIMD | WebGL 2         | 31  |\n| Meet    | 160x96           | WebAssembly      | Canvas 2D + CPU | 29  |\n| Meet    | 160x96           | WebAssembly      | WebGL 2         | 35  |\n| Meet    | 160x96           | WebAssembly SIMD | Canvas 2D + CPU | 48  |\n| Meet    | 160x96           | WebAssembly SIMD | WebGL 2         | 60  |\n\n## Possible improvements\n\n- Rely on alpha channel to save texture fetches from the segmentation mask.\n- Blur the background image outside of the rendering loop and use it for light wrapping instead of the original background image. This should produce better rendering results for large light wrapping masks.\n- Optimize joint bilateral filter shader to prevent unnecessary variables, calculations and costly functions like `exp`.\n- Try [separable approximation](https://www.researchgate.net/publication/4181202_Separable_bilateral_filtering_for_fast_video_preprocessing) for joint bilateral filter.\n- Compute everything on lower source resolution (scaling down at the beginning of the pipeline).\n- Build TFLite and XNNPACK with multithreading support. Few configuration examples are in [TensorFlow.js WASM backend](https://github.com/tensorflow/tfjs/blob/master/tfjs-backend-wasm/src/cc/BUILD).\n- Detect WASM features to load automatically the right TFLite WASM runtime. Inspirations could be taken from [TensorFlow.js WASM backend](https://github.com/tensorflow/tfjs/blob/master/tfjs-backend-wasm/src/flags_wasm.ts) which is based on [GoogleChromeLabs/wasm-feature-detect](https://github.com/GoogleChromeLabs/wasm-feature-detect).\n- Experiment with [DeepLabv3+](https://github.com/tensorflow/models/tree/master/research/deeplab) and maybe retrain `MobileNetv3-small` model directly.\n\n## Related work\n\nYou can learn more about a pre-trained TensorFlow.js model in the [BodyPix repository](https://github.com/tensorflow/tfjs-models/blob/master/body-pix).\n\nHere is a technical overview of [background features in Google Meet](https://ai.googleblog.com/2020/10/background-features-in-google-meet.html) which relies on:\n\n- [MediaPipe](https://mediapipe.dev/)\n- [WebAssembly](https://webassembly.org/)\n- [WebAssembly SIMD](https://github.com/WebAssembly/simd)\n- [WebGL](https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API)\n- [XNNPACK](https://github.com/google/XNNPACK)\n- [TFLite](https://blog.tensorflow.org/2020/07/accelerating-tensorflow-lite-xnnpack-integration.html)\n- [Custom segmentation ML models from Google](https://mediapipe.page.link/meet-mc)\n- Custom rendering effects through OpenGL shaders from Google\n\n## Running locally\n\nIn the project directory, you can run:\n\n### `yarn start`\n\nRuns the app in the development mode.\\\nOpen [http://localhost:3000](http://localhost:3000) to view it in the browser.\n\nThe page will reload if you make edits.\\\nYou will also see any lint errors in the console.\n\n### `yarn test`\n\nLaunches the test runner in the interactive watch mode.\\\nSee the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.\n\n### `yarn build`\n\nBuilds the app for production to the `build` folder.\\\nIt correctly bundles React in production mode and optimizes the build for the best performance.\n\nThe build is minified and the filenames include the hashes.\\\nYour app is ready to be deployed!\n\nSee the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.\n\n## Building TensorFlow Lite tool\n\nDocker is required to build TensorFlow Lite inference tool locally.\n\n### `yarn build:tflite`\n\nBuilds WASM functions that can infer Meet and ML Kit segmentation models. The TFLite tool is built both with and without SIMD support.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FVolcomix%2Fvirtual-background","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FVolcomix%2Fvirtual-background","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FVolcomix%2Fvirtual-background/lists"}