8000 refactor(face-swapper): extract pair-building helpers, reduce nesting by laurigates · Pull Request #1685 · hacksider/Deep-Live-Cam · GitHub
[go: up one dir, main page]

Skip to content

refactor(face-swapper): extract pair-building helpers, reduce nesting#1685

Open
laurigates wants to merge 2 commits intohacksider:mainfrom
laurigates:pr/refactor-pair-building
Open

refactor(face-swapper): extract pair-building helpers, reduce nesting#1685
laurigates wants to merge 2 commits intohacksider:mainfrom
laurigates:pr/refactor-pair-building

Conversation

@laurigates
Copy link
Contributor
@laurigates laurigates commented Feb 23, 2026

Summary

  • Extract face-pair building logic from process_frame_v2 into focused helpers:
    • _build_pairs_simple() — single-source mode (no map)
    • _build_pairs_mapped() — map-faces mode (image/video)
    • _build_pairs_live() — live/webcam mode with map
  • Significantly reduce nesting depth in the main processing function
  • Reuse already-detected faces in the fallback path instead of running redundant detection
  • Add USE_GLOBAL_CONSTANT flag for source face caching (from stacked PR refactor(globals): add named constants for magic numbers #1683)

Behavioral note: the helpers return empty pairs lists instead of early-returning from the
parent function, but the effect is identical — no swaps are performed when no pairs are found.

Stacked on #1683 (adds USE_GLOBAL_CONSTANT to globals).

Test plan

  • Verify face swapping works in all modes: simple, map-faces, live
  • Verify many-faces mode still works correctly
  • Test with multiple mapped faces in live mode
  • Confirm no redundant face detection calls in fallback path

Generated with Claude Code

Add FACE_CONFIDENCE_THRESHOLD, DETECTION_INTERVAL, DETECTION_CACHE_SIZE,
FPS_CAP, and MOUTH_FEATHER_RADIUS constants to replace scattered magic
numbers throughout the codebase.

Note: MAP_LOCK is handled separately in PR hacksider#1655

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@sourcery-ai
Copy link
Contributor
sourcery-ai bot commented Feb 23, 2026

Reviewer's Guide

Refactors face-swap pair selection by extracting dedicated helpers for file-based and live modes, centralizing detection timing constants in globals, and tightening synchronization around mapping structures.

Sequence diagram for live face-swap pair building with _build_pairs_live

sequenceDiagram
    participant Caller as process_frame_v2
    participant FS as _build_pairs_live
    participant GF as get_many_faces
    participant G as modules_globals
    participant FC as find_closest_centroid

    Caller->>FS: _build_pairs_live(processed_frame)
    FS->>GF: get_many_faces(processed_frame)
    GF-->>FS: detected_faces
    FS->>FS: check detected_faces is not empty

    FS->>G: acquire MAP_LOCK
    FS->>G: simple_map = getattr(simple_map)
    FS->>G: release MAP_LOCK

    alt many_faces enabled
        FS->>G: source_face = default_source_face()
        FS->>FS: for each target_face in detected_faces
        FS->>FS: append (source_face, target_face)
    else simple_map configured
        FS->>FS: source_faces = simple_map.source_faces
        FS->>FS: target_embeddings = simple_map.target_embeddings
        alt len(detected_faces) <= len(target_embeddings)
            loop for each detected_face
                FS->>FS: ensure detected_face.normed_embedding
                FS->>FC: find_closest_centroid(target_embeddings, detected_face.normed_embedding)
                FC-->>FS: closest_idx
                FS->>FS: append (source_faces[closest_idx], detected_face)
            end
        else len(detected_faces) > len(target_embeddings)
            FS->>FS: build detected_embeddings and detected_faces_with_embedding
            loop for i, target_embedding in target_embeddings
                FS->>FC: find_closest_centroid(detected_embeddings, target_embedding)
                FC-->>FS: closest_idx
                FS->>FS: append (source_faces[i], detected_faces_with_embedding[closest_idx])
            end
        end
    else no mapping configured
        FS->>G: source_face = default_source_face()
        FS->>GF: get_one_face(processed_frame)
        GF-->>FS: target_face
        FS->>FS: if source_face and target_face append pair
    end

    FS-->>Caller: source_target_pairs
Loading

Class diagram for refactored face_swapper helpers and globals constants

classDiagram
    class ModulesGlobals {
        <<module>>
        +float FACE_CONFIDENCE_THRESHOLD
        +float DETECTION_INTERVAL
        +int DETECTION_CACHE_SIZE
        +int FPS_CAP
        +int MOUTH_FEATHER_RADIUS
        +Lock MAP_LOCK
        +str target_path
        +bool many_faces
        +list source_target_map
        +dict simple_map
    }

    class FaceSwapperModule {
        <<module>>
        +deque FRAME_CACHE
        +dict FACE_DETECTION_CACHE
        +float LAST_DETECTION_TIME
        +int FRAME_SKIP_COUNTER
        +bool ADAPTIVE_QUALITY
        +get_faces_optimized(frame: Frame, use_cache: bool) List~Face~
        +process_frame(source_face: Face, temp_frame: Frame) Frame
        +process_frame_v2(temp_frame: Frame, temp_frame_path: str) Frame
        +_build_pairs_from_file_map(temp_frame_path: str) list
        +_build_pairs_live(processed_frame: Frame) list
    }

    class FaceDetectionHelpers {
        <<functions>>
        +get_many_faces(frame: Frame) list
        +get_one_face(frame: Frame) Face
        +find_closest_centroid(centroids: list, embedding: Any) tuple
        +default_source_face() Face
        +is_image(path: str) bool
        +is_video(path: str) bool
    }

    ModulesGlobals <.. FaceSwapperModule : uses
    FaceDetectionHelpers <.. FaceSwapperModule : calls

    class get_faces_optimized {
        +frame: Frame
        +use_cache: bool
        +return List~Face~
    }

    FaceSwapperModule o-- get_faces_optimized

    class _build_pairs_from_file_map {
        +temp_frame_path: str
        +return list
    }

    class _build_pairs_live {
        +processed_frame: Frame
        +return list
    }

    FaceSwapperModule o-- _build_pairs_from_file_map
    FaceSwapperModule o-- _build_pairs_live

    class process_frame_v2 {
        +temp_frame: Frame
        +temp_frame_path: str
        +return Frame
    }

    FaceSwapperModule o-- process_frame_v2

    ModulesGlobals --> _build_pairs_from_file_map : provides_maps
    ModulesGlobals --> _build_pairs_live : provides_maps
    FaceDetectionHelpers --> _build_pairs_live : detection_and_matching
    FaceDetectionHelpers --> _build_pairs_from_file_map : file_type_checks
Loading

File-Level Changes

Change Details Files
Extract helper functions to build source/target face pairs and simplify process_frame_v2 control flow.
  • Introduce _build_pairs_from_file_map(temp_frame_path) to construct (source_face, target_face) pairs from source_target_map for image/video targets, handling both many_faces and single-face modes.
  • Introduce _build_pairs_live(processed_frame) to construct (source_face, target_face) pairs for live/webcam mode using get_many_faces, simple_map, and default_source_face fallbacks.
  • Replace the inlined mapping logic in process_frame_v2 with calls to the new helpers, reducing nesting and keeping process_frame_v2 focused on orchestration and swapping.
modules/processors/frame/face_swapper.py
Use shared detection and processing constants from globals instead of local definitions.
  • Remove local DETECTION_INTERVAL constant from face_swapper and reference modules.globals.DETECTION_INTERVAL in get_faces_optimized to control cache TTL / detection rate.
  • Add DETECTION_INTERVAL and other related processing constants (FACE_CONFIDENCE_THRESHOLD, DETECTION_CACHE_SIZE, FPS_CAP, MOUTH_FEATHER_RADIUS) to modules/globals.py for centralized configuration.
modules/processors/frame/face_swapper.py
modules/globals.py
Improve thread safety when accessing global face-mapping structures.
  • In _build_pairs_from_file_map, snapshot modules.globals.source_target_map under MAP_LOCK before iterating to avoid concurrent modification issues.
  • In _build_pairs_live, snapshot modules.globals.simple_map under MAP_LOCK before using it to build live mapping pairs.
modules/processors/frame/face_swapper.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

Copy link
Contributor
@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • In _build_pairs_from_file_map, the image/video handling logic is almost duplicated between the many_faces and non-many_faces branches; consider extracting the shared inner loops into a small helper to avoid divergence between the two paths over time.
  • In the live-stream fallback path, the previous implementation called get_one_face(processed_frame, detected_faces) to reuse already-detected faces, while the new _build_pairs_live calls get_one_face(processed_frame) without passing detected_faces; it may be worth checking whether this reintroduces extra detection work or changes behavior and, if so, preserving the prior call pattern.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `_build_pairs_from_file_map`, the image/video handling logic is almost duplicated between the `many_faces` and non-`many_faces` branches; consider extracting the shared inner loops into a small helper to avoid divergence between the two paths over time.
- In the live-stream fallback path, the previous implementation called `get_one_face(processed_frame, detected_faces)` to reuse already-detected faces, while the new `_build_pairs_live` calls `get_one_face(processed_frame)` without passing `detected_faces`; it may be worth checking whether this reintroduces extra detection work or changes behavior and, if so, preserving the prior call pattern.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

…nstants

- Extract _build_pairs_from_file_map and _build_pairs_live from
  process_frame_v2, reducing nesting from 7 levels to 1
- Remove local DETECTION_INTERVAL; use modules.globals.DETECTION_INTERVAL
- Both helpers snapshot maps under MAP_LOCK before iterating

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@laurigates laurigates force-pushed the pr/refactor-pair-building branch from 0a22b62 to c754e76 Compare February 23, 2026 15:19
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

0