You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
To integrate the real-time face-swapping (live_swap) functionality more modular and reusable for me, I wrote a separate script, RealTime.py, which imports various existing classes, functions from your project. While the script runs without errors, the face-swapping itself doesn't seem to work as expected.
I would really appreciate it if you could provide some guidance on how to ensure the separate script (RealTime.py) works. Thank you for your time and support!
To Reproduce
Steps to reproduce the behavior:
Create a file named RealTime.py and paste the provided code into it
Download Jackie_Chan.png (an example image used for face-swapping)
Place RealTime.py and Jackie_Chan.png in the root directory of the project.
Run RealTime.py with: python RealTime.py
Face-swapping does not work as expected despite no errors being raised.
Details
What OS are you using?
Linux
Linux in WSL
[√] Windows
Mac
Are you using a GPU?
No. CPU FTW
[√] NVIDIA
AMD
Intel
Mac
Which version of roop unleashed are you using?
Version 4.2.8 (16.9.2024)
Script RealTime.py
import cv2
import numpy as np
import insightface
from insightface.app import FaceAnalysis
import roop.globals
from roop.core import live_swap
from roop.ProcessOptions import ProcessOptions
from roop.utilities import resolve_relative_path, conditional_download
def load_input_face(input_face_path):
# Load the face analysis model and extract faces from the input image
model_path = resolve_relative_path('..')
allowed_modules = roop.globals.g_desired_face_analysis
face_analyser = FaceAnalysis(name="buffalo_l", root=model_path, providers=['CUDAExecutionProvider'], allowed_modules=allowed_modules) # Using CUDA
face_analyser.prepare(ctx_id=0, det_size=(640, 640))
input_image = cv2.imread(input_face_path)
if input_image is None:
print("Error: Unable to load input face image.")
return None
faces = face_analyser.get(input_image)
return faces
def swap_face_with_camera(input_face_path):
# Read and display the input image
original_image = cv2.imread(input_face_path)
if original_image is None:
print("Failed to load input image. Please check the path.")
return
# Load the input image and extract faces
input_face_data = load_input_face(input_face_path)
print(f"Number of faces detected: {len(input_face_data)}") # Output the number of faces detected
if not input_face_data:
print("No faces could be extracted from the input image.")
return
face_data = input_face_data[0]
roop.globals.INPUT_FACESETS = [face_data]
# Configure options
options = ProcessOptions(
processordefines={},
face_distance=roop.globals.distance_threshold,
blend_ratio=roop.globals.blend_ratio,
swap_mode="first",
selected_index=0,
masking_text=None,
imagemask=None,
num_steps=1,
subsample_size=roop.globals.subsample_size,
show_face_area=False,
restore_original_mouth=True,
show_mask=False
)
cap = cv2.VideoCapture(0) # Open the camera
if not cap.isOpened():
print("Unable to access the camera.")
return
while True:
ret, frame = cap.read()
if not ret:
print("Unable to capture frames from the camera.")
break
swapped_frame = live_swap(frame, options)
if swapped_frame is None:
print("Warning: live_swap returned None, no face swap applied.")
swapped_frame = frame # Display the original frame in case of error
# Display the video stream with swapped faces
cv2.imshow("Face Swapped Frame", swapped_frame)
# Press 'q' to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
input_face_path = r'.\Jackie_Chan.png' # Input image path
swap_face_with_camera(input_face_path)
Jackie_Chan.png (an example image used for face-swapping)
Screenshots
The script runs without any errors, and the console shows models loading successfully and detecting one face. However, face-swapping does not work, and the webcam feed displayed by cv2.imshow("Face Swapped Frame", swapped_frame) only shows the original face without any changes.
The text was updated successfully, but these errors were encountered:
Leo000901
changed the title
Real-Time Face Swapping in Custom Script (RealTime.py) Not Working
Real-Time Face Swapping(live_swap) in Custom Script (RealTime.py) Not Working
Dec 4, 2024
Describe the bug
To integrate the real-time face-swapping (live_swap) functionality more modular and reusable for me, I wrote a separate script, RealTime.py, which imports various existing classes, functions from your project. While the script runs without errors, the face-swapping itself doesn't seem to work as expected.
I would really appreciate it if you could provide some guidance on how to ensure the separate script (RealTime.py) works. Thank you for your time and support!
To Reproduce
Steps to reproduce the behavior:
python RealTime.py
Details
What OS are you using?
Are you using a GPU?
Which version of roop unleashed are you using?
Version 4.2.8 (16.9.2024)
Script RealTime.py
Jackie_Chan.png (an example image used for face-swapping)
Screenshots
The script runs without any errors, and the console shows models loading successfully and detecting one face. However, face-swapping does not work, and the webcam feed displayed by
cv2.imshow("Face Swapped Frame", swapped_frame)
only shows the original face without any changes.The text was updated successfully, but these errors were encountered: