Deep-Live-Cam icon indicating copy to clipboard operation
Deep-Live-Cam copied to clipboard

Implement advanced color correction and Poisson blending.

Open netmirror-apple9 opened this issue 5 months ago • 1 comments

  • 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

netmirror-apple9 avatar Jun 24 '25 18:06 netmirror-apple9

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

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
    }

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

  • Contact our support team for questions or feedback.
  • Visit our documentation for detailed guides and information.
  • Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.

sourcery-ai[bot] avatar Jun 24 '25 18:06 sourcery-ai[bot]