Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
-
# -* coding:UTF-8
|
| 2 |
# !/usr/bin/env python
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import gradio as gr
|
| 5 |
import roop.globals
|
|
@@ -13,31 +14,50 @@ from roop.processors.frame.core import get_frame_processors_modules
|
|
| 13 |
from roop.utilities import normalize_output_path
|
| 14 |
import os
|
| 15 |
from PIL import Image
|
|
|
|
| 16 |
|
| 17 |
|
| 18 |
-
def swap_face(source_file, target_file,doFaceEnhancer):
|
|
|
|
|
|
|
| 19 |
|
|
|
|
| 20 |
source_path = "input.jpg"
|
| 21 |
-
target_path = "target.jpg"
|
| 22 |
-
|
| 23 |
source_image = Image.fromarray(source_file)
|
| 24 |
source_image.save(source_path)
|
| 25 |
-
target_image = Image.fromarray(target_file)
|
| 26 |
-
target_image.save(target_path)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
roop.globals.source_path = source_path
|
| 32 |
roop.globals.target_path = target_path
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
roop.globals.output_path = normalize_output_path(
|
| 35 |
roop.globals.source_path, roop.globals.target_path, output_path
|
| 36 |
)
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
roop.globals.headless = True
|
| 42 |
roop.globals.keep_fps = True
|
| 43 |
roop.globals.keep_audio = True
|
|
@@ -60,13 +80,25 @@ def swap_face(source_file, target_file,doFaceEnhancer):
|
|
| 60 |
roop.globals.frame_processors
|
| 61 |
):
|
| 62 |
if not frame_processor.pre_check():
|
| 63 |
-
return
|
| 64 |
|
| 65 |
start()
|
| 66 |
-
return output_path
|
| 67 |
|
| 68 |
|
| 69 |
app = gr.Interface(
|
| 70 |
-
fn=swap_face,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
)
|
| 72 |
-
|
|
|
|
|
|
| 1 |
+
# -* coding:UTF-8 *-
|
| 2 |
# !/usr/bin/env python
|
| 3 |
+
|
| 4 |
import numpy as np
|
| 5 |
import gradio as gr
|
| 6 |
import roop.globals
|
|
|
|
| 14 |
from roop.utilities import normalize_output_path
|
| 15 |
import os
|
| 16 |
from PIL import Image
|
| 17 |
+
import mimetypes
|
| 18 |
|
| 19 |
|
| 20 |
+
def swap_face(source_file, target_file, swap_type, doFaceEnhancer):
|
| 21 |
+
supported_images = ['.jpg', '.jpeg', '.png']
|
| 22 |
+
supported_videos = ['.mp4', '.mov', '.avi']
|
| 23 |
|
| 24 |
+
# Save source image
|
| 25 |
source_path = "input.jpg"
|
|
|
|
|
|
|
| 26 |
source_image = Image.fromarray(source_file)
|
| 27 |
source_image.save(source_path)
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Save target as image or video
|
| 30 |
+
mime_type, _ = mimetypes.guess_type(target_file.name)
|
| 31 |
+
ext = os.path.splitext(target_file.name)[1].lower()
|
| 32 |
+
|
| 33 |
+
if mime_type and "video" in mime_type:
|
| 34 |
+
target_path = "target" + ext
|
| 35 |
+
with open(target_path, "wb") as f:
|
| 36 |
+
f.write(target_file.read())
|
| 37 |
+
else:
|
| 38 |
+
target_path = "target.jpg"
|
| 39 |
+
target_image = Image.fromarray(target_file)
|
| 40 |
+
target_image.save(target_path)
|
| 41 |
+
|
| 42 |
+
print("source_path:", source_path)
|
| 43 |
+
print("target_path:", target_path)
|
| 44 |
|
| 45 |
roop.globals.source_path = source_path
|
| 46 |
roop.globals.target_path = target_path
|
| 47 |
+
|
| 48 |
+
# Output path based on type
|
| 49 |
+
if "video" in swap_type.lower() or ext in supported_videos:
|
| 50 |
+
output_path = "output.mp4"
|
| 51 |
+
else:
|
| 52 |
+
output_path = "output.jpg"
|
| 53 |
+
|
| 54 |
roop.globals.output_path = normalize_output_path(
|
| 55 |
roop.globals.source_path, roop.globals.target_path, output_path
|
| 56 |
)
|
| 57 |
+
|
| 58 |
+
roop.globals.frame_processors = (
|
| 59 |
+
["face_swapper", "face_enhancer"] if doFaceEnhancer else ["face_swapper"]
|
| 60 |
+
)
|
| 61 |
roop.globals.headless = True
|
| 62 |
roop.globals.keep_fps = True
|
| 63 |
roop.globals.keep_audio = True
|
|
|
|
| 80 |
roop.globals.frame_processors
|
| 81 |
):
|
| 82 |
if not frame_processor.pre_check():
|
| 83 |
+
return "Frame processor pre-check failed"
|
| 84 |
|
| 85 |
start()
|
| 86 |
+
return roop.globals.output_path
|
| 87 |
|
| 88 |
|
| 89 |
app = gr.Interface(
|
| 90 |
+
fn=swap_face,
|
| 91 |
+
inputs=[
|
| 92 |
+
gr.Image(label="Source Image"),
|
| 93 |
+
gr.File(label="Target (Image or Video)"),
|
| 94 |
+
gr.Radio(choices=["Image to Image", "Image to Video"], label="Swap Type"),
|
| 95 |
+
gr.Checkbox(label="Face Enhancer?", info="Apply face enhancement")
|
| 96 |
+
],
|
| 97 |
+
outputs=[
|
| 98 |
+
gr.Image(label="Output Image") if not os.path.exists("output.mp4") else gr.Video(label="Output Video")
|
| 99 |
+
],
|
| 100 |
+
title="Face Swap Tool (Roop)",
|
| 101 |
+
description="Swap face from source image to a target image or video using Roop.",
|
| 102 |
)
|
| 103 |
+
|
| 104 |
+
app.launch()
|