Skip to content

Feature/bg swap 15383030312325891351 - #1976

Closed
art-e-ui wants to merge 4 commits into
hacksider:mainfrom
art-e-ui:feature/bg-swap-15383030312325891351
Closed

art-e-ui wants to merge 4 commits into
hacksider:mainfrom
art-e-ui:feature/bg-swap-15383030312325891351

Conversation

@art-e-ui

@art-e-ui art-e-ui commented Nov 6, 2026 •

Copy link
Copy Markdown

Summary by Sourcery

Add background replacement with parallax effects and virtual-camera output across processing, live preview, and user interfaces.

New Features:

  • Add portrait-matting background replacement with 3D perspective and parallax adjustment for images, videos, and live previews.
  • Add live virtual-camera output with selectable devices for routing processed webcam frames to other applications.
  • Expose background selection, background-swap controls, and virtual-camera settings in the UI and command-line interface.

Enhancements:

  • Persist the new background-swap and virtual-camera settings across sessions and automatically manage their active frame processors.

Build:

  • Add the pyvirtualcam dependency and register the portrait-matting model for download.

Tests:

  • Add coverage for background compositing, viewer-pose estimation, perspective rendering, and virtual-camera lifecycle and error handling.

google-labs-jules Bot and others added 4 commits November 6, 2026 08:59
- Implement background swapper frame processor using ONNX portrait matting
- Add 3D perspective and translation parallax warping based on viewer point of view
- Add background selection UI and toggle in PySide6 main window
- Support background swap in live webcam preview, offline video/image processing, and CLI
- Add unit tests for perspective rendering and blending

Co-authored-by: art-e-ui <269083712+art-e-ui@users.noreply.github.com>
Co-authored-by: art-e-ui <269083712+art-e-ui@users.noreply.github.com>
- Add pyvirtualcam integration to stream processed live frames
- Add Virtual Camera toggle switch and device selector in UI
- Add --virtual-camera and --virtual-camera-device CLI flags
- Add unit tests for virtual camera manager

Co-authored-by: art-e-ui <269083712+art-e-ui@users.noreply.github.com>
- Add background swapper frame processor using ONNX portrait matting
- Implement 3D perspective and translation parallax homography based on viewer point of view
- Add pyvirtualcam virtual camera output for live preview
- Add UI controls for background selection, background swap toggle, virtual camera toggle, and device selector
- Add CLI options -b/--background, --background-swap, --virtual-camera, and --virtual-camera-device
- Add comprehensive unit tests and pass ruff lint checks

Co-authored-by: art-e-ui <269083712+art-e-ui@users.noreply.github.com>
@sourcery-ai

sourcery-ai Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

This PR adds an end-to-end background-swap processor based on MODNet segmentation and pose-responsive perspective/parallax compositing, exposes it through CLI and UI controls, and adds optional pyvirtualcam output for processed live video with device selection, persistence, lifecycle management, and tests.

Sequence diagram for background swapping a processed frame

sequenceDiagram
    participant Input as Input Frame
    participant Processor as background_swapper
    participant Face as get_one_face
    participant Matte as predict_matte
    participant Model as MODNet ONNX
    participant Background as get_background_image
    participant Warp as render_3d_perspective_background

    Input->>Processor: swap_background(frame, target_face)
    Processor->>Background: get_background_image()
    Processor->>Face: get_one_face(frame)
    Processor->>Processor: compute_viewer_pov(face, frame_shape)
    Processor->>Warp: render_3d_perspective_background(background, shape, dx, dy, yaw, pitch, roll)
    Processor->>Matte: predict_matte(frame)
    Matte->>Model: sess.run(input)
    Model-->>Matte: portrait matte
    Matte-->>Processor: alpha mask
    Processor-->>Input: alpha-composited frame
Loading

Sequence diagram for routing live frames to a virtual camera

sequenceDiagram
    participant Capture as Live Capture
    participant Preview as Webcam Preview
    participant Manager as VIRTUAL_CAM
    participant Device as Virtual Camera Device

    Preview->>Manager: start(width, height, fps, device)
    Manager->>Device: pyvirtualcam.Camera(...)
    Device-->>Manager: camera handle
    Capture->>Preview: processed BGR frame
    Preview->>Manager: send_bgr_frame(raw_bgr_frame)
    Manager->>Manager: cv2.cvtColor(bgr_frame, BGR2RGB)
    Manager->>Device: send(rgb_frame)
    Device-->>Preview: live virtual-camera output
    Preview->>Manager: stop()
    Manager->>Device: close()
Loading

Flow diagram for enabling background swap

flowchart LR
    CLI[CLI or UI background selection] --> State[background_path and background_swap]
    State --> ProcessorList[frame_processors]
    ProcessorList --> BackgroundSwapper[background_swapper]
    BackgroundSwapper --> PreCheck[pre_check]
    PreCheck --> Model[MODNet model]
    BackgroundSwapper --> Process[process_frame or process_image]
    Process --> Output[Processed image or video frame]
Loading

File-Level Changes

Change Details Files
Adds configurable background replacement using portrait matting and pose-driven parallax rendering.
  • Introduces a background swap frame processor with MODNet model acquisition, ONNX inference, background caching, temporal POV smoothing, perspective warping, and alpha compositing.
  • Registers the processor in the frame-processing pipeline and synchronizes it with CLI and UI toggle state.
  • Adds background image selection, preview, persistence, and command-line options.
  • Adds the MODNet model to the downloader manifest.
modules/core.py
modules/globals.py
modules/model_downloader.py
modules/processors/frame/background_swapper.py
modules/processors/frame/core.py
modules/ui.py
tests/test_background_swapper.py
Adds virtual webcam output for processed live camera frames.
  • Adds a pyvirtualcam-backed singleton manager with platform device discovery, RGB conversion, lifecycle handling, and resolution changes.
  • Adds CLI and persisted UI controls for enabling virtual-camera output and selecting a device.
  • Starts, feeds, and stops the virtual camera from the live preview lifecycle while retaining local preview rendering.
  • Adds the pyvirtualcam dependency and manager tests.
modules/core.py
modules/globals.py
modules/ui.py
modules/virtual_camera.py
requirements.txt
tests/test_virtual_camera.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@art-e-ui art-e-ui closed this Nov 6, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="modules/virtual_camera.py" line_range="47-48" />
<code_context>
+        system = platform.system()
+        if system == "Linux":
+            # Search for v4l2 video loopback devices
+            for dev in sorted(glob.glob("/dev/video*")):
+                devices.append(dev)
+        elif system == "Windows":
+            # Common Windows virtual camera driver names
</code_context>
<issue_to_address>
**Physical cameras appear as outputs**

When a Linux system has ordinary cameras alongside virtual-camera loopbacks, `get_available_devices` lists every `/dev/video*` node, including capture-only cameras. Selecting one passes it to `pyvirtualcam.Camera`, which cannot start output on it.

Filter Linux device discovery to include only virtual-camera loopback devices.

Also at `modules/virtual_camera.py:73-74`.
</issue_to_address>

### Comment 2
<location path="tests/test_virtual_camera.py" line_range="38" />
<code_context>
+        frame[:] = (255, 0, 0)  # BGR blue
+        sent = self.manager.send_bgr_frame(frame)
+        self.assertTrue(sent)
+        self.assertTrue(mock_cam.send.called)
+
+        # Stop camera
</code_context>
<issue_to_address>
**Sent frame color can be wrong**

When `send_bgr_frame` sends frames without converting BGR to RGB, `test_start_and_send_frame` only checks that `mock_cam.send` was called, so a broken BGR-to-RGB conversion still passes; the test does not establish that the virtual camera receives correctly ordered color channels.

Inspect the array passed to `mock_cam.send` and assert its channel values are in RGB order.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 2 findings to address first, and when enabled, processed live camera frames are sent to a virtual camera that other applications can consume or record; stopping or reverting the feature cannot recall frames already captured. The new pyvirtualcam dependency also adds a third-party runtime component.

Blocking findings: modules/virtual_camera.py:48, tests/test_virtual_camera.py:38


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread modules/virtual_camera.py
Comment on lines +47 to +48
for dev in sorted(glob.glob("/dev/video*")):
devices.append(dev)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Medium · Physical cameras appear as outputs

When a Linux system has ordinary cameras alongside virtual-camera loopbacks, get_available_devices lists every /dev/video* node, including capture-only cameras. Selecting one passes it to pyvirtualcam.Camera, which cannot start output on it.

Filter Linux device discovery to include only virtual-camera loopback devices.

Also at modules/virtual_camera.py:73-74.

Prompt for AI agents
In `modules/virtual_camera.py` at lines 47-48:

**Physical cameras appear as outputs**

When a Linux system has ordinary cameras alongside virtual-camera loopbacks, `get_available_devices` lists every `/dev/video*` node, including capture-only cameras. Selecting one passes it to `pyvirtualcam.Camera`, which cannot start output on it.

Filter Linux device discovery to include only virtual-camera loopback devices.

Also at `modules/virtual_camera.py:73-74`.

frame[:] = (255, 0, 0) # BGR blue
sent = self.manager.send_bgr_frame(frame)
self.assertTrue(sent)
self.assertTrue(mock_cam.send.called)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Medium · Sent frame color can be wrong

When send_bgr_frame sends frames without converting BGR to RGB, test_start_and_send_frame only checks that mock_cam.send was called, so a broken BGR-to-RGB conversion still passes; the test does not establish that the virtual camera receives correctly ordered color channels.

Inspect the array passed to mock_cam.send and assert its channel values are in RGB order.

Prompt for AI agents
In `tests/test_virtual_camera.py` at line 38:

**Sent frame color can be wrong**

When `send_bgr_frame` sends frames without converting BGR to RGB, `test_start_and_send_frame` only checks that `mock_cam.send` was called, so a broken BGR-to-RGB conversion still passes; the test does not establish that the virtual camera receives correctly ordered color channels.

Inspect the array passed to `mock_cam.send` and assert its channel values are in RGB order.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant