This commit incorporates fixes based on a detailed code review, addressing several critical and code quality issues to improve application stability and maintainability.
Key changes include:
modules/hair_segmenter.py:
- Removed a redundant model initialization check to streamline logic.
modules/processors/frame/face_swapper.py:
- Added try-except error handling around calls to segment_hair(), create_face_mask(), and apply_color_transfer() to prevent crashes during the hair swapping process and allow for graceful fallbacks.
- Ensured safer access to the global 'enable_hair_swapping' flag by using getattr(), providing a default value to prevent AttributeErrors.
- Verified that a previously flagged redundant image read in process_image() was already addressed.
modules/ui.py:
- Corrected the function call signature for the Face Enhancer's process_frame_v2 method within the webcam preview when map_faces mode is active.
- Made the conditional check for source image objects (source_face_obj_for_cam and source_frame_full_for_cam) in the webcam preview more explicit.
- Reviewed and confirmed that other reported code quality issues (like a redundant conditional for the face enhancer toggle and webcam error handling consistency) were either not present in the current codebase or already adequately handled by previous modifications.
These changes aim to make the application more robust before tackling further functional improvements and performance optimizations for the hair swapping feature.
This commit introduces automation scripts for Windows users and updates the README.md accordingly.
New/Modified Windows Scripts:
- setup_windows.bat: New script to automate Python checks, ffmpeg warning, virtual environment (.venv) creation, pip upgrade, and dependency installation.
- run_windows.bat: New script to run the application with CPU execution provider by default, activating .venv.
- run-cuda.bat: Updated to use .venv and pass arguments.
- run-directml.bat: Updated to use .venv and pass arguments.
README.md Changes:
- Updated the "For Windows:" section under "Installation (Manual)" to detail the new automated setup using setup_windows.bat and the revised run scripts.
- Recommended Python 3.10 for Windows for best compatibility.
- Provided updated manual setup notes for Windows, including a PowerShell command for ffmpeg installation and using .venv for consistency.
- Ensured the general Python recommendation in the manual setup prerequisites also mentions Python 3.10.
I modified `modules/video_capture.py` so that it will explicitly try using `cv2.CAP_AVFOUNDATION` when initializing `cv2.VideoCapture` on macOS. If AVFoundation fails to open the camera, it will then fall back to the default OpenCV backend.
This adjustment should improve camera compatibility and stability on macOS, especially in situations where the default backend might not be working as expected.
Replaces Python 3.10+ type hint syntax (e.g., Frame | None)
with Python 3.9 compatible syntax (e.g., Optional[Frame])
in modules/processors/frame/face_swapper.py.
This resolves a TypeError encountered when running on Python 3.9.
Specifically, the return type of _prepare_warped_source_material_and_mask
was updated.
Removes a duplicated 'if not modules.globals.map_faces:' line within the create_webcam_preview function. This error was causing a syntax issue and preventing the application from running correctly, particularly in webcam mode.
This commit introduces shell scripts to automate the setup process and provide convenient ways to run the application on macOS.
New files added:
- setup_mac.sh: Checks for Python 3.9+ and ffmpeg, creates a virtual environment, installs pip dependencies from requirements.txt.
- run_mac.sh: Runs the application with the CPU execution provider by default.
- run_mac_cpu.sh: Explicitly runs with the CPU execution provider.
- run_mac_coreml.sh: Runs with the CoreML execution provider.
- run_mac_mps.sh: Runs with the MPS execution provider.
The README.md has also been updated with a new section detailing how to use these scripts for macOS users.
These scripts aim to simplify the initial setup and execution of the project on macOS, similar to the .bat files available for Windows.
This commit introduces the capability to swap hair along with the face from a source image to a target image/video or live webcam feed.
Key changes include:
1. **Hair Segmentation:**
- Integrated the `isjackwild/segformer-b0-finetuned-segments-skin-hair-clothing` model from Hugging Face using the `transformers` library.
- Added `modules/hair_segmenter.py` with a `segment_hair` function to produce a binary hair mask from an image.
- Updated `requirements.txt` with `transformers`.
2. **Combined Face-Hair Mask:**
- Implemented `create_face_and_hair_mask` in `modules/processors/frame/face_swapper.py` to generate a unified mask for both face (from landmarks) and segmented hair from the source image.
3. **Enhanced Swapping Logic:**
- Modified `swap_face` and related processing functions (`process_frame`, `process_frame_v2`, `process_frames`, `process_image`) to utilize the full source image (`source_frame_full`).
- The `swap_face` function now performs the standard face swap and then:
- Segments hair from the `source_frame_full`.
- Warps the hair and its mask to the target face's position using an affine transformation estimated from facial landmarks.
- Applies color correction (`apply_color_transfer`) to the warped hair.
- Blends the hair onto the target frame, preferably using `cv2.seamlessClone` for improved realism.
- Existing mouth mask logic is preserved and applied to the final composited frame.
4. **Webcam Integration:**
- Updated the webcam processing loop in `modules/ui.py` (`create_webcam_preview`) to correctly load and pass the `source_frame_full` to the frame processors.
- This enables hair swapping in live webcam mode.
- Added error handling for source image loading in webcam mode.
This set of changes addresses your request for more realistic face swaps that include hair. Further testing and refinement of blending parameters may be beneficial for optimal results across all scenarios.