Skip to content

Conversation

rapterjet2004
Copy link
Contributor

@rapterjet2004 rapterjet2004 commented Sep 16, 2025

This PR implements Background Blurring, an optional (by default off) effect that, like web and iOS, uses a low-memory on-device AI model to create a mask between the foreground/background, before using openCV to perform gaussian blur, along with several other file format conversions, necessary for image processing Y'UV frames to RGBA and vice-versa. In the future this PR also makes it easier to implement additional background effects, like background images, background videos/gifs, party hats, company logo overlays, and more.

Adds new files

  • BackgroundBlurFrameProcessor an implementation of VideoProcessor which is an interface that WebRTC provides to perform operations on camera frames, and ImageSegmentationHelper.SegmenterListener, which is a small interface I wrote to handle asynchronous passing of camera frames from MediaPipe (You can actually handle it synchronously too, since the VideoProcessor operates on it's own thread, but this is cleaner)
  • ImageSegmentationHelper an edited version of google's. Basically a wrapper over MediaPipe's ImageSegmenter which uses the on device AI Model to segment images between the foreground and background.
  • BlurBackgroundViewModel handles the state of the blur background functionality, which is by default off
  • download_model.gradle a small gradle file to download the on device AI Model on compile time, if not already found. The model is pretty light, just a couple hundred KBs. Also it's licensed for Commercial use under Apache v2.
  • background_replace_24px - Background Blur icon

🖼️ Screenshots

Mask Post Process
Screenshot 2025-09-16 at 11 17 07 AM Screenshot 2025-09-16 at 11 19 03 AM

🚧 TODO

  • Fix bug where turning off background blur stops frames from being sent over WebRTC. ( it was a timestamp issue )
  • Optimize object creation, passing, and processing ( still opportunities for optimization, but trivial time savings )
  • Research GPU acceleration ( not maintainable, openGL ES has too high learning curve for future devs )
  • Bug on orientation change, the camera is flipped upside down, change the flip code on detection of landscape in onFrameCaptured
  • The blur background processor crops the Video Frame to square, look into preserving aspect ratio ( Not possible the Selfie Segmentation Model only outputs square or landscape )

🏁 Checklist

  • ⛑️ Tests (unit and/or integration) are included or not needed
  • 🔖 Capability is checked or not needed
  • 🔙 Backport requests are created or not needed: /backport to stable-xx.x
  • 📅 Milestone is set
  • 🌸 PR title is meaningful (if it should be in the changelog: is it meaningful to users?)

@rapterjet2004 rapterjet2004 self-assigned this Sep 16, 2025
@rapterjet2004 rapterjet2004 added the 2. developing Work in progress label Sep 16, 2025
@rapterjet2004 rapterjet2004 changed the title Implemented Background blurring 👤✨ Background blurring 👤✨ Sep 16, 2025
WIP - added CameraFrameProcessor.kt
WIP - finished frame processor
WIP - got blurring down, thanks to Gemini for the YUV format conversion. Need to review that file format, I'm only familiar with ARGB.
WIP - Got it working
WIP - Connected it to the UI
WIP - Fixed bug where turning off background blur stops frames from being sent over webRTC
WIP - Optimized Video Frame I420 Buffer to Matrix
WIP - Fixed rotation bug
WIP - Fixed Bug where stoping BackgroundBlurFrameProcessor.kt doesn't send any more frames over WebRTC. It was a timestamp issue
- Optimize object creation and passing and processing

Signed-off-by: rapterjet2004 <[email protected]>
@rapterjet2004 rapterjet2004 force-pushed the issue-5349-allow-background-blur branch from b63e1ab to 9b6c990 Compare September 25, 2025 17:20
@rapterjet2004 rapterjet2004 marked this pull request as ready for review September 25, 2025 17:21
Signed-off-by: rapterjet2004 <[email protected]>
@rapterjet2004 rapterjet2004 added 3. to review Waiting for reviews and removed 2. developing Work in progress labels Sep 29, 2025
@mahibi mahibi requested a review from sowjanyakch October 1, 2025 14:42
@sowjanyakch
Copy link
Contributor

Tested this PR.

  1. When background blur is enabled, the front camera is slightly zoomed compared to when blur is off.
  2. For audio-only calls, the background blur button should be hidden to avoid unnecessary resource usage. Wdyt?
  3. If you answer a video call, turn on background blur, and then stop your camera and then turn on camera, the person on the other end sees a blank screen. The video is displayed only a certain delay. (here delay is noticeable).
  4. If you answer a video call, turn off background blur, and then stop your camera and then turn on camera, the person on the other end sees a blank screen. The video is displayed only a certain delay. (here delay is small).

Copy link
Contributor

@sowjanyakch sowjanyakch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment #5387 (comment)

- Turns off background blur when camera is turned off

Signed-off-by: rapterjet2004 <[email protected]>
@rapterjet2004
Copy link
Contributor Author

rapterjet2004 commented Oct 8, 2025

  1. When background blur is enabled, the front camera is slightly zoomed compared to when blur is off.

Not possible to fix, as the Selfie Segmentation Model only outputs square or landscape. Besides that, I removed the option for audio only calls, and made sure to turn off background blur when the camera is off, to avoid errors with state.

Copy link
Contributor

github-actions bot commented Oct 8, 2025

APK file: https://www.kaminsky.me/nc-dev/android-artifacts/5387.apk

qrcode

To test this change/fix you can simply download above APK file and install and test it in parallel to your existing Nextcloud app.

Copy link
Contributor

github-actions bot commented Oct 8, 2025

Codacy

Lint

TypemasterPR
Warnings9999
Errors00

SpotBugs

CategoryBaseNew
Bad practice66
Correctness1111
Dodgy code6262
Internationalization33
Malicious code vulnerability33
Performance44
Security11
Total9090

Copy link
Contributor

@sowjanyakch sowjanyakch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you answer a video call, turn on background blur, and then stop your camera and then turn on camera, the person on the other end sees a blank screen. The video is displayed only a certain delay. (here delay is noticeable).

the above problem still exists for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3. to review Waiting for reviews

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow background blur during video calls

2 participants