Skip to content

Conversation

netmirror-apple9
Copy link

@netmirror-apple9 netmirror-apple9 commented Jun 24, 2025

  • Added histogram-based color correction to face_swapper.py for improved color matching of the swapped face to the target frame. Controlled by --color-correction.
  • Integrated Poisson blending (cv2.seamlessClone) for smoother face integration, reducing visible seams. Controlled by --poisson-blending.
  • Added global variables use_poisson_blending and poisson_blending_feather_amount and corresponding command-line arguments.
  • Refined argument parsing in core.py for new features.
  • Updated swap_face logic to incorporate these features and handle potential errors during blending.

Summary by Sourcery

Implement advanced color correction and Poisson blending options in the face swapper, including command-line controls, validation/fallback handling, and related helper logic

New Features:

  • Add optional histogram-based color correction to match the swapped face’s color distribution to the target frame
  • Add optional Poisson blending integration for seamless face insertion and reduced seams

Enhancements:

  • Improve swap_face workflow with validation and fallbacks for failed swaps and blending errors
  • Expose new flags (--color-correction, --poisson-blending) and globals with refined argument parsing and default settings

- Added histogram-based color correction to `face_swapper.py` for improved color matching of the swapped face to the target frame. Controlled by `--color-correction`.
- Integrated Poisson blending (`cv2.seamlessClone`) for smoother face integration, reducing visible seams. Controlled by `--poisson-blending`.
- Added global variables `use_poisson_blending` and `poisson_blending_feather_amount` and corresponding command-line arguments.
- Refined argument parsing in `core.py` for new features.
- Updated `swap_face` logic to incorporate these features and handle potential errors during blending.
Copy link
Contributor

sourcery-ai bot commented Jun 24, 2025

Reviewer's Guide

This PR enhances the face swapping pipeline by introducing optional histogram-based color correction and Poisson blending, updating the swap_face logic to apply region-aware adjustments, generate and feather blending masks, handle errors robustly, and expose these features via new CLI flags and globals.

Sequence diagram for face swapping with color correction and Poisson blending

sequenceDiagram
    participant User as actor User
    participant CLI as CLI Parser
    participant Core as core.py
    participant Globals as modules.globals
    participant Swapper as swap_face()
    participant Frame as Frame

    User->>CLI: Run with --color-correction and/or --poisson-blending
    CLI->>Core: parse_args()
    Core->>Globals: Set color_correction, use_poisson_blending
    Core->>Swapper: swap_face(source_face, target_face, temp_frame)
    Swapper->>Swapper: Apply face swap
    alt Color correction enabled
        Swapper->>Swapper: Apply histogram-based color correction
    end
    alt Poisson blending enabled
        Swapper->>Swapper: Generate/feather mask
        Swapper->>Swapper: Apply cv2.seamlessClone
    end
    Swapper->>Frame: Return blended frame
    Frame-->>Core: Frame with improved integration
Loading

Class diagram for updated swap_face logic and new color correction function

classDiagram
    class swap_face {
        +Frame swap_face(Face source_face, Face target_face, Frame temp_frame)
        +Handles: color correction, Poisson blending, error handling
    }
    class apply_histogram_matching_color_correction {
        +Frame apply_histogram_matching_color_correction(Frame source_img, Frame target_img)
        +Performs histogram matching on B, G, R channels
    }
    swap_face ..> apply_histogram_matching_color_correction : uses
    class modules.globals {
        +bool color_correction
        +bool use_poisson_blending
        +int poisson_blending_feather_amount
    }
Loading

File-Level Changes

Change Details Files
Validate face swap output and rename intermediate variable
  • Renamed swapped_frame to swapped_frame_result to clarify flow
  • Added None/type checks with logging and fallback to original frame on failure
modules/processors/frame/face_swapper.py
Implement histogram-based color correction
  • Conditionally apply color correction using target face bbox or full-frame fallback
  • Invoke apply_histogram_matching_color_correction on swapped region
modules/processors/frame/face_swapper.py
Integrate Poisson blending with mask generation
  • Build blending mask via landmarks convex hull or bbox fallback
  • Feather mask using Gaussian blur based on global feather amount
  • Compute blending center, normalize dtypes, call cv2.seamlessClone with error handling
modules/processors/frame/face_swapper.py
Refine process_frame color conversion logic
  • Removed global BGR-to-RGB conversion in process_frame
  • Shifted all color correction into swap_face
modules/processors/frame/face_swapper.py
Add histogram matching helper function
  • Introduced apply_histogram_matching_color_correction performing per-channel histogram matching
modules/processors/frame/face_swapper.py
Extend CLI and global flags for new features
  • Added --color-correction and --poisson-blending arguments in core.py
  • Mapped new args to modules.globals.color_correction, use_poisson_blending, and poisson_blending_feather_amount
  • Initialized new globals defaults in globals.py
modules/core.py
modules/globals.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

- Updated `face_swapper.py` to prioritize using the face bounding box (bbox) for creating the Poisson blending mask. This provides a tighter mask around the swapped face area, aiming to reduce artifacts near ears/hairline.
- Implemented fallback to landmark-based convex hull if bbox is invalid or unavailable.
- Added logging for mask generation issues and skipping blending if no valid mask can be created.
- Added functionality to preserve target's ears when using Poisson blending.
- Introduced new global variables in `globals.py`:
  - `preserve_target_ears` (boolean flag)
  - `ear_width_ratio`
  - `ear_height_ratio`
  - `ear_vertical_offset_ratio`
  - `ear_horizontal_overlap_ratio`
- Added command-line argument `--preserve-ears` to `core.py`.
- Modified `face_swapper.py` to adjust the Poisson blending mask by
  subtracting calculated ear regions if `preserve_target_ears` is true.
  This aims to reduce artifacts around ears by using the original target
  frame's ear content.
This commit includes:
- Refactored modules/ui.py: Live webcam preview loop now uses ROOT.after() for better UI responsiveness and potentially smoother external capture.
- Refactored modules/video_capture.py: Implemented threaded, non-blocking frame reads for improved camera capture performance and stability.
- Feature: Ear preservation for Poisson blending to reduce artifacts (controlled by --preserve-ears and related globals).
- Feature: Histogram-based color correction (--color-correction).
- Feature: Poisson blending for smoother face integration (--poisson-blending).

These changes aim to improve overall swap quality, UI stability, and address reported FPS issues with external capture tools like SplitCam.
…invalid.

- Modified `create_webcam_preview` in `modules/ui.py` to handle errors during source image loading (e.g., file not readable, no face detected) more gracefully.
- Instead of stopping the live preview, it now shows a warning message and starts the live camera feed without face swapping if the source image is unusable.
- This addresses the issue where clicking 'Live' after selecting a problematic source image would cause the preview to stop or crash immediately.
- Modified `update_webcam_frame_after` in `modules/ui.py` to use the
  source_image passed from `create_webcam_preview` without attempting
  to reload it on every frame if it was initially None.
- This fixes an FPS drop regression that occurred when starting the live
  preview with a problematic or non-existent source image.
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