Spaces:
Running
on
T4
Running
on
T4
Commit
Β·
4d96937
1
Parent(s):
65a97b7
faceswap function rewritten
Browse files
app.py
CHANGED
|
@@ -87,12 +87,14 @@ def face_swap_and_enhance(src_img, tgt_img, fidelity=0.7, background_enhance=Tru
|
|
| 87 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 88 |
os.makedirs(RESULT_DIR, exist_ok=True)
|
| 89 |
|
|
|
|
| 90 |
unique_name = f"swapped_{uuid.uuid4().hex[:8]}.jpg"
|
| 91 |
swapped_path = os.path.join(UPLOAD_DIR, unique_name)
|
| 92 |
swapped_bgr = swapper.get(tgt_bgr, tgt_faces[0], src_faces[0])
|
| 93 |
cv2.imwrite(swapped_path, swapped_bgr)
|
| 94 |
|
| 95 |
-
|
|
|
|
| 96 |
if background_enhance:
|
| 97 |
cmd += " --bg_upsampler realesrgan"
|
| 98 |
if face_upsample:
|
|
@@ -102,24 +104,24 @@ def face_swap_and_enhance(src_img, tgt_img, fidelity=0.7, background_enhance=Tru
|
|
| 102 |
if result.returncode != 0:
|
| 103 |
return None, None, f"β CodeFormer failed:\n{result.stderr}"
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
final_path = os.path.join(root, f)
|
| 110 |
-
break
|
| 111 |
-
if final_path:
|
| 112 |
-
break
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
|
|
|
| 116 |
|
|
|
|
| 117 |
final_img = cv2.cvtColor(cv2.imread(final_path), cv2.COLOR_BGR2RGB)
|
|
|
|
| 118 |
return final_img, final_path, ""
|
| 119 |
|
| 120 |
except Exception as e:
|
| 121 |
return None, None, f"β Error: {str(e)}"
|
| 122 |
|
|
|
|
| 123 |
# -------------------------------------------------
|
| 124 |
# Gradio Interface
|
| 125 |
# -------------------------------------------------
|
|
|
|
| 87 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 88 |
os.makedirs(RESULT_DIR, exist_ok=True)
|
| 89 |
|
| 90 |
+
# ---------------- Save swapped face ----------------
|
| 91 |
unique_name = f"swapped_{uuid.uuid4().hex[:8]}.jpg"
|
| 92 |
swapped_path = os.path.join(UPLOAD_DIR, unique_name)
|
| 93 |
swapped_bgr = swapper.get(tgt_bgr, tgt_faces[0], src_faces[0])
|
| 94 |
cv2.imwrite(swapped_path, swapped_bgr)
|
| 95 |
|
| 96 |
+
# ---------------- Run CodeFormer ----------------
|
| 97 |
+
cmd = f"python {CODEFORMER_PATH} -w {fidelity:.2f} --input_path {swapped_path} --output_path {RESULT_DIR}"
|
| 98 |
if background_enhance:
|
| 99 |
cmd += " --bg_upsampler realesrgan"
|
| 100 |
if face_upsample:
|
|
|
|
| 104 |
if result.returncode != 0:
|
| 105 |
return None, None, f"β CodeFormer failed:\n{result.stderr}"
|
| 106 |
|
| 107 |
+
# ---------------- Locate final result ----------------
|
| 108 |
+
final_results_dir = os.path.join(RESULT_DIR, "final_results")
|
| 109 |
+
if not os.path.exists(final_results_dir):
|
| 110 |
+
return None, None, "β CodeFormer did not produce final results."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
final_files = [f for f in os.listdir(final_results_dir) if f.endswith(".png")]
|
| 113 |
+
if not final_files:
|
| 114 |
+
return None, None, "β No enhanced image found in final results."
|
| 115 |
|
| 116 |
+
final_path = os.path.join(final_results_dir, final_files[0])
|
| 117 |
final_img = cv2.cvtColor(cv2.imread(final_path), cv2.COLOR_BGR2RGB)
|
| 118 |
+
|
| 119 |
return final_img, final_path, ""
|
| 120 |
|
| 121 |
except Exception as e:
|
| 122 |
return None, None, f"β Error: {str(e)}"
|
| 123 |
|
| 124 |
+
|
| 125 |
# -------------------------------------------------
|
| 126 |
# Gradio Interface
|
| 127 |
# -------------------------------------------------
|