Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,42 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
| 3 |
-
import shutil
|
| 4 |
import os
|
| 5 |
from datetime import datetime
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
def run_scripts(target, source, use_face_enhancer):
|
| 9 |
-
output_images = []
|
| 10 |
-
for target_file in target
|
| 11 |
-
target_extension = os.path.splitext(target_file.name)[-1]
|
| 12 |
-
target_name = os.path.splitext(target_file.name)[1]
|
| 13 |
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
| 14 |
-
output_path1 = "
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
subprocess.run(cmd1)
|
|
|
|
| 18 |
# Mở ảnh đã được xử lý và thêm vào danh sách
|
| 19 |
output_image = Image.open(output_path1)
|
| 20 |
output_images.append(output_image)
|
| 21 |
-
#outputfile.append(output_path1)
|
| 22 |
|
| 23 |
-
return output_images
|
| 24 |
|
|
|
|
| 25 |
iface = gr.Interface(
|
| 26 |
fn=run_scripts,
|
| 27 |
inputs=[
|
| 28 |
-
"
|
| 29 |
-
"
|
|
|
|
| 30 |
],
|
| 31 |
-
outputs="
|
| 32 |
-
title="Face
|
| 33 |
description="Upload a target image/video and a source image to swap faces.",
|
| 34 |
live=False
|
| 35 |
)
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
|
|
|
| 3 |
import os
|
| 4 |
from datetime import datetime
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
def run_scripts(target, source, use_face_enhancer):
|
| 8 |
+
output_images = [] # Danh sách để lưu các ảnh đã xử lý
|
| 9 |
+
for target_file in target:
|
| 10 |
+
target_extension = os.path.splitext(target_file.name)[-1] # Lấy phần mở rộng của tệp
|
|
|
|
| 11 |
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
| 12 |
+
output_path1 = f"output_{timestamp}{target_extension}" # Đường dẫn tệp đầu ra
|
| 13 |
|
| 14 |
+
# Chạy lệnh subprocess để xử lý ảnh
|
| 15 |
+
cmd1 = [
|
| 16 |
+
"python3", "run.py", "-s", source.name, "-t", target_file.name,
|
| 17 |
+
"-o", output_path1, "--frame-processor", "face_swapper", "face_enhancer", "--many-faces"
|
| 18 |
+
]
|
| 19 |
subprocess.run(cmd1)
|
| 20 |
+
|
| 21 |
# Mở ảnh đã được xử lý và thêm vào danh sách
|
| 22 |
output_image = Image.open(output_path1)
|
| 23 |
output_images.append(output_image)
|
|
|
|
| 24 |
|
| 25 |
+
return output_images # Trả về danh sách ảnh đã xử lý
|
| 26 |
|
| 27 |
+
# Giao diện Gradio
|
| 28 |
iface = gr.Interface(
|
| 29 |
fn=run_scripts,
|
| 30 |
inputs=[
|
| 31 |
+
gr.Files(label="Target Files"), # Đầu vào là tệp tin mục tiêu (ảnh/video)
|
| 32 |
+
gr.File(label="Source File"), # Đầu vào là tệp nguồn (ảnh)
|
| 33 |
+
gr.Checkbox(label="Use Face Enhancer") # Tùy chọn cho face enhancer
|
| 34 |
],
|
| 35 |
+
outputs=gr.Images(label="Output Images"), # Đầu ra là các ảnh đã được xử lý
|
| 36 |
+
title="Face Swapper",
|
| 37 |
description="Upload a target image/video and a source image to swap faces.",
|
| 38 |
live=False
|
| 39 |
)
|
| 40 |
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
iface.launch()
|