Spaces:
Running
on
T4
Running
on
T4
Commit
Β·
d8fb1f1
1
Parent(s):
4d96937
revised app.py
Browse files
app.py
CHANGED
|
@@ -72,7 +72,10 @@ ensure_codeformer()
|
|
| 72 |
# -------------------------------------------------
|
| 73 |
# Pipeline Function
|
| 74 |
# -------------------------------------------------
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
| 76 |
try:
|
| 77 |
src_bgr = cv2.cvtColor(src_img, cv2.COLOR_RGB2BGR)
|
| 78 |
tgt_bgr = cv2.cvtColor(tgt_img, cv2.COLOR_RGB2BGR)
|
|
@@ -94,12 +97,7 @@ def face_swap_and_enhance(src_img, tgt_img, fidelity=0.7, background_enhance=Tru
|
|
| 94 |
cv2.imwrite(swapped_path, swapped_bgr)
|
| 95 |
|
| 96 |
# ---------------- Run CodeFormer ----------------
|
| 97 |
-
cmd = f"python {CODEFORMER_PATH} -w
|
| 98 |
-
if background_enhance:
|
| 99 |
-
cmd += " --bg_upsampler realesrgan"
|
| 100 |
-
if face_upsample:
|
| 101 |
-
cmd += " --face_upsample"
|
| 102 |
-
|
| 103 |
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
| 104 |
if result.returncode != 0:
|
| 105 |
return None, None, f"β CodeFormer failed:\n{result.stderr}"
|
|
@@ -123,28 +121,25 @@ def face_swap_and_enhance(src_img, tgt_img, fidelity=0.7, background_enhance=Tru
|
|
| 123 |
|
| 124 |
|
| 125 |
# -------------------------------------------------
|
| 126 |
-
# Gradio Interface
|
| 127 |
# -------------------------------------------------
|
| 128 |
with gr.Blocks() as demo:
|
| 129 |
-
gr.Markdown("
|
|
|
|
| 130 |
with gr.Row():
|
| 131 |
-
src_input = gr.Image(type="numpy", label="Upload
|
| 132 |
tgt_input = gr.Image(type="numpy", label="Upload Target Image")
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
bg = gr.Checkbox(value=True, label="Enhance Background")
|
| 136 |
-
face_up = gr.Checkbox(value=True, label="Face Upsample")
|
| 137 |
-
btn = gr.Button("π Run Face Swap + Enhance")
|
| 138 |
|
| 139 |
output_img = gr.Image(type="numpy", label="Enhanced Output")
|
| 140 |
download = gr.File(label="β¬οΈ Download Enhanced Image")
|
| 141 |
error_box = gr.Textbox(label="Logs / Errors", interactive=False)
|
| 142 |
|
| 143 |
-
def process(src, tgt
|
| 144 |
-
img, path, err = face_swap_and_enhance(src, tgt
|
| 145 |
return img, path, err
|
| 146 |
|
| 147 |
-
btn.click(process, [src_input, tgt_input,
|
| 148 |
-
[output_img, download, error_box])
|
| 149 |
|
| 150 |
-
demo.launch()
|
|
|
|
| 72 |
# -------------------------------------------------
|
| 73 |
# Pipeline Function
|
| 74 |
# -------------------------------------------------
|
| 75 |
+
# -------------------------------------------------
|
| 76 |
+
# Pipeline Function (defaults applied)
|
| 77 |
+
# -------------------------------------------------
|
| 78 |
+
def face_swap_and_enhance(src_img, tgt_img):
|
| 79 |
try:
|
| 80 |
src_bgr = cv2.cvtColor(src_img, cv2.COLOR_RGB2BGR)
|
| 81 |
tgt_bgr = cv2.cvtColor(tgt_img, cv2.COLOR_RGB2BGR)
|
|
|
|
| 97 |
cv2.imwrite(swapped_path, swapped_bgr)
|
| 98 |
|
| 99 |
# ---------------- Run CodeFormer ----------------
|
| 100 |
+
cmd = f"python {CODEFORMER_PATH} -w 0.7 --input_path {swapped_path} --output_path {RESULT_DIR} --bg_upsampler realesrgan --face_upsample"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
| 102 |
if result.returncode != 0:
|
| 103 |
return None, None, f"β CodeFormer failed:\n{result.stderr}"
|
|
|
|
| 121 |
|
| 122 |
|
| 123 |
# -------------------------------------------------
|
| 124 |
+
# Gradio Interface (simplified)
|
| 125 |
# -------------------------------------------------
|
| 126 |
with gr.Blocks() as demo:
|
| 127 |
+
gr.Markdown("Face Swap")
|
| 128 |
+
|
| 129 |
with gr.Row():
|
| 130 |
+
src_input = gr.Image(type="numpy", label="Upload Your Face")
|
| 131 |
tgt_input = gr.Image(type="numpy", label="Upload Target Image")
|
| 132 |
+
|
| 133 |
+
btn = gr.Button("Swap Face")
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
output_img = gr.Image(type="numpy", label="Enhanced Output")
|
| 136 |
download = gr.File(label="β¬οΈ Download Enhanced Image")
|
| 137 |
error_box = gr.Textbox(label="Logs / Errors", interactive=False)
|
| 138 |
|
| 139 |
+
def process(src, tgt):
|
| 140 |
+
img, path, err = face_swap_and_enhance(src, tgt)
|
| 141 |
return img, path, err
|
| 142 |
|
| 143 |
+
btn.click(process, [src_input, tgt_input], [output_img, download, error_box])
|
|
|
|
| 144 |
|
| 145 |
+
demo.launch()
|