Spaces:
Runtime error
Runtime error
Upload 3 files
Browse filesFiles changed (3)
- app.pyAdded+109
- packages.txtAdded+3
- requirements.txtAdded+17
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109import gradio as gr
import roop.globals
import roop.core
import roop.processors.frame.core
import os
import shutil
# Ensure roop thinks it's headless so it doesn't try to launch its own GUI
roop.globals.headless = True
roop.globals.execution_providers = ['cpu'] # Default to CPU, can be changed
roop.globals.execution_threads = 1
roop.globals.frame_processors = ['face_swapper']
roop.globals.keep_fps = True
roop.globals.keep_frames = False
roop.globals.skip_audio = False
roop.globals.many_faces = False
roop.globals.reference_face_position = 0
roop.globals.reference_frame_number = 0
roop.globals.similar_face_distance = 0.85
roop.globals.temp_frame_format = 'png'
roop.globals.temp_frame_quality = 100
roop.globals.output_video_encoder = 'libx264'
roop.globals.output_video_quality = 35
roop.globals.max_memory = None
def swap_face(source_file, target_file, enable_enhancer):
if source_file is None or target_file is None:
return None, None
source_path = source_file if isinstance(source_file, str) else source_file.name
target_path = target_file if isinstance(target_file, str) else target_file.name
# Determine output path based on target extension
target_name, target_ext = os.path.splitext(os.path.basename(target_path))
output_ext = target_ext
is_video_output = False
if target_ext.lower() in ['.jpg', '.jpeg', '.png', '.bmp', '.webp']:
output_ext = '.png'
is_video_output = False
elif target_ext.lower() in ['.mp4', '.avi', '.mov', '.mkv']:
output_ext = '.mp4'
is_video_output = True
output_path = os.path.join(os.getcwd(), f"output_{target_name}{output_ext}")
print(f"Source: {source_path}")
print(f"Target: {target_path}")
print(f"Output: {output_path}")
print(f"Enhancer Enabled: {enable_enhancer}")
# Set globals
roop.globals.source_path = source_path
roop.globals.target_path = target_path
roop.globals.output_path = output_path
roop.globals.execution_providers = ['cpu']
# Configure processors
processors = ['face_swapper']
if enable_enhancer:
processors.append('face_enhancer')
roop.globals.frame_processors = processors
# CRITICAL FIX: Clear the cache of frame processors so it reloads based on the new list
# invalidating the previous run's selection (e.g. if enhancer was disabled previously)
roop.processors.frame.core.FRAME_PROCESSORS_MODULES = []
# Use correct provider name to avoid ONNX Runtime warnings
roop.globals.execution_providers = ['CPUExecutionProvider']
try:
roop.core.start()
if os.path.exists(output_path):
if is_video_output:
return None, output_path
else:
return output_path, None
else:
print("Error: Output file not created.")
return None, None
except Exception as e:
print(f"Error during processing: {e}")
return None, None
# Define the interface
with gr.Blocks(title="Roop Web UI") as demo:
gr.Markdown("# Roop Face Swap Web Interface")
gr.Markdown("Upload a source face and a target image/video to swap faces.")
with gr.Row():
with gr.Column():
source_input = gr.Image(type="filepath", label="Source Face (Image)")
target_input = gr.File(label="Target (Image or Video)", file_types=["image", "video"])
enable_enhancer = gr.Checkbox(label="Enable Face Enhancer (GFPGAN)", value=False)
submit_btn = gr.Button("Swap Face", variant="primary")
with gr.Column():
result_image = gr.Image(label="Result Image", interactive=False)
result_video = gr.Video(label="Result Video", interactive=False, format="mp4")
submit_btn.click(
fn=swap_face,
inputs=[source_input, target_input, enable_enhancer],
outputs=[result_image, result_video]
)
if __name__ == "__main__":
demo.launch(inbrowser=True)123ffmpeg
libsm6
libxext61234567891011121314151617torch
torchvision
torchaudio
tensorflow
opencv-python-headless
insightface
onnx
onnxruntime
customtkinter==5.2.0
psutil
pillow
protobuf
tqdm
gfpgan
opennsfw2
basicsr
gradioapp.pyAdded+109
packages.txtAdded+3
requirements.txtAdded+17