Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Real-Time Face Swapping(live_swap) in Custom Script (RealTime.py) Not Working #1010

Open
7 tasks
Leo000901 opened this issue Dec 4, 2024 · 2 comments
Open
7 tasks

Comments

@Leo000901
Copy link

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:

  1. Create a file named RealTime.py and paste the provided code into it
  2. Download Jackie_Chan.png (an example image used for face-swapping)
  3. Place RealTime.py and Jackie_Chan.png in the root directory of the project.
  4. Run RealTime.py with:
    python RealTime.py
  5. 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)
Jackie_Chan

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.

Screenshot

@Leo000901 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
Copy link

github-actions bot commented Jan 4, 2025

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label Jan 4, 2025
@Leo000901
Copy link
Author

This issue is still relevant

@github-actions github-actions bot removed the Stale label Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant