Spaces:
Running
on
T4
Running
on
T4
Update app.py
Browse files
app.py
CHANGED
|
@@ -150,104 +150,98 @@ swap_lock = threading.Lock()
|
|
| 150 |
def face_swap_and_enhance(src_img, tgt_img, temp_dir="/tmp/faceswap_work"):
|
| 151 |
try:
|
| 152 |
with swap_lock:
|
| 153 |
-
#
|
| 154 |
if os.path.exists(temp_dir):
|
| 155 |
shutil.rmtree(temp_dir)
|
| 156 |
os.makedirs(temp_dir, exist_ok=True)
|
| 157 |
|
| 158 |
src_bgr = cv2.cvtColor(src_img, cv2.COLOR_RGB2BGR)
|
| 159 |
-
|
| 160 |
|
| 161 |
src_faces = face_analysis_app.get(src_bgr)
|
| 162 |
-
tgt_faces = face_analysis_app.get(
|
| 163 |
|
| 164 |
if not src_faces or not tgt_faces:
|
| 165 |
return None, None, "❌ Face not detected in source or target image"
|
| 166 |
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
# --- Adaptive expansion based on face size ---
|
| 171 |
-
def adaptive_bbox(bbox, shape, factor=1.7):
|
| 172 |
-
h, w = shape[:2]
|
| 173 |
x1, y1, x2, y2 = map(int, bbox)
|
| 174 |
-
|
| 175 |
-
cx, cy = x1 +
|
| 176 |
-
new_w, new_h = int(
|
| 177 |
-
nx1
|
| 178 |
-
|
|
|
|
|
|
|
| 179 |
return nx1, ny1, nx2, ny2
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
#
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
swapped_path = os.path.join(temp_dir, f"swapped_{uuid.uuid4().hex}.jpg")
|
| 236 |
-
cv2.imwrite(swapped_path, blended)
|
| 237 |
-
|
| 238 |
-
# --- CodeFormer enhancement ---
|
| 239 |
cmd = f"python {CODEFORMER_PATH} -w 0.7 --input_path {swapped_path} --output_path {temp_dir} --bg_upsampler realesrgan --face_upsample"
|
| 240 |
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
| 241 |
if result.returncode != 0:
|
| 242 |
return None, None, f"❌ CodeFormer failed:\n{result.stderr}"
|
| 243 |
|
| 244 |
-
|
| 245 |
-
final_files = [f for f in os.listdir(
|
| 246 |
if not final_files:
|
| 247 |
-
return None, None, "❌ No enhanced
|
| 248 |
-
final_path = os.path.join(final_dir, final_files[0])
|
| 249 |
|
|
|
|
| 250 |
final_img = cv2.cvtColor(cv2.imread(final_path), cv2.COLOR_BGR2RGB)
|
|
|
|
| 251 |
return final_img, final_path, ""
|
| 252 |
|
| 253 |
except Exception as e:
|
|
@@ -255,8 +249,6 @@ def face_swap_and_enhance(src_img, tgt_img, temp_dir="/tmp/faceswap_work"):
|
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
-
|
| 259 |
-
|
| 260 |
# --------------------- Gradio ---------------------
|
| 261 |
with gr.Blocks() as demo:
|
| 262 |
gr.Markdown("Face Swap")
|
|
|
|
| 150 |
def face_swap_and_enhance(src_img, tgt_img, temp_dir="/tmp/faceswap_work"):
|
| 151 |
try:
|
| 152 |
with swap_lock:
|
| 153 |
+
# Prepare temporary directory
|
| 154 |
if os.path.exists(temp_dir):
|
| 155 |
shutil.rmtree(temp_dir)
|
| 156 |
os.makedirs(temp_dir, exist_ok=True)
|
| 157 |
|
| 158 |
src_bgr = cv2.cvtColor(src_img, cv2.COLOR_RGB2BGR)
|
| 159 |
+
tgt_bgr_full = cv2.cvtColor(tgt_img, cv2.COLOR_RGB2BGR)
|
| 160 |
|
| 161 |
src_faces = face_analysis_app.get(src_bgr)
|
| 162 |
+
tgt_faces = face_analysis_app.get(tgt_bgr_full)
|
| 163 |
|
| 164 |
if not src_faces or not tgt_faces:
|
| 165 |
return None, None, "❌ Face not detected in source or target image"
|
| 166 |
|
| 167 |
+
def expand_bbox(bbox, img_shape, scale=1.6):
|
| 168 |
+
ih, iw = img_shape[:2]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
x1, y1, x2, y2 = map(int, bbox)
|
| 170 |
+
w, h = x2 - x1, y2 - y1
|
| 171 |
+
cx, cy = x1 + w // 2, y1 + h // 2
|
| 172 |
+
new_w, new_h = int(w * scale), int(h * scale)
|
| 173 |
+
nx1 = max(0, cx - new_w // 2)
|
| 174 |
+
ny1 = max(0, cy - new_h // 2)
|
| 175 |
+
nx2 = min(iw, cx + new_w // 2)
|
| 176 |
+
ny2 = min(ih, cy + new_h // 2)
|
| 177 |
return nx1, ny1, nx2, ny2
|
| 178 |
|
| 179 |
+
src_face0 = src_faces[0]
|
| 180 |
+
tgt_face0 = tgt_faces[0]
|
| 181 |
+
|
| 182 |
+
# More accurate source face crop with slight expansion
|
| 183 |
+
s_x1, s_y1, s_x2, s_y2 = expand_bbox(src_face0.bbox, src_bgr.shape, scale=1.4)
|
| 184 |
+
src_crop = src_bgr[s_y1:s_y2, s_x1:s_x2]
|
| 185 |
+
src_crop_faces = face_analysis_app.get(src_crop)
|
| 186 |
+
if src_crop_faces:
|
| 187 |
+
src_for_swap = src_crop
|
| 188 |
+
src_face_for_swap = src_crop_faces[0]
|
| 189 |
+
else:
|
| 190 |
+
src_for_swap = src_bgr
|
| 191 |
+
src_face_for_swap = src_face0
|
| 192 |
+
|
| 193 |
+
# More aggressive target crop for precise landmark detection
|
| 194 |
+
t_x1, t_y1, t_x2, t_y2 = expand_bbox(tgt_face0.bbox, tgt_bgr_full.shape, scale=1.6)
|
| 195 |
+
tgt_crop = tgt_bgr_full[t_y1:t_y2, t_x1:t_x2]
|
| 196 |
+
tgt_crop_faces = face_analysis_app.get(tgt_crop)
|
| 197 |
+
|
| 198 |
+
if tgt_crop_faces:
|
| 199 |
+
tgt_for_swap = tgt_crop
|
| 200 |
+
tgt_face_for_swap = tgt_crop_faces[0]
|
| 201 |
+
|
| 202 |
+
swapped_crop = swapper.get(tgt_for_swap, tgt_face_for_swap, src_face_for_swap)
|
| 203 |
+
if swapped_crop is None:
|
| 204 |
+
return None, None, "❌ Face swap failed on crop"
|
| 205 |
+
|
| 206 |
+
# Create mask with threshold for seamlessClone
|
| 207 |
+
mask = cv2.cvtColor(swapped_crop, cv2.COLOR_BGR2GRAY)
|
| 208 |
+
_, mask = cv2.threshold(mask, 1, 255, cv2.THRESH_BINARY)
|
| 209 |
+
|
| 210 |
+
center = ((t_x1 + t_x2) // 2, (t_y1 + t_y2) // 2)
|
| 211 |
+
|
| 212 |
+
try:
|
| 213 |
+
blended = cv2.seamlessClone(swapped_crop, tgt_bgr_full, mask, center, cv2.NORMAL_CLONE)
|
| 214 |
+
except Exception:
|
| 215 |
+
# Fallback to direct paste if seamlessClone fails
|
| 216 |
+
blended = tgt_bgr_full.copy()
|
| 217 |
+
h, w = swapped_crop.shape[:2]
|
| 218 |
+
blended[t_y1:t_y1+h, t_x1:t_x1+w] = swapped_crop
|
| 219 |
+
|
| 220 |
+
swapped_path = os.path.join(temp_dir, f"swapped_{uuid.uuid4().hex[:8]}.jpg")
|
| 221 |
+
cv2.imwrite(swapped_path, blended)
|
| 222 |
+
|
| 223 |
+
else:
|
| 224 |
+
# Fallback: swap on full image if crop detection fails
|
| 225 |
+
swapped_bgr_full = swapper.get(tgt_bgr_full, tgt_face0, src_face0)
|
| 226 |
+
if swapped_bgr_full is None:
|
| 227 |
+
return None, None, "❌ Face swap failed on full image"
|
| 228 |
+
swapped_path = os.path.join(temp_dir, f"swapped_{uuid.uuid4().hex[:8]}.jpg")
|
| 229 |
+
cv2.imwrite(swapped_path, swapped_bgr_full)
|
| 230 |
+
|
| 231 |
+
# Run CodeFormer enhancement on the swapped image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
cmd = f"python {CODEFORMER_PATH} -w 0.7 --input_path {swapped_path} --output_path {temp_dir} --bg_upsampler realesrgan --face_upsample"
|
| 233 |
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
| 234 |
if result.returncode != 0:
|
| 235 |
return None, None, f"❌ CodeFormer failed:\n{result.stderr}"
|
| 236 |
|
| 237 |
+
final_results_dir = os.path.join(temp_dir, "final_results")
|
| 238 |
+
final_files = [f for f in os.listdir(final_results_dir) if f.endswith(".png")]
|
| 239 |
if not final_files:
|
| 240 |
+
return None, None, "❌ No enhanced image found"
|
|
|
|
| 241 |
|
| 242 |
+
final_path = os.path.join(final_results_dir, final_files[0])
|
| 243 |
final_img = cv2.cvtColor(cv2.imread(final_path), cv2.COLOR_BGR2RGB)
|
| 244 |
+
|
| 245 |
return final_img, final_path, ""
|
| 246 |
|
| 247 |
except Exception as e:
|
|
|
|
| 249 |
|
| 250 |
|
| 251 |
|
|
|
|
|
|
|
| 252 |
# --------------------- Gradio ---------------------
|
| 253 |
with gr.Blocks() as demo:
|
| 254 |
gr.Markdown("Face Swap")
|