Update app.py
Browse files
app.py
CHANGED
|
@@ -6,13 +6,10 @@ import argparse
|
|
| 6 |
import insightface
|
| 7 |
import onnxruntime
|
| 8 |
import gradio as gr
|
| 9 |
-
from tqdm import tqdm
|
| 10 |
-
from moviepy.editor import VideoFileClip
|
| 11 |
-
|
| 12 |
from face_swapper import Inswapper, paste_to_whole
|
| 13 |
from face_analyser import analyse_face
|
| 14 |
from face_enhancer import load_face_enhancer_model, get_available_enhancer_names
|
| 15 |
-
|
| 16 |
|
| 17 |
# ------------------------------ ARGS ------------------------------
|
| 18 |
parser = argparse.ArgumentParser(description="Face Swapper (Multi-target, Male+Female Sources)")
|
|
@@ -59,14 +56,11 @@ def swap_on_frame(frame_bgr, analysed_source_male, analysed_source_female, enhan
|
|
| 59 |
gender = analysed_face.get("gender", 1) # 1 = male, 0 = female
|
| 60 |
src_face = None
|
| 61 |
|
| 62 |
-
# Có cả nam + nữ → swap theo giới tính
|
| 63 |
if analysed_source_male is not None and analysed_source_female is not None:
|
| 64 |
src_face = analysed_source_male if gender == 1 else analysed_source_female
|
| 65 |
-
# Chỉ có nam → chỉ swap nam
|
| 66 |
elif analysed_source_male is not None and analysed_source_female is None:
|
| 67 |
if gender == 1:
|
| 68 |
src_face = analysed_source_male
|
| 69 |
-
# Chỉ có nữ → chỉ swap nữ
|
| 70 |
elif analysed_source_female is not None and analysed_source_male is None:
|
| 71 |
if gender == 0:
|
| 72 |
src_face = analysed_source_female
|
|
@@ -96,23 +90,22 @@ def swap_on_frame(frame_bgr, analysed_source_male, analysed_source_female, enhan
|
|
| 96 |
model, runner = load_face_enhancer_model(name=enhancer_name, device=device)
|
| 97 |
frame_bgr = runner(frame_bgr, model)
|
| 98 |
except Exception as e:
|
| 99 |
-
print(f"[Enhancer]
|
| 100 |
|
| 101 |
return frame_bgr
|
| 102 |
|
| 103 |
-
|
| 104 |
# ------------------------------ PROCESS ------------------------------
|
| 105 |
def swap_faces(target_files, male_file, female_file, enhancer_name="NONE"):
|
| 106 |
start_time = time.time()
|
| 107 |
|
| 108 |
analysed_source_male, analysed_source_female = None, None
|
| 109 |
|
| 110 |
-
#
|
| 111 |
if male_file is not None:
|
| 112 |
male_source_path = male_file.name
|
| 113 |
analysed_source_male = analyse_face(cv2.imread(male_source_path), FACE_ANALYSER)
|
| 114 |
|
| 115 |
-
#
|
| 116 |
if female_file is not None:
|
| 117 |
female_source_path = female_file.name
|
| 118 |
analysed_source_female = analyse_face(cv2.imread(female_source_path), FACE_ANALYSER)
|
|
@@ -120,7 +113,7 @@ def swap_faces(target_files, male_file, female_file, enhancer_name="NONE"):
|
|
| 120 |
if analysed_source_male is None and analysed_source_female is None:
|
| 121 |
raise ValueError("❌ Cần ít nhất 1 khuôn mặt nguồn (Nam hoặc Nữ).")
|
| 122 |
|
| 123 |
-
|
| 124 |
|
| 125 |
for f in target_files:
|
| 126 |
target_path = f.name
|
|
@@ -130,18 +123,15 @@ def swap_faces(target_files, male_file, female_file, enhancer_name="NONE"):
|
|
| 130 |
if ext in [".jpg", ".jpeg", ".png"]:
|
| 131 |
frame_bgr = cv2.imread(target_path)
|
| 132 |
out_frame = swap_on_frame(frame_bgr, analysed_source_male, analysed_source_female, enhancer_name)
|
| 133 |
-
out_images.append(out_frame[:, :, ::-1]) # RGB cho Gradio
|
| 134 |
-
out_videos=f">>> File ok"
|
| 135 |
-
|
| 136 |
-
# -------------------- VIDEO --------------------
|
| 137 |
-
elif ext in [".mp4", ".avi", ".mov"]:
|
| 138 |
-
out_videos=f">>> File errored"
|
| 139 |
-
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
print(f"✔ Hoàn tất tất cả trong {time.time() - start_time:.2f}s")
|
| 143 |
-
return
|
| 144 |
-
|
| 145 |
|
| 146 |
# ------------------------------ UI ------------------------------
|
| 147 |
with gr.Blocks() as demo:
|
|
@@ -149,27 +139,24 @@ with gr.Blocks() as demo:
|
|
| 149 |
|
| 150 |
with gr.Row():
|
| 151 |
with gr.Column():
|
| 152 |
-
target_input = gr.Files(label="Files đích (ảnh
|
| 153 |
male_input = gr.File(label="File nguồn Nam (ảnh)", file_types=[".jpg", ".png"])
|
| 154 |
female_input = gr.File(label="File nguồn Nữ (ảnh)", file_types=[".jpg", ".png"])
|
| 155 |
enhancer = gr.Dropdown(ENHANCER_CHOICES, label="Face Enhancer", value="NONE")
|
| 156 |
run_btn = gr.Button("✨ Swap")
|
| 157 |
|
| 158 |
with gr.Column():
|
| 159 |
-
|
| 160 |
-
output_videos = gr.TextArea(label="Kết quả")
|
| 161 |
|
| 162 |
def run_wrapper(target_files, male_file, female_file, enhancer_name):
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
vids = [None]
|
| 166 |
-
return imgs,vids
|
| 167 |
|
| 168 |
run_btn.click(
|
| 169 |
fn=run_wrapper,
|
| 170 |
inputs=[target_input, male_input, female_input, enhancer],
|
| 171 |
-
outputs=[
|
| 172 |
)
|
| 173 |
|
| 174 |
if __name__ == "__main__":
|
| 175 |
-
demo.launch()
|
|
|
|
| 6 |
import insightface
|
| 7 |
import onnxruntime
|
| 8 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 9 |
from face_swapper import Inswapper, paste_to_whole
|
| 10 |
from face_analyser import analyse_face
|
| 11 |
from face_enhancer import load_face_enhancer_model, get_available_enhancer_names
|
| 12 |
+
import tempfile
|
| 13 |
|
| 14 |
# ------------------------------ ARGS ------------------------------
|
| 15 |
parser = argparse.ArgumentParser(description="Face Swapper (Multi-target, Male+Female Sources)")
|
|
|
|
| 56 |
gender = analysed_face.get("gender", 1) # 1 = male, 0 = female
|
| 57 |
src_face = None
|
| 58 |
|
|
|
|
| 59 |
if analysed_source_male is not None and analysed_source_female is not None:
|
| 60 |
src_face = analysed_source_male if gender == 1 else analysed_source_female
|
|
|
|
| 61 |
elif analysed_source_male is not None and analysed_source_female is None:
|
| 62 |
if gender == 1:
|
| 63 |
src_face = analysed_source_male
|
|
|
|
| 64 |
elif analysed_source_female is not None and analysed_source_male is None:
|
| 65 |
if gender == 0:
|
| 66 |
src_face = analysed_source_female
|
|
|
|
| 90 |
model, runner = load_face_enhancer_model(name=enhancer_name, device=device)
|
| 91 |
frame_bgr = runner(frame_bgr, model)
|
| 92 |
except Exception as e:
|
| 93 |
+
print(f"[Enhancer] Error while running {enhancer_name}: {e}")
|
| 94 |
|
| 95 |
return frame_bgr
|
| 96 |
|
|
|
|
| 97 |
# ------------------------------ PROCESS ------------------------------
|
| 98 |
def swap_faces(target_files, male_file, female_file, enhancer_name="NONE"):
|
| 99 |
start_time = time.time()
|
| 100 |
|
| 101 |
analysed_source_male, analysed_source_female = None, None
|
| 102 |
|
| 103 |
+
# Source male
|
| 104 |
if male_file is not None:
|
| 105 |
male_source_path = male_file.name
|
| 106 |
analysed_source_male = analyse_face(cv2.imread(male_source_path), FACE_ANALYSER)
|
| 107 |
|
| 108 |
+
# Source female
|
| 109 |
if female_file is not None:
|
| 110 |
female_source_path = female_file.name
|
| 111 |
analysed_source_female = analyse_face(cv2.imread(female_source_path), FACE_ANALYSER)
|
|
|
|
| 113 |
if analysed_source_male is None and analysed_source_female is None:
|
| 114 |
raise ValueError("❌ Cần ít nhất 1 khuôn mặt nguồn (Nam hoặc Nữ).")
|
| 115 |
|
| 116 |
+
output_files = []
|
| 117 |
|
| 118 |
for f in target_files:
|
| 119 |
target_path = f.name
|
|
|
|
| 123 |
if ext in [".jpg", ".jpeg", ".png"]:
|
| 124 |
frame_bgr = cv2.imread(target_path)
|
| 125 |
out_frame = swap_on_frame(frame_bgr, analysed_source_male, analysed_source_female, enhancer_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
# Save the output image to a temporary file
|
| 128 |
+
with tempfile.NamedTemporaryFile(suffix=ext, delete=False) as temp_file:
|
| 129 |
+
output_path = temp_file.name
|
| 130 |
+
cv2.imwrite(output_path, out_frame[:, :, ::-1]) # Convert BGR to RGB before saving
|
| 131 |
+
output_files.append(output_path)
|
| 132 |
|
| 133 |
print(f"✔ Hoàn tất tất cả trong {time.time() - start_time:.2f}s")
|
| 134 |
+
return output_files
|
|
|
|
| 135 |
|
| 136 |
# ------------------------------ UI ------------------------------
|
| 137 |
with gr.Blocks() as demo:
|
|
|
|
| 139 |
|
| 140 |
with gr.Row():
|
| 141 |
with gr.Column():
|
| 142 |
+
target_input = gr.Files(label="Files đích (ảnh)", file_types=[".jpg", ".png"])
|
| 143 |
male_input = gr.File(label="File nguồn Nam (ảnh)", file_types=[".jpg", ".png"])
|
| 144 |
female_input = gr.File(label="File nguồn Nữ (ảnh)", file_types=[".jpg", ".png"])
|
| 145 |
enhancer = gr.Dropdown(ENHANCER_CHOICES, label="Face Enhancer", value="NONE")
|
| 146 |
run_btn = gr.Button("✨ Swap")
|
| 147 |
|
| 148 |
with gr.Column():
|
| 149 |
+
output_files = gr.Files(label="Kết quả ảnh")
|
|
|
|
| 150 |
|
| 151 |
def run_wrapper(target_files, male_file, female_file, enhancer_name):
|
| 152 |
+
out_files = swap_faces(target_files, male_file, female_file, enhancer_name)
|
| 153 |
+
return out_files
|
|
|
|
|
|
|
| 154 |
|
| 155 |
run_btn.click(
|
| 156 |
fn=run_wrapper,
|
| 157 |
inputs=[target_input, male_input, female_input, enhancer],
|
| 158 |
+
outputs=[output_files],
|
| 159 |
)
|
| 160 |
|
| 161 |
if __name__ == "__main__":
|
| 162 |
+
demo.launch()
|