File size: 1,653 Bytes
21f0a5b
 
 
52dcdf4
da232e2
21f0a5b
 
920f0eb
 
 
52dcdf4
920f0eb
52dcdf4
920f0eb
 
 
 
 
21f0a5b
920f0eb
5c9cabb
 
 
21f0a5b
920f0eb
21f0a5b
920f0eb
21f0a5b
 
 
920f0eb
 
 
21f0a5b
1cf060c
920f0eb
21f0a5b
0c93b15
21f0a5b
 
920f0eb
 
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
import gradio as gr
import subprocess
import os
from datetime import datetime
from PIL import Image

def run_scripts(target, source, use_face_enhancer):
    output_images = []  # Danh sách để lưu các ảnh đã xử lý
    for target_file in target:
        target_extension = os.path.splitext(target_file.name)[-1]  # Lấy phần mở rộng của tệp
        timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
        output_path1 = f"output_{timestamp}{target_extension}"  # Đường dẫn tệp đầu ra

        # Chạy lệnh subprocess để xử lý ảnh
        cmd1 = [
            "python3", "run.py", "-s", source.name, "-t", target_file.name,
            "-o", output_path1, "--frame-processor", "face_swapper", "face_enhancer", "--many-faces"
        ]
        subprocess.run(cmd1)

        # Mở ảnh đã được xử lý và thêm vào danh sách
        output_image = Image.open(output_path1)
        output_images.append(output_image)

    return output_images  # Trả về danh sách ảnh đã xử lý

# Giao diện Gradio
iface = gr.Interface(
    fn=run_scripts,
    inputs=[
        gr.Files(label="Target Files"),  # Đầu vào là tệp tin mục tiêu (ảnh/video)
        gr.File(label="Source File"),    # Đầu vào là tệp nguồn (ảnh)
        gr.Checkbox(label="Use Face Enhancer")  # Tùy chọn cho face enhancer
    ],
    outputs=gr.Gallery(label="Output Images"),  # Đầu ra là các ảnh đã được xử lý
    title="Face Swapper",
    description="Upload a target image/video and a source image to swap faces.",
    live=False
)

if __name__ == "__main__":
    iface.launch()