File size: 1,644 Bytes
453bb82 3b1f0f3 17ae24a ab98968 efc5319 518d9ff 453bb82 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import gradio as gr
import subprocess
import shutil
import os
from PIL import Image
import numpy as np
# Ensure output directory exists
output_dir = "output"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Login function for authentication
def custom_auth(username, password):
return password == "aitutor"
def run_scripts(target, source, use_face_enhancer):
if target is None or (not use_face_enhancer and source is None):
return None
target_extension = os.path.splitext(target.name)[-1]
output_path1 = os.path.join(output_dir, "output1" + target_extension)
output_path2 = os.path.join(output_dir, "output2" + target_extension)
if not use_face_enhancer:
cmd1 = ["python3", "run.py", "-s", source.name, "-t", target.name, "-o", output_path1, "--frame-processor", "face_swapper"]
subprocess.run(cmd1)
cmd2 = ["python3", "run.py", "-t", target.name if use_face_enhancer else output_path1, "-o", output_path2, "--frame-processor", "face_enhancer"]
subprocess.run(cmd2)
if not use_face_enhancer:
os.remove(source.name)
os.remove(target.name)
# Open the image with PIL and convert to NumPy array
with Image.open(output_path2) as img:
img_array = np.array(img)
return img_array
iface = gr.Interface(
fn=run_scripts,
inputs=[
"file",
"file",
gr.inputs.Checkbox(default=False, label="Use only Face Enhancer")
],
outputs="image",
title="Pixio Swap",
description="Upload a target image/video and a source image to swap faces.",
live=True
)
iface.launch(debug=True, auth=custom_auth)
|