Commit Graph

94 Commits (4e36622a473655e8986ee9a1743242d8469edffb)

Author SHA1 Message Date
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] 0fc481db47 fix: Revert Nth frame logic in ui.py to fix UnboundLocalError
I've completely removed the Nth frame processing logic (frame counter,
interval, and conditional execution) from the `create_webcam_preview`
function in `modules/ui.py`. The frame processing block has been
unindented to ensure it runs on every frame.

This resolves an `UnboundLocalError` for 'detection_frame_counter'
that occurred because the variable was being used after its
initialization was removed in a previous attempt to revert this logic.
The webcam preview will now process every frame as it did before the
Nth frame optimization was introduced.
2025-06-18 11:20:32 +00:00
google-labs-jules[bot] 984048b39a fix: Remove orphaned Nth frame counter line in ui.py
Removes the leftover `detection_frame_counter += 1` line from the
`create_webcam_preview` function in modules/ui.py. This line was
erroneously kept after the rest of the Nth frame processing logic
was reverted, causing an UnboundLocalError as the counter was no
longer initialized.

This fix ensures the webcam preview can start correctly without this error.
2025-06-18 10:35:13 +00:00
google-labs-jules[bot] 9fd870cfd2 refactor: Revert Nth frame processing in webcam mode
Reverts the Nth frame processing logic previously introduced in
modules/ui.py (create_webcam_preview function). Webcam frames
will now be processed by the full pipeline on every frame,
instead of skipping frames.

This change is based on your feedback requesting to focus on
optimizing the per-frame performance rather than using frame
skipping techniques at this stage.
2025-06-18 09:54:10 +00:00
google-labs-jules[bot] c5c08b652f perf: Implement Nth frame processing for webcam mode
Optimizes webcam performance by running full face detection and
frame processing (face swap, enhancers) only every N frames
(currently N=3) in modules/ui.py (create_webcam_preview function).

For intermediate frames, the raw (but mirrored/resized) webcam
feed is displayed. This aims to improve UI responsiveness and reduce
overall CPU/GPU load during live webcam sessions, particularly when
resource-intensive operations like hair swapping or face enhancement
are active.

The actual swap/effect will appear at a reduced frame rate (FPS/N),
but the UI should remain smoother.
2025-06-18 09:03:07 +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] 3d8af5180d fix: Correct IndentationError in modules/ui.py
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.
2025-05-25 17:57:58 +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
Gordon Böer bdbd7dcfbc
fix typos in ui.py 2025-05-07 13:23:31 +02:00
Kenneth Estanislao 28c4b34db1
Merge pull request #911 from nimishgautam/main
Fix cv2 size errors on first run in ui.py
2025-02-05 12:51:39 +08:00
Soul Lee 513e413956 fix: typo souce_target_map → source_target_map 2025-02-03 20:33:44 +09:00
Nimish Gåtam ccc04983cf
Update ui.py
removed unnecessary code as per AI code review (which is a thing now because of course it is)
2025-02-01 12:38:37 +01:00
Nimish Gåtam 2506c5a261
Update ui.py
Some checks for first run when models are missing, so it doesn't error out with inv_scale_x > 0 in cv2
2025-02-01 11:52:49 +01:00
qitian 6e29e4061b merge from the source and little change 2025-01-07 13:46:17 +08:00
Makaru d07d4a6a26 Update ui.py
I pushed it to premain
2025-01-07 01:15:05 +08:00
Kenneth Estanislao b38831dfdf Revert "Merge pull request #868 from kier007/main"
This reverts commit c03f697729, reversing
changes made to d8a5cdbc19.
2025-01-06 14:14:21 +08:00
Kenneth Estanislao b518f4337d Revert "Merge pull request #869 from kier007/patch-1"
This reverts commit b38ef62447, reversing
changes made to c03f697729.
2025-01-06 14:14:04 +08:00
Makaru a3469b7bd4
Update ui.py
Added:
- If you happen to turn off the map faces switch while the Source x Target Mapper window is open, the Source x Target Mapper window will close.
2025-01-06 00:10:53 +08:00
Makaru 742bcab130
Update ui.py
Added:
- try-finally Block: This makes sure the camera.release() is called no matter how the while loops end.
- Resource Cleanup: The finally block takes care of cleaning up resources to keep the application stable.
2025-01-05 20:19:36 +08:00
Makaru 22940d1b99
Update ui.py
The following changes have been implemented:
-A "clear" button has been incorporated.
-The Source x Target Mapper window has been retained following the submission of data via the "submit" button.
2025-01-05 18:29:01 +08:00
KRSHH 8be7368949
Added URL to official website 2024-12-30 15:51:46 +05:30
KRSHH 77c19d1073 FaceTime Camera Index to 0 2024-12-23 14:58:43 +05:30
KRSHH 5ce991651d Formatting
Moved Windows only modules, to top too.
2024-12-23 09:46:59 +05:30
KRSHH 432984b3b6 Mac Fix
Pygrabber Module import only on windows
2024-12-23 09:41:17 +05:30
KRSHH a9e8f27360 Pygrabber only for Windows 2024-12-16 18:41:39 +05:30
KRSHH c72582506d Adding Pygrabber as Cam manager 2024-12-13 19:49:11 +05:30
theogbob 916c2f82d8
Fix ui.py
Add command to "mouth_mask": modules.globals.mouth_mask which fixes the error "SyntaxError: invalid syntax. Perhaps you forgot a comma?"
2024-10-26 14:40:03 -04:00
KRSHH 80f6ea9e65
Save Mouth Mask Switch states 2024-10-26 17:54:45 +05:30
KRSHH 29c9c119d3 Add Mouth Mask Feature 2024-10-25 20:59:30 +05:30
KRSHH ab26413ce8 on/off enhancer during inference and improve FPS counter 2024-10-13 13:16:21 +05:30
KRSHH 53d473164b
remember/save switch states 2024-10-09 19:51:04 +05:30
KRSHH 88164c6303
Show FPS Switch 2024-10-05 17:39:41 +05:30
KRSHH a49d3fc6e5
Face Mapping fix 2024-10-05 15:00:00 +05:30
KRSHH 5812ef3cc9 Webcam selection 2024-10-05 01:37:19 +08:00
KRSHH 75decc5838
Hotswap Source image - switch faces without closing live 2024-10-04 18:17:22 +05:30
Kenneth Estanislao f38ebb485a Update ui.py
removed opacity, will work on it later to optimize
2024-10-04 15:39:08 +08:00
Kenneth Estanislao 95742c8fd5 Merge pull request #686 from GhoulBoii/main
BOUNTY - Webcam Merged (tested)
2024-10-04 14:46:06 +08:00
Kenneth Estanislao 60e27f4755 Revert "Merge pull request #685 from KRSHH/main"
This reverts commit d4e5b8078d, reversing
changes made to c08bec22e3.
2024-10-03 14:51:38 +08:00
KRSHH 61b51fc5d4
Move the slider from live to root 2024-10-02 14:37:19 +05:30
KRSHH f19e425143
Update ui.py 2024-10-02 14:20:56 +05:30
KRSHH 12c0a7ac86
Faceswap live opacity slider 2024-10-02 13:23:39 +05:30
KRSHH dff6cec2f9
Comment Indention fix 2024-09-27 20:59:48 +05:30
KRSHH 4d1d2c86af
Preview video Frame by Frame using left and right arrow keys 2024-09-27 20:40:32 +05:30
KRSHH 6d1238212a
New Fixed UI 2024-09-25 17:36:50 +05:30
Kenneth Estanislao fd4e3f546d reverted to the old version
fixing the issue #597
2024-09-19 17:38:02 +08:00
Kenneth Estanislao 5bcd6dabde Update ui.py 2024-09-19 15:54:57 +08:00
KRSHH 4067d24c26 Fix Popup Live width 2024-09-17 20:18:59 +08:00
Kenneth Estanislao ea7bbd49fe Revert "Merge pull request #592 from KRSHH/main"
This reverts commit 2f29d323d9.
2024-09-16 23:34:35 +08:00
Kenneth Estanislao 2f29d323d9 Merge pull request #592 from KRSHH/main
Better UI
2024-09-16 23:32:39 +08:00