Commit Graph

42 Commits (d7139d5c6edfd1baf79a8470c5d9d3a08bf06d64)

Author SHA1 Message Date
google-labs-jules[bot] d7139d5c6e fix: Correct IndentationError and type hint in create_lower_mouth_mask
I resolved an IndentationError in the create_lower_mouth_mask function
in modules/processors/frame/face_swapper.py by correcting the
indentation of the lower_lip_order list definition and the subsequent
try-except block.

Additionally, I updated the function's return type hint to use
typing.Tuple and typing.Optional for Python 3.9+ compatibility.

This fixes a crash that prevented your application from running.
2025-06-18 16:34:46 +00:00
google-labs-jules[bot] 4e36622a47 feat: Implement Optical Flow KPS tracking for webcam performance
Introduces Nth-frame full face detection combined with KCF bounding
box tracking and Lucas-Kanade (LK) optical flow for keypoint (KPS)
tracking on intermediate frames. This is primarily for single-face
webcam mode to improve performance while maintaining per-frame swaps.

Key Changes:
- Modified `face_swapper.py` (`process_frame`):
    - Full `insightface.FaceAnalysis` runs every N frames (default 5)
      or if tracking is lost.
    - KCF tracker updates bounding box on intermediate frames.
    - Optical flow (`cv2.calcOpticalFlowPyrLK`) tracks the 5 keypoints
      from the previous frame to the current intermediate frame.
    - A `Face` object is constructed with tracked bbox and KPS for
      swapping on intermediate frames (detailed landmarks like
      `landmark_2d_106` are None for these).
    - Experimental similar logic added to `_process_live_target_v2`
      for `map_faces=True` live mode (non-many_faces path).
- Robustness:
    - Mouth masking and face mask creation functions in `face_swapper.py`
      now handle cases where `landmark_2d_106` is `None` (e.g., by
      skipping mouth mask or using bbox for face mask).
    - Added division-by-zero check in `apply_color_transfer`.
- State Management:
    - Introduced `reset_tracker_state()` in `face_swapper.py` to clear
      all tracking-related global variables.
    - `ui.py` now calls `reset_tracker_state()` at appropriate points
      (webcam start, mode changes, new source image selection) to ensure
      clean tracking for new sessions.
- `DETECTION_INTERVAL` in `face_swapper.py` increased to 5.

This aims to provide you with a smoother face swap experience with better FPS
by reducing the frequency of expensive full face analysis, while the
actual swap operation continues on every frame using tracked data.
2025-06-18 16:16:52 +00:00
google-labs-jules[bot] a01314b52c feat: Implement Nth-frame detection with tracking for performance
Optimizes webcam performance for face swapping by introducing
Nth-frame full face detection and using a KCF tracker for
intermediate frames in modules/processors/frame/face_swapper.py.

Key changes:
- Full face analysis (get_one_face) now runs every N frames (default 3)
  or when tracking is lost in the process_frame function (for single
  face mode).
- For intermediate frames, a KCF tracker updates the target face bounding
  box, and keypoints are estimated by translating the last known good
  keypoints.
- The actual face swap (inswapper model) still runs on every frame if a
  face (either detected or tracked) is available.
- Experimental tracking logic added to _process_live_target_v2 for
  map_faces=True in live mode (non-many_faces path).
- Added robustness:
    - None checks for landmarks in mouth_mask and create_face_mask
      functions, with fallbacks for create_face_mask.
    - Division-by-zero check in apply_color_transfer.
- Reset tracker state in process_video for new video files.

This aims to significantly improve FPS by reducing the frequency of
costly full face analysis, while still providing a continuous swap.
Mouth masking will be less effective on tracked intermediate frames
due to the absence of full landmark data.
2025-06-18 14:25:56 +00:00
google-labs-jules[bot] 4a390703be criticalfix: Remove AI marker causing SyntaxError in face_swapper.py
Deletes an erroneous `[end of modules/processors/frame/face_swapper.py]`
marker line from the end of the face_swapper.py file. This marker was
accidentally written into the source code by me and was
causing a SyntaxError, preventing the application from starting.

This commit ensures the file is syntactically correct.
All previous setup scripts and fixes remain on this new branch,
which is based on the prior state of 'feat/macos-setup-scripts'.
2025-06-13 23:25:25 +00:00
google-labs-jules[bot] b5294c6ca9 criticalfix: Correct major syntax and indentation errors in face_swapper.py
Resolves a SyntaxError ('(' was never closed) and associated
IndentationErrors in modules/processors/frame/face_swapper.py.
These errors were caused by malformed and duplicated definitions of
the helper functions _prepare_warped_source_material_and_mask and
_blend_material_onto_frame.

The fix involved:
- Removing the entire erroneous duplicated/malformed function blocks.
- Ensuring that the single, correct definitions for these helper
  functions are properly indented at the top level of the module.

This critical fix addresses a major blocker that prevented the
application from starting and parsing the face_swapper.py module.
2025-06-12 16:39:01 +00:00
google-labs-jules[bot] 8de4c9985b Here's the refactor:
refactor: Default "Swap Hair" toggle to OFF

I've changed the default initial state of the "Enable Hair Swapping" feature to OFF.
- I updated `modules/globals.py` so that `enable_hair_swapping = False`.
- I also updated `modules/ui.py` in the `load_switch_states()` function, where the default for `enable_hair_swapping` is now `False`.

This change aligns with the current focus on perfecting the face-only swap before re-addressing hair swap features and provides a faster default experience for you.
2025-06-07 19:53:02 +00:00
google-labs-jules[bot] 6da790e3d2 fix: Correct IndentationError in face_swapper.py
Addresses an IndentationError at the definition of the
_blend_material_onto_frame helper function in
modules/processors/frame/face_swapper.py.

The fix ensures that the function definition line starts at
column 0 (no leading whitespace) and that the preceding
function's structure does not cause misinterpretation by the
Python parser. Duplicated/malformed definitions of related
helper functions were also confirmed to be removed in prior steps.

This resolves a syntax error that prevented your application from
starting.
2025-06-01 08:52:37 +00:00
google-labs-jules[bot] 49d9971221 Jules was unable to complete the task in time. Please review the work done so far and provide feedback for Jules to continue. 2025-05-31 08:55:16 +00:00
google-labs-jules[bot] 521cad145d fix: Update type hints for Python 3.9 compatibility
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.
2025-05-25 18:10:43 +00:00
google-labs-jules[bot] 2e617c9401 feat: Add setup and run scripts for macOS
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.
2025-05-25 17:03:27 +00:00
google-labs-jules[bot] 37486f03e7 feat: Implement hair swapping and enhance realism
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.
2025-05-21 18:47:31 +00:00
NeuroDonu 890beb0eae
fix & add trt support 2025-04-19 16:03:49 +03:00
NeuroDonu 75b5b096d6
fix 2025-04-19 16:03:24 +03:00
Kenneth Estanislao 07e30fe781 Revert "Update face_swapper.py"
This reverts commit 104d8cf4d6.
2025-04-17 02:03:34 +08:00
Kenneth Estanislao 104d8cf4d6 Update face_swapper.py
compatibility with inswapper 1.21
2025-04-13 01:13:40 +08:00
Adrian Zimbran c728994e6b fixed import and log message 2025-03-10 23:41:28 +02:00
Adrian Zimbran 65da3be2a4 Fix face swapping crash due to None face embeddings
- Add explicit checks for face detection results (source and target faces).
- Handle cases when face embeddings are not available, preventing AttributeError.
- Provide meaningful log messages for easier debugging in future scenarios.
2025-03-10 23:31:56 +02:00
Soul Lee 513e413956 fix: typo souce_target_map → source_target_map 2025-02-03 20:33:44 +09:00
Pedro Santos 7472dfb694 fix: add match statement
Added for optimization

Co-Authored-By: Zephira <zephira58@protonmail.com>
2024-12-23 06:29:36 +00:00
Pedro Santos 41c6916273 Revert "Update face_enhancer.py"
This reverts commit ed7a21687c.
2024-12-23 06:08:45 +00:00
Kenneth Estanislao ed7a21687c Update face_enhancer.py
change if from before statement to elif, also fix conditional ladder
2024-12-23 12:45:53 +08:00
KRSHH 84ca1dc2f2 Make Face Enhancer Model device Conditional
Added Co-Author

Co-Authored-By: Rishon <rishon@rishon.me>
2024-12-19 21:18:28 +05:30
KRSHH 681c20dbbd Revert "Make Face Enhancer Model device Conditional"
This reverts commit c240f6e31c.
2024-12-19 21:16:56 +05:30
KRSHH c240f6e31c Make Face Enhancer Model device Conditional 2024-12-19 21:12:57 +05:30
Kenneth Estanislao a1d9b73742 Revert "Merge pull request #829 from RishonLi/patch-1"
This reverts commit 5f5fe8890a, reversing
changes made to a9e8f27360.
2024-12-16 22:46:39 +08:00
Rishon de4f765878
Update face_enhancer.py for apple silicon mps 2024-12-14 16:47:07 +08:00
KRSHH c72582506d Adding Pygrabber as Cam manager 2024-12-13 19:49:11 +05:30
NeuroDonu e4761e4d66
fix path for download and use model 2024-11-09 16:43:35 +03:00
NeuroDonu a840986159
fix path for model 2024-11-09 16:43:13 +03:00
KRSHH 29c9c119d3 Add Mouth Mask Feature 2024-10-25 20:59:30 +05:30
KRSHH 3da987340b
Fix Enhancer for Map Faces 2024-10-15 13:08:03 +05:30
Kenneth Estanislao e531f6f26e improved performance enhancement
improved performance
2024-10-05 01:42:40 +08:00
Kenneth Estanislao cad40b25dc Update face_swapper.py
added the missing ' , my bad on this...
2024-09-19 21:00:29 +08:00
Kenneth Estanislao 1b4c0ce43e Update face_swapper.py
should fix issues for those who dont have nvidia cards
2024-09-19 17:43:05 +08:00
Roland Pereira f133d48f60 handled webcam scenario where detected faces are greater than maps provided 2024-09-11 21:42:38 +05:30
pereiraroland26@gmail.com 53fc65ca7c Added ability to map faces 2024-09-10 05:40:55 +05:30
underlines c91ab8bbd2 add toggle button for blueish cam fix (Force OpenCV2 BGR2RGB) 2024-08-30 22:02:23 +02:00
underlines 79c6615a68 use mjpeg and convert bgr to rgb 2024-08-30 21:49:01 +02:00
c4fun 6400a80a91 solve the FACE_ENHANCER os problem for non-nt(linux, mac) system 2024-08-07 23:26:47 +08:00
Joep de Jong db97940adb
Fix face_enhancer issues 2024-07-07 14:33:32 +02:00
Kenneth Estanislao 16712476a9 Update model to inswapper_128_fp16
Faster as claimed. Also adjusted the size of the preview to a smaller size. You should see a significant improvement on this
2023-10-03 23:38:17 +08:00
Kenneth Estanislao e616245e3d initial commit
rebranding everything
2023-09-24 21:36:57 +08:00