Skip to content

Improve Windows Unicode I/O robustness, seam handling, and UI layout - #1823

Open
unclecatsjourney-maker wants to merge 1 commit into
hacksider:mainfrom
unclecatsjourney-maker:codex/windows-unicode-io-auto-seam-fix-ui
Open

Improve Windows Unicode I/O robustness, seam handling, and UI layout#1823
unclecatsjourney-maker wants to merge 1 commit into
hacksider:mainfrom
unclecatsjourney-maker:codex/windows-unicode-io-auto-seam-fix-ui

Conversation

@unclecatsjourney-maker

@unclecatsjourney-maker unclecatsjourney-maker commented May 15, 2026

Copy link
Copy Markdown

Summary

This PR improves Deep-Live-Cam stability and usability on Windows, especially for Unicode/long paths and seam artifacts during face swap.

What Changed

  1. Windows Unicode/long-path robustness
  • Added read_image / write_image helpers with Windows-safe fallback (np.fromfile + cv2.imdecode, cv2.imencode + file write).
  • Switched key image read/write call sites from raw cv2.imread/imwrite to safe helpers in:
    • modules/face_analyser.py
    • modules/processors/frame/core.py
    • modules/processors/frame/face_swapper.py
    • modules/processors/frame/face_enhancer.py
    • modules/processors/frame/face_enhancer_gpen256.py
    • modules/processors/frame/face_enhancer_gpen512.py
    • modules/ui.py
  1. Temp directory fallback for Windows
  • Added ASCII-safe temp path fallback in modules/utilities.py when path contains non-ASCII chars or is too long.
  • This prevents frame extraction/encoding failures in multilingual folder structures.
  1. Seam quality improvements
  • Added auto_seam_fix and face_smooth_alpha globals.
  • Added shaped soft alpha mask option (elliptical + blur) for cleaner edge blending.
  • Added temporal keypoint EMA smoothing for single-face tracking stability.
  1. Critical fix for “processing but no visible swap”
  • Fixed face keypoint smoothing to preserve insightface.app.common.Face type.
  • Prevents silent fallback to original frame caused by type degradation during smoothing.
  1. UI improvements
  • Added Auto Seam Fix switch and One-Click Final preset button.
  • Added current temp directory display in UI.
  • Adjusted lower UI layout to reduce overlap/crowding (enhancer row, sliders, action buttons, camera row, status area).

Validation

  • Local compile checks passed for modified Python modules (py_compile).
  • Verified branch behavior in real usage flow after the Face-type fix (swap no longer silently returns original output).

Notes

  • This PR contains 1 commit and touches 9 files.
  • Existing user local launcher/batch files are intentionally excluded from this PR.

Summary by Sourcery

Improve Windows file handling robustness, enhance seam quality and temporal stability in face swapping, and refine the desktop UI for better control and feedback.

New Features:

  • Add Unicode- and long-path-safe image read/write helpers and apply them across face analysis, swapping, enhancement, and UI preview flows.
  • Introduce an auto seam fix pipeline combining shaped soft alpha masks with temporal keypoint smoothing for cleaner single-face swaps.
  • Expose auto seam fix and a One-Click Final preset in the UI, along with a live display of the current temporary frame directory.

Bug Fixes:

  • Fix keypoint smoothing to preserve insightface Face semantics, preventing cases where swapping silently falls back to the original frame.
  • Add a Windows-specific ASCII-safe temp directory fallback to avoid frame extraction and encoding failures on long or non-ASCII paths.

Enhancements:

  • Refine CUDA and blending comments and behavior for clarity and consistency.
  • Adjust UI layout for lower controls, camera selection, and status labels to reduce overlap and make options more discoverable.

@sourcery-ai

sourcery-ai Bot commented May 15, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Improves Windows robustness for image I/O and temp directories, introduces configurable seam-fix and temporal keypoint smoothing in the face swapper, fixes a type regression that caused swaps to be skipped, and updates the UI to expose the new controls and show the temp directory path while slightly rearranging layout.

Sequence diagram for auto seam fix and keypoint smoothing in face swap

sequenceDiagram
    actor User
    participant UI as ui
    participant Globals as modules_globals
    participant FS as face_swapper
    participant Smooth as _smooth_face_kps
    participant Swap as swap_face
    participant Alpha as _get_soft_alpha

    User->>UI: Toggle Auto Seam Fix / One-Click Final
    UI->>Globals: set auto_seam_fix, face_smooth_alpha
    UI->>FS: process_frame_v2(temp_frame, temp_frame_path)

    FS->>Globals: read auto_seam_fix, many_faces
    FS->>FS: build source_target_pairs
    FS->>FS: compute smooth_single_target

    alt smooth_single_target is True
        FS->>Smooth: _smooth_face_kps(target_face)
        Smooth->>Globals: read face_smooth_alpha
        Smooth-->>FS: smoothed_face (Face preserved)
        FS->>Swap: swap_face(source_face, smoothed_face, current_swap_target)
    else
        FS->>Swap: swap_face(source_face, target_face, current_swap_target)
    end

    Swap->>Alpha: _get_soft_alpha(face_size)
    Alpha->>Globals: read auto_seam_fix
    Alpha-->>Swap: soft_alpha mask (elliptical or legacy)
    Swap-->>FS: swapped frame
Loading

File-Level Changes

Change Details Files
Introduce Windows-safe image read/write helpers and update callers to use them instead of raw OpenCV I/O.
  • Add read_image and write_image helpers that handle Unicode/long Windows paths with np.fromfile/imdecode and imencode/manual write fallbacks.
  • Ensure parent directories are created safely before writes and gracefully handle failures.
  • Replace cv2.imread/cv2.imwrite at key processing call sites (face analysis, swapper, frame processors, UI pipeline) with the new helpers.
modules/utilities.py
modules/face_analyser.py
modules/processors/frame/face_swapper.py
modules/processors/frame/face_enhancer.py
modules/processors/frame/face_enhancer_gpen256.py
modules/processors/frame/face_enhancer_gpen512.py
modules/processors/frame/core.py
modules/ui.py
Add Windows ASCII-safe temp directory strategy for problematic paths.
  • Introduce SAFE_TEMP_ROOT and helpers to detect non-ASCII characters and sanitize base names.
  • Change get_temp_directory_path to fall back to an ASCII-only, hashed subdirectory when the default temp path on Windows contains non-ASCII characters or is too long.
  • Expose temp directory path in the UI and keep it updated when the target changes.
modules/utilities.py
modules/ui.py
Improve seam quality via shaped soft alpha masks and temporal keypoint smoothing, and wire them into processing flows.
  • Extend paste cache to include auto_seam_fix and generate either an elliptical blurred mask or the legacy erode+blur mask based on a global flag.
  • Implement thread-safe EMA smoothing of Face.kps that preserves the insightface Face type and avoids dragging across large jumps.
  • Integrate keypoint smoothing and tracking reset into process_frame/process_frame_v2 and guard it so it only triggers in appropriate single-face cases.
modules/processors/frame/face_swapper.py
modules/globals.py
Expose new seam-related controls and presets in the UI and persist them across sessions.
  • Add global auto_seam_fix and face_smooth_alpha to saved switch state, with defaults matching processing logic.
  • Add an 'Auto Seam Fix' toggle and a 'One-Click Final' preset button that configure multiple globals (seam fix, smoothing, blending, opacity, sharpness, mouth mask).
  • Reposition buttons, sliders, camera controls, status bar, and add a temp-path label to reduce overlap and surface the temp directory.
modules/ui.py
modules/globals.py
Minor cleanup and consistency fixes in face swapper comments and behavior.
  • Standardize comment punctuation and wording (e.g., replacing em dashes with ASCII hyphens, adjusting x multipliers in comments).
  • Ensure keypoint tracking state is reset when opacity disables processing, many_faces mode is used, or no target face is found.
  • Preserve behavior of CUDA graph and paste-back paths while integrating the new smoothing and mask logic.
modules/processors/frame/face_swapper.py

Possibly linked issues

  • #(unlabeled): PR directly addresses Windows Unicode file path failures and fixes swap rendering artifacts like black squares/invisible swaps.

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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

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 utilities.read_image, you perform an extra np.fromfile/imdecode pass even after a normal cv2.imread fallback, including on non-Windows platforms; consider restricting the second raw-read attempt to the Windows/Unicode edge cases to avoid redundant disk I/O and make failures more transparent.
  • The new keypoint EMA smoothing maintains global state in _kps_smooth_state and is reset in several code paths; double-check whether there are any remaining flows (e.g., switching targets/sources in live or map mode) where _reset_kps_tracking should also be called to avoid cross-session smoothing artifacts.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `utilities.read_image`, you perform an extra `np.fromfile`/`imdecode` pass even after a normal `cv2.imread` fallback, including on non-Windows platforms; consider restricting the second raw-read attempt to the Windows/Unicode edge cases to avoid redundant disk I/O and make failures more transparent.
- The new keypoint EMA smoothing maintains global state in `_kps_smooth_state` and is reset in several code paths; double-check whether there are any remaining flows (e.g., switching targets/sources in live or map mode) where `_reset_kps_tracking` should also be called to avoid cross-session smoothing artifacts.

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