diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md
new file mode 100644
index 0000000..dd84ea7
--- /dev/null
+++ b/.github/ISSUE_TEMPLATE/bug_report.md
@@ -0,0 +1,38 @@
+---
+name: Bug report
+about: Create a report to help us improve
+title: ''
+labels: ''
+assignees: ''
+
+---
+
+**Describe the bug**
+A clear and concise description of what the bug is.
+
+**To Reproduce**
+Steps to reproduce the behavior:
+1. Go to '...'
+2. Click on '....'
+3. Scroll down to '....'
+4. See error
+
+**Expected behavior**
+A clear and concise description of what you expected to happen.
+
+**Screenshots**
+If applicable, add screenshots to help explain your problem.
+
+**Desktop (please complete the following information):**
+ - OS: [e.g. iOS]
+ - Browser [e.g. chrome, safari]
+ - Version [e.g. 22]
+
+**Smartphone (please complete the following information):**
+ - Device: [e.g. iPhone6]
+ - OS: [e.g. iOS8.1]
+ - Browser [e.g. stock browser, safari]
+ - Version [e.g. 22]
+
+**Additional context**
+Add any other context about the problem here.
diff --git a/.gitignore b/.gitignore
index 1f31e58..97197c1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,17 +6,21 @@ __pycache__/
.todo
*.log
*.backup
-
+tf_env/
*.png
*.mp4
*.mkv
.tmp/
temp/
+.venv/
venv/
+env/
workflow/
gfpgan/
models/inswapper_128.onnx
models/GFPGANv1.4.pth
*.onnx
models/DMDNet.pth
+faceswap/
+.vscode/
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
new file mode 100644
index 0000000..41094ad
--- /dev/null
+++ b/CONTRIBUTING.md
@@ -0,0 +1 @@
+Please always push on the experimental to ensure we don't mess with the main branch. All the test will be done on the experimental and will be pushed to the main branch after few days of testing.
diff --git a/README.md b/README.md
index c176603..d636b3e 100644
--- a/README.md
+++ b/README.md
@@ -1,163 +1,214 @@
-
+
Deep Live Cam
+
+ Real-time face swap and video deepfake with a single click and only a single image.
+
+
+
+
+
+
## Disclaimer
-This software is meant to be a productive contribution to the rapidly growing AI-generated media industry. It will help artists with tasks such as animating a custom character or using the character as a model for clothing etc.
-The developers of this software are aware of its possible unethical applicaitons and are committed to take preventative measures against them. It has a built-in check which prevents the program from working on inappropriate media including but not limited to nudity, graphic content, sensitive material such as war footage etc. We will continue to develop this project in the positive direction while adhering to law and ethics. This project may be shut down or include watermarks on the output if requested by law.
+This software is intended as a productive contribution to the AI-generated media industry. It aims to assist artists with tasks like animating custom characters or using them as models for clothing, etc.
-Users of this software are expected to use this software responsibly while abiding the local law. If face of a real person is being used, users are suggested to get consent from the concerned person and clearly mention that it is a deepfake when posting content online. Developers of this software will not be responsible for actions of end-users.
+We are aware of the potential for unethical applications and are committed to preventative measures. A built-in check prevents the program from processing inappropriate media (nudity, graphic content, sensitive material like war footage, etc.). We will continue to develop this project responsibly, adhering to law and ethics. We may shut down the project or add watermarks if legally required.
-## How do I install it?
+Users are expected to use this software responsibly and legally. If using a real person's face, obtain their consent and clearly label any output as a deepfake when sharing online. We are not responsible for end-user actions.
-### Basic: It is more likely to work on your computer but it will also be very slow. You can follow instructions for the basic install (This usually runs via **CPU**)
-#### 1.Setup your platform
-- python (3.10 recommended)
-- pip
-- git
-- [ffmpeg](https://www.youtube.com/watch?v=OlNWCpFdVMA)
-- [visual studio 2022 runtimes (windows)](https://visualstudio.microsoft.com/visual-cpp-build-tools/)
-#### 2. Clone Repository
- https://github.com/hacksider/Deep-Live-Cam.git
+## Quick Start (Windows / Nvidia)
-#### 3. Download Models
+[](https://hacksider.gumroad.com/l/vccdmm)
- 1. [GFPGANv1.4](https://huggingface.co/hacksider/deep-live-cam/resolve/main/GFPGANv1.4.pth)
- 2. [inswapper_128_fp16.onnx](https://huggingface.co/hacksider/deep-live-cam/resolve/main/inswapper_128_fp16.onnx)
-Then put those 2 files on the "**models**" folder
-#### 4. Install dependency
-We highly recommend to work with a `venv` to avoid issues.
+
+
+[Download latest pre-built version with CUDA support](https://hacksider.gumroad.com/l/vccdmm) - No Manual Installation/Downloading required.
+
+## Installation (Manual)
+**Please be aware that the installation needs technical skills and is NOT for beginners, consider downloading the prebuilt. Please do NOT open platform and installation related issues on GitHub before discussing it on the discord server.**
+### Basic Installation (CPU)
+
+This is more likely to work on your computer but will be slower as it utilizes the CPU.
+
+**1. Setup Your Platform**
+
+- Python (3.10 recommended)
+- pip
+- git
+- [ffmpeg](https://www.youtube.com/watch?v=OlNWCpFdVMA)
+- [Visual Studio 2022 Runtimes (Windows)](https://visualstudio.microsoft.com/visual-cpp-build-tools/)
+
+**2. Clone Repository**
+
+```bash
+https://github.com/hacksider/Deep-Live-Cam.git
```
+
+**3. Download Models**
+
+1. [GFPGANv1.4](https://huggingface.co/hacksider/deep-live-cam/resolve/main/GFPGANv1.4.pth)
+2. [inswapper_128_fp16.onnx](https://huggingface.co/hacksider/deep-live-cam/resolve/main/inswapper_128.onnx) (Note: Use this [replacement version](https://github.com/facefusion/facefusion-assets/releases/download/models/inswapper_128.onnx) if you encounter issues)
+
+Place these files in the "**models**" folder.
+
+**4. Install Dependencies**
+
+We highly recommend using a `venv` to avoid issues.
+
+```bash
pip install -r requirements.txt
```
-##### DONE!!! If you dont have any GPU, You should be able to run roop using `python run.py` command. Keep in mind that while running the program for first time, it will download some models which can take time depending on your network connection.
-### *Proceed if you want to use GPU Acceleration
-### CUDA Execution Provider (Nvidia)*
-
-1. Install [CUDA Toolkit 11.8](https://developer.nvidia.com/cuda-11-8-0-download-archive)
-
-2. Install dependencies:
-
+**For macOS:** Install or upgrade the `python-tk` package:
+```bash
+brew install python-tk@3.10
```
+
+**Run:** If you don't have a GPU, you can run Deep-Live-Cam using `python run.py`. Note that initial execution will download models (~300MB).
+
+
+### GPU Acceleration (Optional)
+
+
+Click to see the details
+
+**CUDA Execution Provider (Nvidia)**
+
+1. Install [CUDA Toolkit 11.8](https://developer.nvidia.com/cuda-11-8-0-download-archive)
+2. Install dependencies:
+```bash
pip uninstall onnxruntime onnxruntime-gpu
pip install onnxruntime-gpu==1.16.3
-
-```
-
-3. Usage in case the provider is available:
-
```
+3. Usage:
+```bash
python run.py --execution-provider cuda
-
```
-### [](https://github.com/s0md3v/roop/wiki/2.-Acceleration#coreml-execution-provider-apple-silicon)CoreML Execution Provider (Apple Silicon)
+**CoreML Execution Provider (Apple Silicon)**
-1. Install dependencies:
-
-```
+1. Install dependencies:
+```bash
pip uninstall onnxruntime onnxruntime-silicon
pip install onnxruntime-silicon==1.13.1
-
-```
-
-2. Usage in case the provider is available:
-
```
+2. Usage:
+```bash
python run.py --execution-provider coreml
-
```
-### [](https://github.com/s0md3v/roop/wiki/2.-Acceleration#coreml-execution-provider-apple-legacy)CoreML Execution Provider (Apple Legacy)
+**CoreML Execution Provider (Apple Legacy)**
-1. Install dependencies:
-
-```
+1. Install dependencies:
+```bash
pip uninstall onnxruntime onnxruntime-coreml
pip install onnxruntime-coreml==1.13.1
-
-```
-
-2. Usage in case the provider is available:
-
```
+2. Usage:
+```bash
python run.py --execution-provider coreml
-
```
-### [](https://github.com/s0md3v/roop/wiki/2.-Acceleration#directml-execution-provider-windows)DirectML Execution Provider (Windows)
+**DirectML Execution Provider (Windows)**
-1. Install dependencies:
-
-```
+1. Install dependencies:
+```bash
pip uninstall onnxruntime onnxruntime-directml
pip install onnxruntime-directml==1.15.1
-
-```
-
-2. Usage in case the provider is available:
-
```
+2. Usage:
+```bash
python run.py --execution-provider directml
-
```
-### [](https://github.com/s0md3v/roop/wiki/2.-Acceleration#openvino-execution-provider-intel)OpenVINO™ Execution Provider (Intel)
+**OpenVINO™ Execution Provider (Intel)**
-1. Install dependencies:
-
-```
+1. Install dependencies:
+```bash
pip uninstall onnxruntime onnxruntime-openvino
pip install onnxruntime-openvino==1.15.0
-
-```
-
-2. Usage in case the provider is available:
-
```
+2. Usage:
+```bash
python run.py --execution-provider openvino
```
-## How do I use it?
-> Note: When you run this program for the first time, it will download some models ~300MB in size.
+
-Executing `python run.py` command will launch this window:
-
-Choose a face (image with desired face) and the target image/video (image/video in which you want to replace the face) and click on `Start`. Open file explorer and navigate to the directory you select your output to be in. You will find a directory named `` where you can see the frames being swapped in realtime. Once the processing is done, it will create the output file. That's it.
+## Usage
-## For the webcam mode
-Just follow the clicks on the screenshot
-1. Select a face
-2. Click live
-3. Wait for a few second (it takes a longer time, usually 10 to 30 seconds before the preview shows up)
+**1. Image/Video Mode**
+
+- Execute `python run.py`.
+- Choose a source face image and a target image/video.
+- Click "Start".
+- The output will be saved in a directory named after the target video.
+
+**2. Webcam Mode**
+
+- Execute `python run.py`.
+- Select a source face image.
+- Click "Live".
+- Wait for the preview to appear (10-30 seconds).
+- Use a screen capture tool like OBS to stream.
+- To change the face, select a new source image.

-Just use your favorite screencapture to stream like OBS
-> Note: In case you want to change your face, just select another picture, the preview mode will then restart (so just wait a bit).
+## Features
+
+### Resizable Preview Window
+
+Dynamically improve performance using the `--live-resizable` parameter.
+
+
+
+### Face Mapping
+
+Track and change faces on the fly.
+
+
+
+**Source Video:**
+
+
+
+**Enable Face Mapping:**
+
+
+
+**Map the Faces:**
+
+
+
+**See the Magic!**
-Additional command line arguments are given below. To learn out what they do, check [this guide](https://github.com/s0md3v/roop/wiki/Advanced-Options).
+## Command Line Arguments
```
options:
-h, --help show this help message and exit
- -s SOURCE_PATH, --source SOURCE_PATH select an source image
- -t TARGET_PATH, --target TARGET_PATH select an target image or video
+ -s SOURCE_PATH, --source SOURCE_PATH select a source image
+ -t TARGET_PATH, --target TARGET_PATH select a target image or video
-o OUTPUT_PATH, --output OUTPUT_PATH select output file or directory
--frame-processor FRAME_PROCESSOR [FRAME_PROCESSOR ...] frame processors (choices: face_swapper, face_enhancer, ...)
--keep-fps keep original fps
--keep-audio keep original audio
--keep-frames keep temporary frames
--many-faces process every face
+ --map-faces map source target faces
+ --nsfw-filter filter the NSFW image or video
--video-encoder {libx264,libx265,libvpx-vp9} adjust output video encoder
--video-quality [0-51] adjust output video quality
+ --live-mirror the live camera display as you see it in the front-facing camera frame
+ --live-resizable the live camera frame is resizable
--max-memory MAX_MEMORY maximum amount of RAM in GB
--execution-provider {cpu} [{cpu} ...] available execution provider (choices: cpu, ...)
--execution-threads EXECUTION_THREADS number of execution threads
@@ -166,10 +217,189 @@ options:
Looking for a CLI mode? Using the -s/--source argument will make the run program in cli mode.
+
+## Webcam Mode on WSL2 Ubuntu (Optional)
+
+
+Click to see the details
+
+If you want to use WSL2 on Windows 11 you will notice, that Ubuntu WSL2 doesn't come with USB-Webcam support in the Kernel. You need to do two things: Compile the Kernel with the right modules integrated and forward your USB Webcam from Windows to Ubuntu with the usbipd app. Here are detailed Steps:
+
+This tutorial will guide you through the process of setting up WSL2 Ubuntu with USB webcam support, rebuilding the kernel, and preparing the environment for the Deep-Live-Cam project.
+
+**1. Install WSL2 Ubuntu**
+
+Install WSL2 Ubuntu from the Microsoft Store or using PowerShell:
+
+**2. Enable USB Support in WSL2**
+
+1. Install the USB/IP tool for Windows:
+[https://learn.microsoft.com/en-us/windows/wsl/connect-usb](https://learn.microsoft.com/en-us/windows/wsl/connect-usb)
+
+2. In Windows PowerShell (as Administrator), connect your webcam to WSL:
+
+```powershell
+usbipd list
+usbipd bind --busid x-x # Replace x-x with your webcam's bus ID
+usbipd attach --wsl --busid x-x # Replace x-x with your webcam's bus ID
+```
+ You need to redo the above every time you reboot wsl or re-connect your webcam/usb device.
+
+**3. Rebuild WSL2 Ubuntu Kernel with USB and Webcam Modules**
+
+Follow these steps to rebuild the kernel:
+
+1. Start with this guide: [https://github.com/PINTO0309/wsl2_linux_kernel_usbcam_enable_conf](https://github.com/PINTO0309/wsl2_linux_kernel_usbcam_enable_conf)
+
+2. When you reach the `sudo wget [github.com](http://github.com/)...PINTO0309` step, which won't work for newer kernel versions, follow this video instead or alternatively follow the video tutorial from the beginning:
+[https://www.youtube.com/watch?v=t_YnACEPmrM](https://www.youtube.com/watch?v=t_YnACEPmrM)
+
+Additional info: [https://askubuntu.com/questions/1413377/camera-not-working-in-cheese-in-wsl2](https://askubuntu.com/questions/1413377/camera-not-working-in-cheese-in-wsl2)
+
+3. After rebuilding, restart WSL with the new kernel.
+
+**4. Set Up Deep-Live-Cam Project**
+ Within Ubuntu:
+1. Clone the repository:
+
+```bash
+git clone [https://github.com/hacksider/Deep-Live-Cam](https://github.com/hacksider/Deep-Live-Cam)
+```
+
+2. Follow the installation instructions in the repository, including cuda toolkit 11.8, make 100% sure it's not cuda toolkit 12.x.
+
+**5. Verify and Load Kernel Modules**
+
+1. Check if USB and webcam modules are built into the kernel:
+
+```bash
+zcat /proc/config.gz | grep -i "CONFIG_USB_VIDEO_CLASS"
+```
+
+2. If modules are loadable (m), not built-in (y), check if the file exists:
+
+```bash
+ls /lib/modules/$(uname -r)/kernel/drivers/media/usb/uvc/
+```
+
+3. Load the module and check for errors (optional if built-in):
+
+```bash
+sudo modprobe uvcvideo
+dmesg | tail
+```
+
+4. Verify video devices:
+
+```bash
+sudo ls -al /dev/video*
+```
+
+**6. Set Up Permissions**
+
+1. Add user to video group and set permissions:
+
+```bash
+sudo usermod -a -G video $USER
+sudo chgrp video /dev/video0 /dev/video1
+sudo chmod 660 /dev/video0 /dev/video1
+```
+
+2. Create a udev rule for permanent permissions:
+
+```bash
+sudo nano /etc/udev/rules.d/81-webcam.rules
+```
+
+Add this content:
+
+```
+KERNEL=="video[0-9]*", GROUP="video", MODE="0660"
+```
+
+3. Reload udev rules:
+
+```bash
+sudo udevadm control --reload-rules && sudo udevadm trigger
+```
+
+4. Log out and log back into your WSL session.
+
+5. Start Deep-Live-Cam with `python run.py --execution-provider cuda --max-memory 8` where 8 can be changed to the number of GB VRAM of your GPU has, minus 1-2GB. If you have a RTX3080 with 10GB I suggest adding 8GB. Leave some left for Windows.
+
+**Final Notes**
+
+- Steps 6 and 7 may be optional if the modules are built into the kernel and permissions are already set correctly.
+- Always ensure you're using compatible versions of CUDA, ONNX, and other dependencies.
+- If issues persist, consider checking the Deep-Live-Cam project's specific requirements and troubleshooting steps.
+
+By following these steps, you should have a WSL2 Ubuntu environment with USB webcam support ready for the Deep-Live-Cam project. If you encounter any issues, refer back to the specific error messages and troubleshooting steps provided.
+
+**Troubleshooting CUDA Issues**
+
+If you encounter this error:
+
+```
+[ONNXRuntimeError] : 1 : FAIL : Failed to load library [libonnxruntime_providers_cuda.so](http://libonnxruntime_providers_cuda.so/) with error: libcufft.so.10: cannot open shared object file: No such file or directory
+```
+
+Follow these steps:
+
+1. Install CUDA Toolkit 11.8 (ONNX 1.16.3 requires CUDA 11.x, not 12.x):
+[https://developer.nvidia.com/cuda-11-8-0-download-archive](https://developer.nvidia.com/cuda-11-8-0-download-archive)
+ select: Linux, x86_64, WSL-Ubuntu, 2.0, deb (local)
+2. Check CUDA version:
+
+```bash
+/usr/local/cuda/bin/nvcc --version
+```
+
+3. If the wrong version is installed, remove it completely:
+[https://askubuntu.com/questions/530043/removing-nvidia-cuda-toolkit-and-installing-new-one](https://askubuntu.com/questions/530043/removing-nvidia-cuda-toolkit-and-installing-new-one)
+
+4. Install CUDA Toolkit 11.8 again [https://developer.nvidia.com/cuda-11-8-0-download-archive](https://developer.nvidia.com/cuda-11-8-0-download-archive), select: Linux, x86_64, WSL-Ubuntu, 2.0, deb (local)
+
+```bash
+sudo apt-get -y install cuda-toolkit-11-8
+```
+
+
+
+## Future Updates & Roadmap
+
+For the latest experimental builds and features, see the [experimental branch](https://github.com/hacksider/Deep-Live-Cam/tree/experimental).
+
+**TODO:**
+
+- [ ] Develop a version for web app/service
+- [ ] Speed up model loading
+- [ ] Speed up real-time face swapping
+- [x] Support multiple faces
+- [x] UI/UX enhancements for desktop app
+
+This is an open-source project developed in our free time. Updates may be delayed.
+
+
## Credits
-- [henryruhs](https://github.com/henryruhs): for being an irreplaceable contributor to the project
+
- [ffmpeg](https://ffmpeg.org/): for making video related operations easy
-- [deepinsight](https://github.com/deepinsight): for their [insightface](https://github.com/deepinsight/insightface) project which provided a well-made library and models.
+- [deepinsight](https://github.com/deepinsight): for their [insightface](https://github.com/deepinsight/insightface) project which provided a well-made library and models. Please be reminded that the [use of the model is for non-commercial research purposes only](https://github.com/deepinsight/insightface?tab=readme-ov-file#license).
- [havok2-htwo](https://github.com/havok2-htwo) : for sharing the code for webcam
-- [GosuDRM](https://github.com/GosuDRM/nsfw-roop) : for uncensoring roop
-- and all developers behind libraries used in this project.
+- [GosuDRM](https://github.com/GosuDRM) : for open version of roop
+- [pereiraroland26](https://github.com/pereiraroland26) : Multiple faces support
+- [vic4key](https://github.com/vic4key) : For supporting/contributing on this project
+- [KRSHH](https://github.com/KRSHH) : For updating the UI
+- and [all developers](https://github.com/hacksider/Deep-Live-Cam/graphs/contributors) behind libraries used in this project.
+- Foot Note: [This is originally roop-cam, see the full history of the code here.](https://github.com/hacksider/roop-cam) Please be informed that the base author of the code is [s0md3v](https://github.com/s0md3v/roop)
+
+## Contributions
+
+## Star History
+
+
+
+
+
+
+
+
diff --git a/avgpcperformancedemo.gif b/avgpcperformancedemo.gif
new file mode 100644
index 0000000..815d622
Binary files /dev/null and b/avgpcperformancedemo.gif differ
diff --git a/docs/demo.gif b/docs/demo.gif
deleted file mode 100644
index def2909..0000000
Binary files a/docs/demo.gif and /dev/null differ
diff --git a/docs/gui-demo.jpg b/docs/gui-demo.jpg
deleted file mode 100644
index 660b10f..0000000
Binary files a/docs/gui-demo.jpg and /dev/null differ
diff --git a/face_mapping.png b/face_mapping.png
new file mode 100644
index 0000000..7086e9a
Binary files /dev/null and b/face_mapping.png differ
diff --git a/face_mapping2.png b/face_mapping2.png
new file mode 100644
index 0000000..40e8e0a
Binary files /dev/null and b/face_mapping2.png differ
diff --git a/face_mapping_result.gif b/face_mapping_result.gif
new file mode 100644
index 0000000..190d23c
Binary files /dev/null and b/face_mapping_result.gif differ
diff --git a/face_mapping_source.gif b/face_mapping_source.gif
new file mode 100644
index 0000000..4f32135
Binary files /dev/null and b/face_mapping_source.gif differ
diff --git a/modules/capturer.py b/modules/capturer.py
index fd49d46..a87cf4c 100644
--- a/modules/capturer.py
+++ b/modules/capturer.py
@@ -1,16 +1,28 @@
from typing import Any
import cv2
+import modules.globals # Import the globals to check the color correction toggle
def get_video_frame(video_path: str, frame_number: int = 0) -> Any:
capture = cv2.VideoCapture(video_path)
+
+ # Set MJPEG format to ensure correct color space handling
+ capture.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG'))
+
+ # Only force RGB conversion if color correction is enabled
+ if modules.globals.color_correction:
+ capture.set(cv2.CAP_PROP_CONVERT_RGB, 1)
+
frame_total = capture.get(cv2.CAP_PROP_FRAME_COUNT)
capture.set(cv2.CAP_PROP_POS_FRAMES, min(frame_total, frame_number - 1))
has_frame, frame = capture.read()
+
+ if has_frame and modules.globals.color_correction:
+ # Convert the frame color if necessary
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
+
capture.release()
- if has_frame:
- return frame
- return None
+ return frame if has_frame else None
def get_video_frame_total(video_path: str) -> int:
diff --git a/modules/cluster_analysis.py b/modules/cluster_analysis.py
new file mode 100644
index 0000000..0e7db03
--- /dev/null
+++ b/modules/cluster_analysis.py
@@ -0,0 +1,32 @@
+import numpy as np
+from sklearn.cluster import KMeans
+from sklearn.metrics import silhouette_score
+from typing import Any
+
+
+def find_cluster_centroids(embeddings, max_k=10) -> Any:
+ inertia = []
+ cluster_centroids = []
+ K = range(1, max_k+1)
+
+ for k in K:
+ kmeans = KMeans(n_clusters=k, random_state=0)
+ kmeans.fit(embeddings)
+ inertia.append(kmeans.inertia_)
+ cluster_centroids.append({"k": k, "centroids": kmeans.cluster_centers_})
+
+ diffs = [inertia[i] - inertia[i+1] for i in range(len(inertia)-1)]
+ optimal_centroids = cluster_centroids[diffs.index(max(diffs)) + 1]['centroids']
+
+ return optimal_centroids
+
+def find_closest_centroid(centroids: list, normed_face_embedding) -> list:
+ try:
+ centroids = np.array(centroids)
+ normed_face_embedding = np.array(normed_face_embedding)
+ similarities = np.dot(centroids, normed_face_embedding)
+ closest_centroid_index = np.argmax(similarities)
+
+ return closest_centroid_index, centroids[closest_centroid_index]
+ except ValueError:
+ return None
\ No newline at end of file
diff --git a/modules/core.py b/modules/core.py
index 3ec1617..55707e2 100644
--- a/modules/core.py
+++ b/modules/core.py
@@ -39,8 +39,12 @@ def parse_args() -> None:
program.add_argument('--keep-audio', help='keep original audio', dest='keep_audio', action='store_true', default=True)
program.add_argument('--keep-frames', help='keep temporary frames', dest='keep_frames', action='store_true', default=False)
program.add_argument('--many-faces', help='process every face', dest='many_faces', action='store_true', default=False)
+ program.add_argument('--nsfw-filter', help='filter the NSFW image or video', dest='nsfw_filter', action='store_true', default=False)
+ program.add_argument('--map-faces', help='map source target faces', dest='map_faces', action='store_true', default=False)
program.add_argument('--video-encoder', help='adjust output video encoder', dest='video_encoder', default='libx264', choices=['libx264', 'libx265', 'libvpx-vp9'])
program.add_argument('--video-quality', help='adjust output video quality', dest='video_quality', type=int, default=18, choices=range(52), metavar='[0-51]')
+ program.add_argument('--live-mirror', help='The live camera display as you see it in the front-facing camera frame', dest='live_mirror', action='store_true', default=False)
+ program.add_argument('--live-resizable', help='The live camera frame is resizable', dest='live_resizable', action='store_true', default=False)
program.add_argument('--max-memory', help='maximum amount of RAM in GB', dest='max_memory', type=int, default=suggest_max_memory())
program.add_argument('--execution-provider', help='execution provider', dest='execution_provider', default=['cpu'], choices=suggest_execution_providers(), nargs='+')
program.add_argument('--execution-threads', help='number of execution threads', dest='execution_threads', type=int, default=suggest_execution_threads())
@@ -63,8 +67,12 @@ def parse_args() -> None:
modules.globals.keep_audio = args.keep_audio
modules.globals.keep_frames = args.keep_frames
modules.globals.many_faces = args.many_faces
+ modules.globals.nsfw_filter = args.nsfw_filter
+ modules.globals.map_faces = args.map_faces
modules.globals.video_encoder = args.video_encoder
modules.globals.video_quality = args.video_quality
+ modules.globals.live_mirror = args.live_mirror
+ modules.globals.live_resizable = args.live_resizable
modules.globals.max_memory = args.max_memory
modules.globals.execution_providers = decode_execution_providers(args.execution_provider)
modules.globals.execution_threads = args.execution_threads
@@ -74,8 +82,6 @@ def parse_args() -> None:
modules.globals.fp_ui['face_enhancer'] = True
else:
modules.globals.fp_ui['face_enhancer'] = False
-
- modules.globals.nsfw = False
# translate deprecated args
if args.source_path_deprecated:
@@ -165,18 +171,19 @@ def update_status(message: str, scope: str = 'DLC.CORE') -> None:
if not modules.globals.headless:
ui.update_status(message)
-
def start() -> None:
for frame_processor in get_frame_processors_modules(modules.globals.frame_processors):
if not frame_processor.pre_start():
return
+ update_status('Processing...')
# process image to image
if has_image_extension(modules.globals.target_path):
- if modules.globals.nsfw == False:
- from modules.predicter import predict_image
- if predict_image(modules.globals.target_path):
- destroy()
- shutil.copy2(modules.globals.target_path, modules.globals.output_path)
+ if modules.globals.nsfw_filter and ui.check_and_ignore_nsfw(modules.globals.target_path, destroy):
+ return
+ try:
+ shutil.copy2(modules.globals.target_path, modules.globals.output_path)
+ except Exception as e:
+ print("Error copying file:", str(e))
for frame_processor in get_frame_processors_modules(modules.globals.frame_processors):
update_status('Progressing...', frame_processor.NAME)
frame_processor.process_image(modules.globals.source_path, modules.globals.output_path, modules.globals.output_path)
@@ -187,14 +194,15 @@ def start() -> None:
update_status('Processing to image failed!')
return
# process image to videos
- if modules.globals.nsfw == False:
- from modules.predicter import predict_video
- if predict_video(modules.globals.target_path):
- destroy()
- update_status('Creating temp resources...')
- create_temp(modules.globals.target_path)
- update_status('Extracting frames...')
- extract_frames(modules.globals.target_path)
+ if modules.globals.nsfw_filter and ui.check_and_ignore_nsfw(modules.globals.target_path, destroy):
+ return
+
+ if not modules.globals.map_faces:
+ update_status('Creating temp resources...')
+ create_temp(modules.globals.target_path)
+ update_status('Extracting frames...')
+ extract_frames(modules.globals.target_path)
+
temp_frame_paths = get_temp_frame_paths(modules.globals.target_path)
for frame_processor in get_frame_processors_modules(modules.globals.frame_processors):
update_status('Progressing...', frame_processor.NAME)
@@ -226,10 +234,10 @@ def start() -> None:
update_status('Processing to video failed!')
-def destroy() -> None:
+def destroy(to_quit=True) -> None:
if modules.globals.target_path:
clean_temp(modules.globals.target_path)
- quit()
+ if to_quit: quit()
def run() -> None:
diff --git a/modules/face_analyser.py b/modules/face_analyser.py
index f2d46bf..2122784 100644
--- a/modules/face_analyser.py
+++ b/modules/face_analyser.py
@@ -1,8 +1,16 @@
+import os
+import shutil
from typing import Any
import insightface
+import cv2
+import numpy as np
import modules.globals
+from tqdm import tqdm
from modules.typing import Frame
+from modules.cluster_analysis import find_cluster_centroids, find_closest_centroid
+from modules.utilities import get_temp_directory_path, create_temp, extract_frames, clean_temp, get_temp_frame_paths
+from pathlib import Path
FACE_ANALYSER = None
@@ -29,3 +37,153 @@ def get_many_faces(frame: Frame) -> Any:
return get_face_analyser().get(frame)
except IndexError:
return None
+
+def has_valid_map() -> bool:
+ for map in modules.globals.souce_target_map:
+ if "source" in map and "target" in map:
+ return True
+ return False
+
+def default_source_face() -> Any:
+ for map in modules.globals.souce_target_map:
+ if "source" in map:
+ return map['source']['face']
+ return None
+
+def simplify_maps() -> Any:
+ centroids = []
+ faces = []
+ for map in modules.globals.souce_target_map:
+ if "source" in map and "target" in map:
+ centroids.append(map['target']['face'].normed_embedding)
+ faces.append(map['source']['face'])
+
+ modules.globals.simple_map = {'source_faces': faces, 'target_embeddings': centroids}
+ return None
+
+def add_blank_map() -> Any:
+ try:
+ max_id = -1
+ if len(modules.globals.souce_target_map) > 0:
+ max_id = max(modules.globals.souce_target_map, key=lambda x: x['id'])['id']
+
+ modules.globals.souce_target_map.append({
+ 'id' : max_id + 1
+ })
+ except ValueError:
+ return None
+
+def get_unique_faces_from_target_image() -> Any:
+ try:
+ modules.globals.souce_target_map = []
+ target_frame = cv2.imread(modules.globals.target_path)
+ many_faces = get_many_faces(target_frame)
+ i = 0
+
+ for face in many_faces:
+ x_min, y_min, x_max, y_max = face['bbox']
+ modules.globals.souce_target_map.append({
+ 'id' : i,
+ 'target' : {
+ 'cv2' : target_frame[int(y_min):int(y_max), int(x_min):int(x_max)],
+ 'face' : face
+ }
+ })
+ i = i + 1
+ except ValueError:
+ return None
+
+
+def get_unique_faces_from_target_video() -> Any:
+ try:
+ modules.globals.souce_target_map = []
+ frame_face_embeddings = []
+ face_embeddings = []
+
+ print('Creating temp resources...')
+ clean_temp(modules.globals.target_path)
+ create_temp(modules.globals.target_path)
+ print('Extracting frames...')
+ extract_frames(modules.globals.target_path)
+
+ temp_frame_paths = get_temp_frame_paths(modules.globals.target_path)
+
+ i = 0
+ for temp_frame_path in tqdm(temp_frame_paths, desc="Extracting face embeddings from frames"):
+ temp_frame = cv2.imread(temp_frame_path)
+ many_faces = get_many_faces(temp_frame)
+
+ for face in many_faces:
+ face_embeddings.append(face.normed_embedding)
+
+ frame_face_embeddings.append({'frame': i, 'faces': many_faces, 'location': temp_frame_path})
+ i += 1
+
+ centroids = find_cluster_centroids(face_embeddings)
+
+ for frame in frame_face_embeddings:
+ for face in frame['faces']:
+ closest_centroid_index, _ = find_closest_centroid(centroids, face.normed_embedding)
+ face['target_centroid'] = closest_centroid_index
+
+ for i in range(len(centroids)):
+ modules.globals.souce_target_map.append({
+ 'id' : i
+ })
+
+ temp = []
+ for frame in tqdm(frame_face_embeddings, desc=f"Mapping frame embeddings to centroids-{i}"):
+ temp.append({'frame': frame['frame'], 'faces': [face for face in frame['faces'] if face['target_centroid'] == i], 'location': frame['location']})
+
+ modules.globals.souce_target_map[i]['target_faces_in_frame'] = temp
+
+ # dump_faces(centroids, frame_face_embeddings)
+ default_target_face()
+ except ValueError:
+ return None
+
+
+def default_target_face():
+ for map in modules.globals.souce_target_map:
+ best_face = None
+ best_frame = None
+ for frame in map['target_faces_in_frame']:
+ if len(frame['faces']) > 0:
+ best_face = frame['faces'][0]
+ best_frame = frame
+ break
+
+ for frame in map['target_faces_in_frame']:
+ for face in frame['faces']:
+ if face['det_score'] > best_face['det_score']:
+ best_face = face
+ best_frame = frame
+
+ x_min, y_min, x_max, y_max = best_face['bbox']
+
+ target_frame = cv2.imread(best_frame['location'])
+ map['target'] = {
+ 'cv2' : target_frame[int(y_min):int(y_max), int(x_min):int(x_max)],
+ 'face' : best_face
+ }
+
+
+def dump_faces(centroids: Any, frame_face_embeddings: list):
+ temp_directory_path = get_temp_directory_path(modules.globals.target_path)
+
+ for i in range(len(centroids)):
+ if os.path.exists(temp_directory_path + f"/{i}") and os.path.isdir(temp_directory_path + f"/{i}"):
+ shutil.rmtree(temp_directory_path + f"/{i}")
+ Path(temp_directory_path + f"/{i}").mkdir(parents=True, exist_ok=True)
+
+ for frame in tqdm(frame_face_embeddings, desc=f"Copying faces to temp/./{i}"):
+ temp_frame = cv2.imread(frame['location'])
+
+ j = 0
+ for face in frame['faces']:
+ if face['target_centroid'] == i:
+ x_min, y_min, x_max, y_max = face['bbox']
+
+ if temp_frame[int(y_min):int(y_max), int(x_min):int(x_max)].size > 0:
+ cv2.imwrite(temp_directory_path + f"/{i}/{frame['frame']}_{j}.png", temp_frame[int(y_min):int(y_max), int(x_min):int(x_max)])
+ j += 1
\ No newline at end of file
diff --git a/modules/globals.py b/modules/globals.py
index c392a80..cffae16 100644
--- a/modules/globals.py
+++ b/modules/globals.py
@@ -1,30 +1,38 @@
import os
-from typing import List, Dict
+from typing import List, Dict, Any
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
-WORKFLOW_DIR = os.path.join(ROOT_DIR, 'workflow')
+WORKFLOW_DIR = os.path.join(ROOT_DIR, "workflow")
file_types = [
- ('Image', ('*.png','*.jpg','*.jpeg','*.gif','*.bmp')),
- ('Video', ('*.mp4','*.mkv'))
+ ("Image", ("*.png", "*.jpg", "*.jpeg", "*.gif", "*.bmp")),
+ ("Video", ("*.mp4", "*.mkv")),
]
+souce_target_map = []
+simple_map = {}
+
source_path = None
target_path = None
output_path = None
frame_processors: List[str] = []
-keep_fps = None
-keep_audio = None
-keep_frames = None
-many_faces = None
+keep_fps = True
+keep_audio = True
+keep_frames = False
+many_faces = False
+map_faces = False
+color_correction = False # New global variable for color correction toggle
+nsfw_filter = False
video_encoder = None
video_quality = None
+live_mirror = False
+live_resizable = False
max_memory = None
execution_providers: List[str] = []
execution_threads = None
headless = None
-log_level = 'error'
-fp_ui: Dict[str, bool] = {}
-nsfw = None
+log_level = "error"
+fp_ui: Dict[str, bool] = {"face_enhancer": False}
camera_input_combobox = None
-webcam_preview_running = False
\ No newline at end of file
+webcam_preview_running = False
+show_fps = False
diff --git a/modules/metadata.py b/modules/metadata.py
index 9309bea..97776e2 100644
--- a/modules/metadata.py
+++ b/modules/metadata.py
@@ -1,3 +1,3 @@
name = 'Deep Live Cam'
-version = '1.3.0'
+version = '1.6.0'
edition = 'Portable'
diff --git a/modules/predicter.py b/modules/predicter.py
index bb51b0a..23a2564 100644
--- a/modules/predicter.py
+++ b/modules/predicter.py
@@ -1,16 +1,27 @@
import numpy
import opennsfw2
from PIL import Image
+import cv2 # Add OpenCV import
+import modules.globals # Import globals to access the color correction toggle
from modules.typing import Frame
MAX_PROBABILITY = 0.85
+# Preload the model once for efficiency
+model = None
def predict_frame(target_frame: Frame) -> bool:
+ # Convert the frame to RGB before processing if color correction is enabled
+ if modules.globals.color_correction:
+ target_frame = cv2.cvtColor(target_frame, cv2.COLOR_BGR2RGB)
+
image = Image.fromarray(target_frame)
image = opennsfw2.preprocess_image(image, opennsfw2.Preprocessing.YAHOO)
- model = opennsfw2.make_open_nsfw_model()
+ global model
+ if model is None:
+ model = opennsfw2.make_open_nsfw_model()
+
views = numpy.expand_dims(image, axis=0)
_, probability = model.predict(views)[0]
return probability > MAX_PROBABILITY
diff --git a/modules/processors/frame/face_enhancer.py b/modules/processors/frame/face_enhancer.py
index 4d960af..608071a 100644
--- a/modules/processors/frame/face_enhancer.py
+++ b/modules/processors/frame/face_enhancer.py
@@ -2,6 +2,7 @@ from typing import Any, List
import cv2
import threading
import gfpgan
+import os
import modules.globals
import modules.processors.frame.core
@@ -34,8 +35,11 @@ def get_face_enhancer() -> Any:
with THREAD_LOCK:
if FACE_ENHANCER is None:
- model_path = resolve_relative_path('..\models\GFPGANv1.4.pth')
- # todo: set models path https://github.com/TencentARC/GFPGAN/issues/399
+ if os.name == 'nt':
+ model_path = resolve_relative_path('..\models\GFPGANv1.4.pth')
+ # todo: set models path https://github.com/TencentARC/GFPGAN/issues/399
+ else:
+ model_path = resolve_relative_path('../models/GFPGANv1.4.pth')
FACE_ENHANCER = gfpgan.GFPGANer(model_path=model_path, upscale=1) # type: ignore[attr-defined]
return FACE_ENHANCER
diff --git a/modules/processors/frame/face_swapper.py b/modules/processors/frame/face_swapper.py
index 4b4a222..6fd0760 100644
--- a/modules/processors/frame/face_swapper.py
+++ b/modules/processors/frame/face_swapper.py
@@ -6,9 +6,10 @@ import threading
import modules.globals
import modules.processors.frame.core
from modules.core import update_status
-from modules.face_analyser import get_one_face, get_many_faces
+from modules.face_analyser import get_one_face, get_many_faces, default_source_face
from modules.typing import Face, Frame
from modules.utilities import conditional_download, resolve_relative_path, is_image, is_video
+from modules.cluster_analysis import find_closest_centroid
FACE_SWAPPER = None
THREAD_LOCK = threading.Lock()
@@ -22,10 +23,10 @@ def pre_check() -> bool:
def pre_start() -> bool:
- if not is_image(modules.globals.source_path):
+ if not modules.globals.map_faces and not is_image(modules.globals.source_path):
update_status('Select an image for source path.', NAME)
return False
- elif not get_one_face(cv2.imread(modules.globals.source_path)):
+ elif not modules.globals.map_faces and not get_one_face(cv2.imread(modules.globals.source_path)):
update_status('No face in source path detected.', NAME)
return False
if not is_image(modules.globals.target_path) and not is_video(modules.globals.target_path):
@@ -49,6 +50,10 @@ def swap_face(source_face: Face, target_face: Face, temp_frame: Frame) -> Frame:
def process_frame(source_face: Face, temp_frame: Frame) -> Frame:
+ # Ensure the frame is in RGB format if color correction is enabled
+ if modules.globals.color_correction:
+ temp_frame = cv2.cvtColor(temp_frame, cv2.COLOR_BGR2RGB)
+
if modules.globals.many_faces:
many_faces = get_many_faces(temp_frame)
if many_faces:
@@ -61,26 +66,109 @@ def process_frame(source_face: Face, temp_frame: Frame) -> Frame:
return temp_frame
+def process_frame_v2(temp_frame: Frame, temp_frame_path: str = "") -> Frame:
+ if is_image(modules.globals.target_path):
+ if modules.globals.many_faces:
+ source_face = default_source_face()
+ for map in modules.globals.souce_target_map:
+ target_face = map['target']['face']
+ temp_frame = swap_face(source_face, target_face, temp_frame)
+
+ elif not modules.globals.many_faces:
+ for map in modules.globals.souce_target_map:
+ if "source" in map:
+ source_face = map['source']['face']
+ target_face = map['target']['face']
+ temp_frame = swap_face(source_face, target_face, temp_frame)
+
+ elif is_video(modules.globals.target_path):
+ if modules.globals.many_faces:
+ source_face = default_source_face()
+ for map in modules.globals.souce_target_map:
+ target_frame = [f for f in map['target_faces_in_frame'] if f['location'] == temp_frame_path]
+
+ for frame in target_frame:
+ for target_face in frame['faces']:
+ temp_frame = swap_face(source_face, target_face, temp_frame)
+
+ elif not modules.globals.many_faces:
+ for map in modules.globals.souce_target_map:
+ if "source" in map:
+ target_frame = [f for f in map['target_faces_in_frame'] if f['location'] == temp_frame_path]
+ source_face = map['source']['face']
+
+ for frame in target_frame:
+ for target_face in frame['faces']:
+ temp_frame = swap_face(source_face, target_face, temp_frame)
+ else:
+ detected_faces = get_many_faces(temp_frame)
+ if modules.globals.many_faces:
+ if detected_faces:
+ source_face = default_source_face()
+ for target_face in detected_faces:
+ temp_frame = swap_face(source_face, target_face, temp_frame)
+
+ elif not modules.globals.many_faces:
+ if detected_faces:
+ if len(detected_faces) <= len(modules.globals.simple_map['target_embeddings']):
+ for detected_face in detected_faces:
+ closest_centroid_index, _ = find_closest_centroid(modules.globals.simple_map['target_embeddings'], detected_face.normed_embedding)
+
+ temp_frame = swap_face(modules.globals.simple_map['source_faces'][closest_centroid_index], detected_face, temp_frame)
+ else:
+ detected_faces_centroids = []
+ for face in detected_faces:
+ detected_faces_centroids.append(face.normed_embedding)
+ i = 0
+ for target_embedding in modules.globals.simple_map['target_embeddings']:
+ closest_centroid_index, _ = find_closest_centroid(detected_faces_centroids, target_embedding)
+
+ temp_frame = swap_face(modules.globals.simple_map['source_faces'][i], detected_faces[closest_centroid_index], temp_frame)
+ i += 1
+ return temp_frame
+
+
def process_frames(source_path: str, temp_frame_paths: List[str], progress: Any = None) -> None:
- source_face = get_one_face(cv2.imread(source_path))
- for temp_frame_path in temp_frame_paths:
- temp_frame = cv2.imread(temp_frame_path)
- try:
- result = process_frame(source_face, temp_frame)
- cv2.imwrite(temp_frame_path, result)
- except Exception as exception:
- print(exception)
- pass
- if progress:
- progress.update(1)
+ if not modules.globals.map_faces:
+ source_face = get_one_face(cv2.imread(source_path))
+ for temp_frame_path in temp_frame_paths:
+ temp_frame = cv2.imread(temp_frame_path)
+ try:
+ result = process_frame(source_face, temp_frame)
+ cv2.imwrite(temp_frame_path, result)
+ except Exception as exception:
+ print(exception)
+ pass
+ if progress:
+ progress.update(1)
+ else:
+ for temp_frame_path in temp_frame_paths:
+ temp_frame = cv2.imread(temp_frame_path)
+ try:
+ result = process_frame_v2(temp_frame, temp_frame_path)
+ cv2.imwrite(temp_frame_path, result)
+ except Exception as exception:
+ print(exception)
+ pass
+ if progress:
+ progress.update(1)
def process_image(source_path: str, target_path: str, output_path: str) -> None:
- source_face = get_one_face(cv2.imread(source_path))
- target_frame = cv2.imread(target_path)
- result = process_frame(source_face, target_frame)
- cv2.imwrite(output_path, result)
+ if not modules.globals.map_faces:
+ source_face = get_one_face(cv2.imread(source_path))
+ target_frame = cv2.imread(target_path)
+ result = process_frame(source_face, target_frame)
+ cv2.imwrite(output_path, result)
+ else:
+ if modules.globals.many_faces:
+ update_status('Many faces enabled. Using first source image. Progressing...', NAME)
+ target_frame = cv2.imread(output_path)
+ result = process_frame_v2(target_frame)
+ cv2.imwrite(output_path, result)
def process_video(source_path: str, temp_frame_paths: List[str]) -> None:
+ if modules.globals.map_faces and modules.globals.many_faces:
+ update_status('Many faces enabled. Using first source image. Progressing...', NAME)
modules.processors.frame.core.process_video(source_path, temp_frame_paths, process_frames)
diff --git a/modules/ui.py b/modules/ui.py
index 1d0bb69..80a3a6d 100644
--- a/modules/ui.py
+++ b/modules/ui.py
@@ -3,22 +3,57 @@ import webbrowser
import customtkinter as ctk
from typing import Callable, Tuple
import cv2
+from cv2_enumerate_cameras import enumerate_cameras # Add this import
from PIL import Image, ImageOps
+import time
+import json
import modules.globals
import modules.metadata
-from modules.face_analyser import get_one_face
+from modules.face_analyser import (
+ get_one_face,
+ get_unique_faces_from_target_image,
+ get_unique_faces_from_target_video,
+ add_blank_map,
+ has_valid_map,
+ simplify_maps,
+)
from modules.capturer import get_video_frame, get_video_frame_total
from modules.processors.frame.core import get_frame_processors_modules
-from modules.utilities import is_image, is_video, resolve_relative_path
+from modules.utilities import (
+ is_image,
+ is_video,
+ resolve_relative_path,
+ has_image_extension,
+)
ROOT = None
+POPUP = None
+POPUP_LIVE = None
ROOT_HEIGHT = 700
ROOT_WIDTH = 600
PREVIEW = None
PREVIEW_MAX_HEIGHT = 700
PREVIEW_MAX_WIDTH = 1200
+PREVIEW_DEFAULT_WIDTH = 960
+PREVIEW_DEFAULT_HEIGHT = 540
+
+POPUP_WIDTH = 750
+POPUP_HEIGHT = 810
+POPUP_SCROLL_WIDTH = (740,)
+POPUP_SCROLL_HEIGHT = 700
+
+POPUP_LIVE_WIDTH = 900
+POPUP_LIVE_HEIGHT = 820
+POPUP_LIVE_SCROLL_WIDTH = (890,)
+POPUP_LIVE_SCROLL_HEIGHT = 700
+
+MAPPER_PREVIEW_MAX_HEIGHT = 100
+MAPPER_PREVIEW_MAX_WIDTH = 100
+
+DEFAULT_BUTTON_WIDTH = 200
+DEFAULT_BUTTON_HEIGHT = 40
RECENT_DIRECTORY_SOURCE = None
RECENT_DIRECTORY_TARGET = None
@@ -29,6 +64,11 @@ preview_slider = None
source_label = None
target_label = None
status_label = None
+popup_status_label = None
+popup_status_label_live = None
+source_label_dict = {}
+source_label_dict_live = {}
+target_label_dict_live = {}
img_ft, vid_ft = modules.globals.file_types
@@ -42,18 +82,60 @@ def init(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.CTk:
return ROOT
+def save_switch_states():
+ switch_states = {
+ "keep_fps": modules.globals.keep_fps,
+ "keep_audio": modules.globals.keep_audio,
+ "keep_frames": modules.globals.keep_frames,
+ "many_faces": modules.globals.many_faces,
+ "map_faces": modules.globals.map_faces,
+ "color_correction": modules.globals.color_correction,
+ "nsfw_filter": modules.globals.nsfw_filter,
+ "live_mirror": modules.globals.live_mirror,
+ "live_resizable": modules.globals.live_resizable,
+ "fp_ui": modules.globals.fp_ui,
+ "show_fps": modules.globals.show_fps,
+ }
+ with open("switch_states.json", "w") as f:
+ json.dump(switch_states, f)
+
+
+def load_switch_states():
+ try:
+ with open("switch_states.json", "r") as f:
+ switch_states = json.load(f)
+ modules.globals.keep_fps = switch_states.get("keep_fps", True)
+ modules.globals.keep_audio = switch_states.get("keep_audio", True)
+ modules.globals.keep_frames = switch_states.get("keep_frames", False)
+ modules.globals.many_faces = switch_states.get("many_faces", False)
+ modules.globals.map_faces = switch_states.get("map_faces", False)
+ modules.globals.color_correction = switch_states.get("color_correction", False)
+ modules.globals.nsfw_filter = switch_states.get("nsfw_filter", False)
+ modules.globals.live_mirror = switch_states.get("live_mirror", False)
+ modules.globals.live_resizable = switch_states.get("live_resizable", False)
+ modules.globals.fp_ui = switch_states.get("fp_ui", {"face_enhancer": False})
+ modules.globals.show_fps = switch_states.get("show_fps", False)
+ except FileNotFoundError:
+ # If the file doesn't exist, use default values
+ pass
+
+
def create_root(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.CTk:
- global source_label, target_label, status_label
+ global source_label, target_label, status_label, show_fps_switch
+
+ load_switch_states()
ctk.deactivate_automatic_dpi_awareness()
- ctk.set_appearance_mode('system')
- ctk.set_default_color_theme(resolve_relative_path('ui.json'))
+ ctk.set_appearance_mode("system")
+ ctk.set_default_color_theme(resolve_relative_path("ui.json"))
root = ctk.CTk()
root.minsize(ROOT_WIDTH, ROOT_HEIGHT)
- root.title(f'{modules.metadata.name} {modules.metadata.version} {modules.metadata.edition}')
+ root.title(
+ f"{modules.metadata.name} {modules.metadata.version} {modules.metadata.edition}"
+ )
root.configure()
- root.protocol('WM_DELETE_WINDOW', lambda: destroy())
+ root.protocol("WM_DELETE_WINDOW", lambda: destroy())
source_label = ctk.CTkLabel(root, text=None)
source_label.place(relx=0.1, rely=0.1, relwidth=0.3, relheight=0.25)
@@ -61,74 +143,357 @@ def create_root(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.C
target_label = ctk.CTkLabel(root, text=None)
target_label.place(relx=0.6, rely=0.1, relwidth=0.3, relheight=0.25)
- source_button = ctk.CTkButton(root, text='Select a face', cursor='hand2', command=lambda: select_source_path())
- source_button.place(relx=0.1, rely=0.4, relwidth=0.3, relheight=0.1)
+ select_face_button = ctk.CTkButton(
+ root, text="Select a face", cursor="hand2", command=lambda: select_source_path()
+ )
+ select_face_button.place(relx=0.1, rely=0.4, relwidth=0.3, relheight=0.1)
- target_button = ctk.CTkButton(root, text='Select a target', cursor='hand2', command=lambda: select_target_path())
- target_button.place(relx=0.6, rely=0.4, relwidth=0.3, relheight=0.1)
+ swap_faces_button = ctk.CTkButton(
+ root, text="↔", cursor="hand2", command=lambda: swap_faces_paths()
+ )
+ swap_faces_button.place(relx=0.45, rely=0.4, relwidth=0.1, relheight=0.1)
+
+ select_target_button = ctk.CTkButton(
+ root,
+ text="Select a target",
+ cursor="hand2",
+ command=lambda: select_target_path(),
+ )
+ select_target_button.place(relx=0.6, rely=0.4, relwidth=0.3, relheight=0.1)
keep_fps_value = ctk.BooleanVar(value=modules.globals.keep_fps)
- keep_fps_checkbox = ctk.CTkSwitch(root, text='Keep fps', variable=keep_fps_value, cursor='hand2', command=lambda: setattr(modules.globals, 'keep_fps', not modules.globals.keep_fps))
+ keep_fps_checkbox = ctk.CTkSwitch(
+ root,
+ text="Keep fps",
+ variable=keep_fps_value,
+ cursor="hand2",
+ command=lambda: (
+ setattr(modules.globals, "keep_fps", keep_fps_value.get()),
+ save_switch_states(),
+ ),
+ )
keep_fps_checkbox.place(relx=0.1, rely=0.6)
keep_frames_value = ctk.BooleanVar(value=modules.globals.keep_frames)
- keep_frames_switch = ctk.CTkSwitch(root, text='Keep frames', variable=keep_frames_value, cursor='hand2', command=lambda: setattr(modules.globals, 'keep_frames', keep_frames_value.get()))
+ keep_frames_switch = ctk.CTkSwitch(
+ root,
+ text="Keep frames",
+ variable=keep_frames_value,
+ cursor="hand2",
+ command=lambda: (
+ setattr(modules.globals, "keep_frames", keep_frames_value.get()),
+ save_switch_states(),
+ ),
+ )
keep_frames_switch.place(relx=0.1, rely=0.65)
- # for FRAME PROCESSOR ENHANCER tumbler:
- enhancer_value = ctk.BooleanVar(value=modules.globals.fp_ui['face_enhancer'])
- enhancer_switch = ctk.CTkSwitch(root, text='Face Enhancer', variable=enhancer_value, cursor='hand2', command=lambda: update_tumbler('face_enhancer',enhancer_value.get()))
+ enhancer_value = ctk.BooleanVar(value=modules.globals.fp_ui["face_enhancer"])
+ enhancer_switch = ctk.CTkSwitch(
+ root,
+ text="Face Enhancer",
+ variable=enhancer_value,
+ cursor="hand2",
+ command=lambda: (
+ update_tumbler("face_enhancer", enhancer_value.get()),
+ save_switch_states(),
+ ),
+ )
enhancer_switch.place(relx=0.1, rely=0.7)
keep_audio_value = ctk.BooleanVar(value=modules.globals.keep_audio)
- keep_audio_switch = ctk.CTkSwitch(root, text='Keep audio', variable=keep_audio_value, cursor='hand2', command=lambda: setattr(modules.globals, 'keep_audio', keep_audio_value.get()))
+ keep_audio_switch = ctk.CTkSwitch(
+ root,
+ text="Keep audio",
+ variable=keep_audio_value,
+ cursor="hand2",
+ command=lambda: (
+ setattr(modules.globals, "keep_audio", keep_audio_value.get()),
+ save_switch_states(),
+ ),
+ )
keep_audio_switch.place(relx=0.6, rely=0.6)
many_faces_value = ctk.BooleanVar(value=modules.globals.many_faces)
- many_faces_switch = ctk.CTkSwitch(root, text='Many faces', variable=many_faces_value, cursor='hand2', command=lambda: setattr(modules.globals, 'many_faces', many_faces_value.get()))
+ many_faces_switch = ctk.CTkSwitch(
+ root,
+ text="Many faces",
+ variable=many_faces_value,
+ cursor="hand2",
+ command=lambda: (
+ setattr(modules.globals, "many_faces", many_faces_value.get()),
+ save_switch_states(),
+ ),
+ )
many_faces_switch.place(relx=0.6, rely=0.65)
- nsfw_value = ctk.BooleanVar(value=modules.globals.nsfw)
- nsfw_switch = ctk.CTkSwitch(root, text='NSFW', variable=nsfw_value, cursor='hand2', command=lambda: setattr(modules.globals, 'nsfw', nsfw_value.get()))
- nsfw_switch.place(relx=0.6, rely=0.7)
+ color_correction_value = ctk.BooleanVar(value=modules.globals.color_correction)
+ color_correction_switch = ctk.CTkSwitch(
+ root,
+ text="Fix Blueish Cam\n(force cv2 to use RGB instead of BGR)",
+ variable=color_correction_value,
+ cursor="hand2",
+ command=lambda: (
+ setattr(modules.globals, "color_correction", color_correction_value.get()),
+ save_switch_states(),
+ ),
+ )
+ color_correction_switch.place(relx=0.6, rely=0.70)
- start_button = ctk.CTkButton(root, text='Start', cursor='hand2', command=lambda: select_output_path(start))
+ # nsfw_value = ctk.BooleanVar(value=modules.globals.nsfw_filter)
+ # nsfw_switch = ctk.CTkSwitch(root, text='NSFW filter', variable=nsfw_value, cursor='hand2', command=lambda: setattr(modules.globals, 'nsfw_filter', nsfw_value.get()))
+ # nsfw_switch.place(relx=0.6, rely=0.7)
+
+ map_faces = ctk.BooleanVar(value=modules.globals.map_faces)
+ map_faces_switch = ctk.CTkSwitch(
+ root,
+ text="Map faces",
+ variable=map_faces,
+ cursor="hand2",
+ command=lambda: (
+ setattr(modules.globals, "map_faces", map_faces.get()),
+ save_switch_states(),
+ ),
+ )
+ map_faces_switch.place(relx=0.1, rely=0.75)
+
+ show_fps_value = ctk.BooleanVar(value=modules.globals.show_fps)
+ show_fps_switch = ctk.CTkSwitch(
+ root,
+ text="Show FPS",
+ variable=show_fps_value,
+ cursor="hand2",
+ command=lambda: (
+ setattr(modules.globals, "show_fps", show_fps_value.get()),
+ save_switch_states(),
+ ),
+ )
+ show_fps_switch.place(relx=0.6, rely=0.75)
+
+ start_button = ctk.CTkButton(
+ root, text="Start", cursor="hand2", command=lambda: analyze_target(start, root)
+ )
start_button.place(relx=0.15, rely=0.80, relwidth=0.2, relheight=0.05)
- stop_button = ctk.CTkButton(root, text='Destroy', cursor='hand2', command=lambda: destroy())
+ stop_button = ctk.CTkButton(
+ root, text="Destroy", cursor="hand2", command=lambda: destroy()
+ )
stop_button.place(relx=0.4, rely=0.80, relwidth=0.2, relheight=0.05)
- preview_button = ctk.CTkButton(root, text='Preview', cursor='hand2', command=lambda: toggle_preview())
+ preview_button = ctk.CTkButton(
+ root, text="Preview", cursor="hand2", command=lambda: toggle_preview()
+ )
preview_button.place(relx=0.65, rely=0.80, relwidth=0.2, relheight=0.05)
- live_button = ctk.CTkButton(root, text='Live', cursor='hand2', command=lambda: webcam_preview())
- live_button.place(relx=0.40, rely=0.86, relwidth=0.2, relheight=0.05)
+ # --- Camera Selection ---
+ camera_label = ctk.CTkLabel(root, text="Select Camera:")
+ camera_label.place(relx=0.1, rely=0.86, relwidth=0.2, relheight=0.05)
- status_label = ctk.CTkLabel(root, text=None, justify='center')
+ available_cameras = get_available_cameras()
+ # Convert camera indices to strings for CTkOptionMenu
+ available_camera_indices, available_camera_strings = available_cameras
+ camera_variable = ctk.StringVar(
+ value=(
+ available_camera_strings[0]
+ if available_camera_strings
+ else "No cameras found"
+ )
+ )
+ camera_optionmenu = ctk.CTkOptionMenu(
+ root, variable=camera_variable, values=available_camera_strings
+ )
+ camera_optionmenu.place(relx=0.35, rely=0.86, relwidth=0.25, relheight=0.05)
+
+ live_button = ctk.CTkButton(
+ root,
+ text="Live",
+ cursor="hand2",
+ command=lambda: webcam_preview(
+ root,
+ available_camera_indices[
+ available_camera_strings.index(camera_variable.get())
+ ],
+ ),
+ )
+ live_button.place(relx=0.65, rely=0.86, relwidth=0.2, relheight=0.05)
+ # --- End Camera Selection ---
+
+ status_label = ctk.CTkLabel(root, text=None, justify="center")
status_label.place(relx=0.1, rely=0.9, relwidth=0.8)
- donate_label = ctk.CTkLabel(root, text='Deep Live Cam', justify='center', cursor='hand2')
+ donate_label = ctk.CTkLabel(
+ root, text="Deep Live Cam", justify="center", cursor="hand2"
+ )
donate_label.place(relx=0.1, rely=0.95, relwidth=0.8)
- donate_label.configure(text_color=ctk.ThemeManager.theme.get('URL').get('text_color'))
- donate_label.bind('