Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import shutil
|
|
| 6 |
import subprocess
|
| 7 |
import time
|
| 8 |
import argparse
|
|
|
|
| 9 |
|
| 10 |
# --- LOAD ENGINE ---
|
| 11 |
try:
|
|
@@ -21,12 +22,8 @@ except ImportError:
|
|
| 21 |
|
| 22 |
# --- CSS & THEME CONFIGURATION ---
|
| 23 |
custom_css = """
|
| 24 |
-
body {
|
| 25 |
-
|
| 26 |
-
}
|
| 27 |
-
.gradio-container {
|
| 28 |
-
background-color: #0b0f19 !important;
|
| 29 |
-
}
|
| 30 |
h1.title-header {
|
| 31 |
color: #00ff41;
|
| 32 |
font-family: 'Courier New', Courier, monospace;
|
|
@@ -61,6 +58,28 @@ shinyy_theme = gr.themes.Soft(
|
|
| 61 |
button_primary_text_color="#000000",
|
| 62 |
)
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
# --- LOGIC FUNCTIONS ---
|
| 65 |
|
| 66 |
def swap_single_photo(src_img, src_idx, dst_img, dst_idx, include_hair, progress=gr.Progress(track_tqdm=True)):
|
|
@@ -79,30 +98,25 @@ def swap_single_photo(src_img, src_idx, dst_img, dst_idx, include_hair, progress
|
|
| 79 |
os.makedirs(os.path.dirname(dst_path), exist_ok=True)
|
| 80 |
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
| 81 |
|
| 82 |
-
# Save images
|
| 83 |
cv2.imwrite(src_path, cv2.cvtColor(src_img, cv2.COLOR_RGB2BGR))
|
| 84 |
cv2.imwrite(dst_path, cv2.cvtColor(dst_img, cv2.COLOR_RGB2BGR))
|
| 85 |
|
| 86 |
progress(0.5, desc="Swapping faces" + (" & hair" if include_hair else ""))
|
| 87 |
|
| 88 |
-
# ---
|
| 89 |
result = swapper.swap_faces(src_path, int(src_idx), dst_path, int(dst_idx), swap_hair=include_hair)
|
| 90 |
|
| 91 |
cv2.imwrite(output_path, result)
|
| 92 |
log += f"Swapped{' (with Hair)' if include_hair else ''} and saved.\n"
|
| 93 |
|
| 94 |
progress(1, desc="Done")
|
| 95 |
-
|
| 96 |
-
# Convert BGR back to RGB for Gradio display
|
| 97 |
result_rgb = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
|
| 98 |
return result_rgb, log
|
| 99 |
except Exception as e:
|
| 100 |
log += f"Error: {e}\n"
|
| 101 |
return None, log
|
| 102 |
|
| 103 |
-
# --- REMAINING LOGIC FUNCTIONS (Standard) ---
|
| 104 |
def swap_single_src_multi_dst(src_img, dst_imgs, dst_indices, progress=gr.Progress(track_tqdm=True)):
|
| 105 |
-
# Standard implementation (hair swap disabled for batch stability)
|
| 106 |
log = ""
|
| 107 |
results = []
|
| 108 |
src_dir = "SingleSrcMultiDst/src"
|
|
@@ -129,7 +143,6 @@ def swap_single_src_multi_dst(src_img, dst_imgs, dst_indices, progress=gr.Progre
|
|
| 129 |
dst_path = os.path.join(dst_dir, f"data_dst_{j}.jpg")
|
| 130 |
output_path = os.path.join(output_dir, f"output_swapped_{j}.jpg")
|
| 131 |
cv2.imwrite(dst_path, cv2.cvtColor(dst_img, cv2.COLOR_RGB2BGR))
|
| 132 |
-
|
| 133 |
try:
|
| 134 |
dst_idx = dst_indices_list[j] if j < len(dst_indices_list) else 1
|
| 135 |
result = swapper.swap_faces(src_path, 1, dst_path, int(dst_idx))
|
|
@@ -347,7 +360,7 @@ with gr.Blocks(theme=shinyy_theme, css=custom_css, title="Shinyy's Face Swapper"
|
|
| 347 |
gr.Markdown("""<h1 class="title-header">s h i n y y ' s Face Swapping Tool</h1>""")
|
| 348 |
|
| 349 |
with gr.Tabs():
|
| 350 |
-
# TAB 1: Single Photo
|
| 351 |
with gr.TabItem("📸 Single Photo"):
|
| 352 |
with gr.Row():
|
| 353 |
with gr.Column(variant="panel"):
|
|
@@ -360,7 +373,6 @@ with gr.Blocks(theme=shinyy_theme, css=custom_css, title="Shinyy's Face Swapper"
|
|
| 360 |
sp_dst = gr.Image(label="Target Image", type="numpy", height=300)
|
| 361 |
sp_dst_idx = gr.Number(value=1, label="Face Index", precision=0)
|
| 362 |
|
| 363 |
-
# --- NEW CHECKBOX ---
|
| 364 |
with gr.Row():
|
| 365 |
sp_hair = gr.Checkbox(label="Swap Hair (Beta)", value=False,
|
| 366 |
info="Warps source hair onto target.")
|
|
@@ -451,6 +463,16 @@ with gr.Blocks(theme=shinyy_theme, css=custom_css, title="Shinyy's Face Swapper"
|
|
| 451 |
va_out = gr.Video(label="Result")
|
| 452 |
va_btn.click(swap_video_all_faces, inputs=[va_src, va_target, va_count, vid_del, vid_audio, vid_drive], outputs=[va_out, vid_log])
|
| 453 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
gr.Markdown("---")
|
| 455 |
gr.Markdown("### s h i n y y ' s S u i t e v 2 . 1")
|
| 456 |
|
|
|
|
| 6 |
import subprocess
|
| 7 |
import time
|
| 8 |
import argparse
|
| 9 |
+
import zipfile
|
| 10 |
|
| 11 |
# --- LOAD ENGINE ---
|
| 12 |
try:
|
|
|
|
| 22 |
|
| 23 |
# --- CSS & THEME CONFIGURATION ---
|
| 24 |
custom_css = """
|
| 25 |
+
body { background-color: #0b0f19; }
|
| 26 |
+
.gradio-container { background-color: #0b0f19 !important; }
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
h1.title-header {
|
| 28 |
color: #00ff41;
|
| 29 |
font-family: 'Courier New', Courier, monospace;
|
|
|
|
| 58 |
button_primary_text_color="#000000",
|
| 59 |
)
|
| 60 |
|
| 61 |
+
# --- UTILITY: ZIP PROJECT ---
|
| 62 |
+
def zip_project_directory():
|
| 63 |
+
"""Zips the current working directory for download."""
|
| 64 |
+
zip_filename = "project_backup.zip"
|
| 65 |
+
# Files/Folders to ignore to keep zip clean/small
|
| 66 |
+
ignore_folders = {'.git', '__pycache__', 'venv', 'env', '.ipynb_checkpoints'}
|
| 67 |
+
|
| 68 |
+
with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 69 |
+
for root, dirs, files in os.walk('.'):
|
| 70 |
+
# Remove ignored directories from traversal
|
| 71 |
+
dirs[:] = [d for d in dirs if d not in ignore_folders]
|
| 72 |
+
|
| 73 |
+
for file in files:
|
| 74 |
+
if file == zip_filename: continue # Don't zip the zip itself
|
| 75 |
+
|
| 76 |
+
file_path = os.path.join(root, file)
|
| 77 |
+
# Archive name is the path relative to current directory
|
| 78 |
+
arcname = os.path.relpath(file_path, '.')
|
| 79 |
+
zipf.write(file_path, arcname)
|
| 80 |
+
|
| 81 |
+
return zip_filename
|
| 82 |
+
|
| 83 |
# --- LOGIC FUNCTIONS ---
|
| 84 |
|
| 85 |
def swap_single_photo(src_img, src_idx, dst_img, dst_idx, include_hair, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
| 98 |
os.makedirs(os.path.dirname(dst_path), exist_ok=True)
|
| 99 |
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
| 100 |
|
|
|
|
| 101 |
cv2.imwrite(src_path, cv2.cvtColor(src_img, cv2.COLOR_RGB2BGR))
|
| 102 |
cv2.imwrite(dst_path, cv2.cvtColor(dst_img, cv2.COLOR_RGB2BGR))
|
| 103 |
|
| 104 |
progress(0.5, desc="Swapping faces" + (" & hair" if include_hair else ""))
|
| 105 |
|
| 106 |
+
# --- ENGINE CALL ---
|
| 107 |
result = swapper.swap_faces(src_path, int(src_idx), dst_path, int(dst_idx), swap_hair=include_hair)
|
| 108 |
|
| 109 |
cv2.imwrite(output_path, result)
|
| 110 |
log += f"Swapped{' (with Hair)' if include_hair else ''} and saved.\n"
|
| 111 |
|
| 112 |
progress(1, desc="Done")
|
|
|
|
|
|
|
| 113 |
result_rgb = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
|
| 114 |
return result_rgb, log
|
| 115 |
except Exception as e:
|
| 116 |
log += f"Error: {e}\n"
|
| 117 |
return None, log
|
| 118 |
|
|
|
|
| 119 |
def swap_single_src_multi_dst(src_img, dst_imgs, dst_indices, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
| 120 |
log = ""
|
| 121 |
results = []
|
| 122 |
src_dir = "SingleSrcMultiDst/src"
|
|
|
|
| 143 |
dst_path = os.path.join(dst_dir, f"data_dst_{j}.jpg")
|
| 144 |
output_path = os.path.join(output_dir, f"output_swapped_{j}.jpg")
|
| 145 |
cv2.imwrite(dst_path, cv2.cvtColor(dst_img, cv2.COLOR_RGB2BGR))
|
|
|
|
| 146 |
try:
|
| 147 |
dst_idx = dst_indices_list[j] if j < len(dst_indices_list) else 1
|
| 148 |
result = swapper.swap_faces(src_path, 1, dst_path, int(dst_idx))
|
|
|
|
| 360 |
gr.Markdown("""<h1 class="title-header">s h i n y y ' s Face Swapping Tool</h1>""")
|
| 361 |
|
| 362 |
with gr.Tabs():
|
| 363 |
+
# TAB 1: Single Photo
|
| 364 |
with gr.TabItem("📸 Single Photo"):
|
| 365 |
with gr.Row():
|
| 366 |
with gr.Column(variant="panel"):
|
|
|
|
| 373 |
sp_dst = gr.Image(label="Target Image", type="numpy", height=300)
|
| 374 |
sp_dst_idx = gr.Number(value=1, label="Face Index", precision=0)
|
| 375 |
|
|
|
|
| 376 |
with gr.Row():
|
| 377 |
sp_hair = gr.Checkbox(label="Swap Hair (Beta)", value=False,
|
| 378 |
info="Warps source hair onto target.")
|
|
|
|
| 463 |
va_out = gr.Video(label="Result")
|
| 464 |
va_btn.click(swap_video_all_faces, inputs=[va_src, va_target, va_count, vid_del, vid_audio, vid_drive], outputs=[va_out, vid_log])
|
| 465 |
|
| 466 |
+
# TAB 7: DOWNLOAD
|
| 467 |
+
with gr.TabItem("💾 Download Source Code"):
|
| 468 |
+
gr.Markdown("### Backup & Export")
|
| 469 |
+
gr.Markdown("Click the button below to zip the entire project folder (excluding models and cache) so you can run it on your local PC.")
|
| 470 |
+
|
| 471 |
+
dl_btn = gr.Button("📦 Zip & Download Project", variant="primary")
|
| 472 |
+
dl_file = gr.File(label="Download Zip")
|
| 473 |
+
|
| 474 |
+
dl_btn.click(zip_project_directory, inputs=[], outputs=[dl_file])
|
| 475 |
+
|
| 476 |
gr.Markdown("---")
|
| 477 |
gr.Markdown("### s h i n y y ' s S u i t e v 2 . 1")
|
| 478 |
|