Skip to content

fix: add missing logging import and fix invalid get_one_face call in face_swapper#1730

Open
JiayuuWang wants to merge 1 commit intohacksider:mainfrom
JiayuuWang:contribot/fix-face-swapper-logging-and-call
Open

fix: add missing logging import and fix invalid get_one_face call in face_swapper#1730
JiayuuWang wants to merge 1 commit intohacksider:mainfrom
JiayuuWang:contribot/fix-face-swapper-logging-and-call

Conversation

@JiayuuWang
Copy link
Copy Markdown
Contributor

@JiayuuWang JiayuuWang commented Apr 3, 2026

What

Two bugs in modules/processors/frame/face_swapper.py:

Bug 1 — Missing logging import causes NameError

In pre_check(), if os.makedirs() raises an OSError (e.g. permission denied), the code calls logging.error(...) but logging was never imported. This triggers a secondary NameError: name 'logging' is not defined, masking the original error and making it very hard to diagnose.

Fix: add import logging to the imports.

Bug 2 — get_one_face called with two arguments

In the live-stream fallback path of process_frame_v2 (line ~521):

target_face = get_one_face(processed_frame, detected_faces)

get_one_face only accepts one argument (frame), so this raises a TypeError at runtime. Faces have already been detected into detected_faces at this point, so there's no need to call the analyser again. The fix reuses the already-detected list with the same selection logic (min by bbox[0]) that get_one_face uses internally, avoiding both the crash and a redundant detection pass.

Test plan

  • Start the app in live/webcam mode with --map-faces omitted (simple mode), to exercise the fallback path in process_frame_v2.
  • Verify no TypeError is raised when a face is detected in the live feed.
  • Simulate a permission-denied scenario on the models directory and confirm the error is logged correctly instead of raising NameError.

🤖 Generated with Claude Code

Summary by Sourcery

Fix runtime errors in the face swapper fallback path and improve error logging for directory creation failures.

Bug Fixes:

  • Import the logging module so pre_check can log directory creation errors without raising a NameError.
  • Correct the live-stream fallback in process_frame_v2 to select a detected face directly instead of calling get_one_face with an invalid signature.

- Add missing `import logging` that caused a NameError when the models
  directory could not be created in `pre_check()`.
- Fix `get_one_face(processed_frame, detected_faces)` call in the live
  stream fallback path which incorrectly passed two arguments to a
  one-argument function. Replace with an inline min() over already-
  detected faces to match the original intent without a redundant
  detection pass.
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Apr 3, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Fixes two runtime errors in the face swapper: adds the missing logging import used in pre_check error handling, and replaces an invalid two-argument get_one_face call in the live-stream fallback path with direct selection from already-detected faces.

Sequence diagram for updated fallback face selection in process_frame_v2

sequenceDiagram
    actor LiveUser
    participant VideoStream
    participant FaceSwapper as process_frame_v2
    participant FaceDetector as detect_faces

    LiveUser ->> VideoStream: Provide_frame()
    VideoStream ->> FaceSwapper: process_frame_v2(temp_frame)

    FaceSwapper ->> FaceDetector: detect_faces(processed_frame)
    FaceDetector -->> FaceSwapper: detected_faces

    alt map_faces_provided
        FaceSwapper ->> FaceSwapper: build_source_target_pairs_with_mapping(detected_faces)
    else fallback_simple_mode
        FaceSwapper ->> FaceSwapper: source_face = default_source_face()
        FaceSwapper ->> FaceSwapper: target_face = min(detected_faces, key=bbox_left)
        FaceSwapper ->> FaceSwapper: append(source_face, target_face) to source_target_pairs
    end

    FaceSwapper -->> VideoStream: swapped_frame
    VideoStream -->> LiveUser: Display(swapped_frame)
Loading

Flow diagram for pre_check directory creation with logging

flowchart TD
    A[Start pre_check] --> B[Check models_directory exists]
    B -->|Exists| C[Continue initialization]
    B -->|Missing| D[Call os.makedirs on models_directory]
    D --> E{os.makedirs successful?}
    E -->|Yes| C
    E -->|OSError raised| F[logging.error with exception details]
    F --> G[Propagate or handle error appropriately]
    C --> H[End pre_check]
    G --> H
Loading

File-Level Changes

Change Details Files
Fix error logging in pre_check by importing the logging module used in the OSError handler.
  • Add an import for the logging module alongside the existing imports.
  • Ensure the OSError handling path in pre_check can call logging.error without raising NameError.
modules/processors/frame/face_swapper.py
Fix the live-stream fallback face selection logic that incorrectly called get_one_face with two arguments.
  • Replace the invalid get_one_face(processed_frame, detected_faces) call with selection of the left-most face from detected_faces using min(..., key=lambda x: x.bbox[0]).
  • Preserve behavior when no faces are detected by returning None for target_face, so the source_target_pairs list is only appended when both source and target faces exist.
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
Copy Markdown
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:

  • Consider extracting the face-selection logic (currently duplicated from get_one_face via min(detected_faces, key=lambda x: x.bbox[0])) into a shared helper so any future changes to the selection criteria only need to be updated in one place.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider extracting the face-selection logic (currently duplicated from get_one_face via min(detected_faces, key=lambda x: x.bbox[0])) into a shared helper so any future changes to the selection criteria only need to be updated in one place.

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