Skip to content

perf: use cached face detection in map-faces live mode#1681

Open
laurigates wants to merge 1 commit intohacksider:mainfrom
laurigates:pr/perf-cached-map-faces
Open

perf: use cached face detection in map-faces live mode#1681
laurigates wants to merge 1 commit intohacksider:mainfrom
laurigates:pr/perf-cached-map-faces

Conversation

@laurigates
Copy link
Contributor

@laurigates laurigates commented Feb 22, 2026

Summary

Switch process_frame_v2() live path from get_many_faces() to get_faces_optimized() so map-faces live mode benefits from time-based detection caching, matching the simple-mode live path.

get_faces_optimized() returns cached results when called within the detection interval (~33ms), avoiding redundant face detection on consecutive frames. This was already used in the simple (non-map) live mode but the map-faces live branch was still calling get_many_faces() directly.

Files changed

  • modules/processors/frame/face_swapper.py — 1 line: get_many_facesget_faces_optimized in the live stream branch of process_frame_v2()

Inspired by #1617.

Test plan

  • Run live webcam with map-faces mode enabled (multiple source faces mapped to targets)
  • Verify face detection caching is active (reduced CPU/GPU usage for detection)
  • Confirm face swapping still works correctly with cached detection results
  • Test with single-face and multi-face scenarios

🤖 Generated with Claude Code

Summary by Sourcery

Enhancements:

  • Route live map-faces processing through the optimized face detection function to reuse recent detection results and reduce redundant work.

Switch process_frame_v2() live path from get_many_faces() to
get_faces_optimized() so map-faces live mode benefits from time-based
detection caching, matching the simple-mode live path.

get_faces_optimized() returns cached results when called within the
detection interval (~33ms), avoiding redundant face detection on
consecutive frames. This was already used in the simple (non-map)
live mode but the map-faces live branch was still calling
get_many_faces() directly.

Inspired by hacksider#1617.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Feb 22, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Switches live map-faces processing in process_frame_v2() to use the cached face detection path so live mode benefits from time-based detection caching, aligning behavior with the simple live mode path.

Sequence diagram for cached face detection in live map-faces processing

sequenceDiagram
    actor User
    participant LiveStream as LiveStreamLoop
    participant FaceSwapper as process_frame_v2
    participant Detector as get_faces_optimized
    participant Cache as DetectionCache

    User ->> LiveStream: start_live_map_faces()
    loop For_each_frame
        LiveStream ->> FaceSwapper: process_frame_v2(frame)
        FaceSwapper ->> Detector: get_faces_optimized(processed_frame)
        Detector ->> Cache: lookup(processed_frame_timestamp)
        alt cache_hit
            Cache -->> Detector: cached_detected_faces
            Detector -->> FaceSwapper: detected_faces
        else cache_miss
            Detector ->> Detector: run_face_detection()
            Detector ->> Cache: store(results, timestamp)
            Detector -->> FaceSwapper: detected_faces
        end
        FaceSwapper ->> FaceSwapper: map_faces_and_swap(detected_faces)
        FaceSwapper -->> LiveStream: swapped_frame
    end
    LiveStream -->> User: display_swapped_video
Loading

Flow diagram for live path in process_frame_v2 using get_faces_optimized

flowchart TD
    A[start_process_frame_v2] --> B{mode}
    B -->|image_or_video| C[analyze_precomputed_faces]
    C --> Z[end_process_frame_v2]

    B -->|live_or_webcam| D[preprocess_frame]
    D --> E[get_faces_optimized with processed_frame]
    E --> F{detected_faces_present}

    F -->|no| Z

    F -->|yes and many_faces_flag| G[default_source_face]
    G --> H[apply_face_swaps_to_all_detected_targets]
    H --> Z

    F -->|yes and not many_faces_flag| I[select_single_source_face]
    I --> J[apply_face_swap_to_primary_target]
    J --> Z[end_process_frame_v2]
Loading

File-Level Changes

Change Details Files
Use cached, time-optimized face detection for live/map-faces processing instead of uncached multi-face detection.
  • Replace get_many_faces() with get_faces_optimized() in the live stream branch of process_frame_v2() so live map-faces mode reuses recent detection results within the configured interval.
  • Ensure that map-faces live behavior aligns with the existing simple live mode, reducing redundant detections and CPU/GPU usage while preserving face swapping behavior.
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 reviewed your changes and they look great!


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.

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