Deepro Bardhan commited on
Commit ·
ee01137
1
Parent(s): 655b242
added progress bar
Browse files
app.py
CHANGED
|
@@ -13,9 +13,10 @@ wellcomingMessage = """
|
|
| 13 |
|
| 14 |
swapper = FaceSwapper()
|
| 15 |
|
| 16 |
-
def swap_single_photo(src_img, src_idx, dst_img, dst_idx):
|
| 17 |
log = ""
|
| 18 |
try:
|
|
|
|
| 19 |
src_path = "SinglePhoto/data_src.jpg"
|
| 20 |
dst_path = "SinglePhoto/data_dst.jpg"
|
| 21 |
output_path = "SinglePhoto/output_swapped.jpg"
|
|
@@ -27,9 +28,11 @@ def swap_single_photo(src_img, src_idx, dst_img, dst_idx):
|
|
| 27 |
cv2.imwrite(src_path, src_img_bgr)
|
| 28 |
cv2.imwrite(dst_path, dst_img_bgr)
|
| 29 |
log += f"Saved source to {src_path}, destination to {dst_path}\n"
|
|
|
|
| 30 |
result = swapper.swap_faces(src_path, int(src_idx), dst_path, int(dst_idx))
|
| 31 |
cv2.imwrite(output_path, result)
|
| 32 |
log += f"Swapped and saved result to {output_path}\n"
|
|
|
|
| 33 |
try:
|
| 34 |
if os.path.exists(src_path):
|
| 35 |
os.remove(src_path)
|
|
@@ -38,12 +41,14 @@ def swap_single_photo(src_img, src_idx, dst_img, dst_idx):
|
|
| 38 |
log += "Cleaned up temp files.\n"
|
| 39 |
except Exception as cleanup_error:
|
| 40 |
log += f"Cleanup error: {cleanup_error}\n"
|
|
|
|
| 41 |
return output_path, log
|
| 42 |
except Exception as e:
|
| 43 |
log += f"Error: {e}\n"
|
|
|
|
| 44 |
return None, log
|
| 45 |
|
| 46 |
-
def swap_video(src_img, src_idx, video, dst_idx):
|
| 47 |
log = ""
|
| 48 |
src_path = "VideoSwapping/data_src.jpg"
|
| 49 |
dst_video_path = "VideoSwapping/data_dst.mp4"
|
|
@@ -60,6 +65,7 @@ def swap_video(src_img, src_idx, video, dst_idx):
|
|
| 60 |
src_img_bgr = cv2.cvtColor(src_img, cv2.COLOR_RGB2BGR)
|
| 61 |
cv2.imwrite(src_path, src_img_bgr)
|
| 62 |
log += f"Saved source image to {src_path}\n"
|
|
|
|
| 63 |
|
| 64 |
if isinstance(video, str) and os.path.exists(video):
|
| 65 |
shutil.copy(video, dst_video_path)
|
|
@@ -70,6 +76,8 @@ def swap_video(src_img, src_idx, video, dst_idx):
|
|
| 70 |
from VideoSwapping import extract_frames, frames_to_video
|
| 71 |
frame_paths = extract_frames(dst_video_path, frames_dir)
|
| 72 |
log += f"Extracted {len(frame_paths)} frames to {frames_dir}\n"
|
|
|
|
|
|
|
| 73 |
for idx, frame_path in enumerate(frame_paths):
|
| 74 |
out_path = os.path.join(swapped_dir, f"swapped_{idx:05d}.jpg")
|
| 75 |
try:
|
|
@@ -79,11 +87,13 @@ def swap_video(src_img, src_idx, video, dst_idx):
|
|
| 79 |
except Exception as e:
|
| 80 |
cv2.imwrite(out_path, cv2.imread(frame_path))
|
| 81 |
log += f"Failed to swap frame {idx}: {e}\n"
|
|
|
|
| 82 |
cap = cv2.VideoCapture(dst_video_path)
|
| 83 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 84 |
cap.release()
|
| 85 |
frames_to_video(swapped_dir, output_video_path, fps)
|
| 86 |
log += f"Combined swapped frames into video {output_video_path}\n"
|
|
|
|
| 87 |
|
| 88 |
# Add audio from original video
|
| 89 |
ok, audio_log = add_audio_to_video(dst_video_path, output_video_path, final_output_path)
|
|
@@ -105,7 +115,7 @@ def swap_video(src_img, src_idx, video, dst_idx):
|
|
| 105 |
log += "Cleaned up temp files and folders.\n"
|
| 106 |
except Exception as cleanup_error:
|
| 107 |
log += f"Cleanup error: {cleanup_error}\n"
|
| 108 |
-
|
| 109 |
return final_output_path, log
|
| 110 |
|
| 111 |
def add_audio_to_video(original_video_path, video_no_audio_path, output_path):
|
|
@@ -130,7 +140,7 @@ def add_audio_to_video(original_video_path, video_no_audio_path, output_path):
|
|
| 130 |
except subprocess.CalledProcessError as e:
|
| 131 |
return False, e.stderr.decode()
|
| 132 |
|
| 133 |
-
def swap_multi_src_single_dst(src_imgs, dst_img, dst_idx):
|
| 134 |
log = ""
|
| 135 |
results = []
|
| 136 |
src_dir = "MultiSrcSingleDst/src"
|
|
@@ -146,6 +156,7 @@ def swap_multi_src_single_dst(src_imgs, dst_img, dst_idx):
|
|
| 146 |
dst_path = os.path.join(dst_dir, "data_dst.jpg")
|
| 147 |
cv2.imwrite(dst_path, dst_img_bgr)
|
| 148 |
log += f"Saved destination image to {dst_path}\n"
|
|
|
|
| 149 |
|
| 150 |
for i, src_img in enumerate(src_imgs):
|
| 151 |
if isinstance(src_img, tuple):
|
|
@@ -163,9 +174,11 @@ def swap_multi_src_single_dst(src_imgs, dst_img, dst_idx):
|
|
| 163 |
except Exception as e:
|
| 164 |
results.append(f"Error: {e}")
|
| 165 |
log += f"Error swapping source {i}: {e}\n"
|
|
|
|
|
|
|
| 166 |
return results, log
|
| 167 |
|
| 168 |
-
def swap_multi_src_multi_dst(src_imgs, dst_imgs, dst_indices):
|
| 169 |
log = ""
|
| 170 |
results = []
|
| 171 |
src_dir = "MultiSrcMultiDst/src"
|
|
@@ -180,6 +193,8 @@ def swap_multi_src_multi_dst(src_imgs, dst_imgs, dst_indices):
|
|
| 180 |
else:
|
| 181 |
dst_indices_list = [int(idx) for idx in dst_indices]
|
| 182 |
|
|
|
|
|
|
|
| 183 |
for i, src_img in enumerate(src_imgs):
|
| 184 |
if isinstance(src_img, tuple):
|
| 185 |
src_img = src_img[0]
|
|
@@ -228,9 +243,12 @@ def swap_multi_src_multi_dst(src_imgs, dst_imgs, dst_indices):
|
|
| 228 |
except Exception as e:
|
| 229 |
results.append(f"Error: {e}")
|
| 230 |
log += f"Error swapping src {i} with dst {j}: {e}\n"
|
|
|
|
|
|
|
|
|
|
| 231 |
return results, log
|
| 232 |
|
| 233 |
-
def swap_single_src_multi_dst(src_img, dst_imgs, dst_indices):
|
| 234 |
log = ""
|
| 235 |
results = []
|
| 236 |
src_dir = "SingleSrcMultiDst/src"
|
|
@@ -246,6 +264,7 @@ def swap_single_src_multi_dst(src_img, dst_imgs, dst_indices):
|
|
| 246 |
src_img_bgr = cv2.cvtColor(src_img, cv2.COLOR_RGB2BGR)
|
| 247 |
cv2.imwrite(src_path, src_img_bgr)
|
| 248 |
log += f"Saved source image to {src_path}\n"
|
|
|
|
| 249 |
|
| 250 |
if isinstance(dst_indices, str):
|
| 251 |
dst_indices_list = [int(idx.strip()) for idx in dst_indices.split(",") if idx.strip().isdigit()]
|
|
@@ -269,6 +288,8 @@ def swap_single_src_multi_dst(src_img, dst_imgs, dst_indices):
|
|
| 269 |
except Exception as e:
|
| 270 |
results.append(f"Error: {e}")
|
| 271 |
log += f"Error swapping with destination {j}: {e}\n"
|
|
|
|
|
|
|
| 272 |
return results, log
|
| 273 |
|
| 274 |
with gr.Blocks() as demo:
|
|
|
|
| 13 |
|
| 14 |
swapper = FaceSwapper()
|
| 15 |
|
| 16 |
+
def swap_single_photo(src_img, src_idx, dst_img, dst_idx, progress=gr.Progress(track_tqdm=True)):
|
| 17 |
log = ""
|
| 18 |
try:
|
| 19 |
+
progress(0, desc="Preparing files")
|
| 20 |
src_path = "SinglePhoto/data_src.jpg"
|
| 21 |
dst_path = "SinglePhoto/data_dst.jpg"
|
| 22 |
output_path = "SinglePhoto/output_swapped.jpg"
|
|
|
|
| 28 |
cv2.imwrite(src_path, src_img_bgr)
|
| 29 |
cv2.imwrite(dst_path, dst_img_bgr)
|
| 30 |
log += f"Saved source to {src_path}, destination to {dst_path}\n"
|
| 31 |
+
progress(0.5, desc="Swapping faces")
|
| 32 |
result = swapper.swap_faces(src_path, int(src_idx), dst_path, int(dst_idx))
|
| 33 |
cv2.imwrite(output_path, result)
|
| 34 |
log += f"Swapped and saved result to {output_path}\n"
|
| 35 |
+
progress(0.8, desc="Cleaning up")
|
| 36 |
try:
|
| 37 |
if os.path.exists(src_path):
|
| 38 |
os.remove(src_path)
|
|
|
|
| 41 |
log += "Cleaned up temp files.\n"
|
| 42 |
except Exception as cleanup_error:
|
| 43 |
log += f"Cleanup error: {cleanup_error}\n"
|
| 44 |
+
progress(1, desc="Done")
|
| 45 |
return output_path, log
|
| 46 |
except Exception as e:
|
| 47 |
log += f"Error: {e}\n"
|
| 48 |
+
progress(1, desc="Error")
|
| 49 |
return None, log
|
| 50 |
|
| 51 |
+
def swap_video(src_img, src_idx, video, dst_idx, progress=gr.Progress(track_tqdm=True)):
|
| 52 |
log = ""
|
| 53 |
src_path = "VideoSwapping/data_src.jpg"
|
| 54 |
dst_video_path = "VideoSwapping/data_dst.mp4"
|
|
|
|
| 65 |
src_img_bgr = cv2.cvtColor(src_img, cv2.COLOR_RGB2BGR)
|
| 66 |
cv2.imwrite(src_path, src_img_bgr)
|
| 67 |
log += f"Saved source image to {src_path}\n"
|
| 68 |
+
progress(0.05, desc="Saved source image")
|
| 69 |
|
| 70 |
if isinstance(video, str) and os.path.exists(video):
|
| 71 |
shutil.copy(video, dst_video_path)
|
|
|
|
| 76 |
from VideoSwapping import extract_frames, frames_to_video
|
| 77 |
frame_paths = extract_frames(dst_video_path, frames_dir)
|
| 78 |
log += f"Extracted {len(frame_paths)} frames to {frames_dir}\n"
|
| 79 |
+
progress(0.15, desc="Extracted frames")
|
| 80 |
+
|
| 81 |
for idx, frame_path in enumerate(frame_paths):
|
| 82 |
out_path = os.path.join(swapped_dir, f"swapped_{idx:05d}.jpg")
|
| 83 |
try:
|
|
|
|
| 87 |
except Exception as e:
|
| 88 |
cv2.imwrite(out_path, cv2.imread(frame_path))
|
| 89 |
log += f"Failed to swap frame {idx}: {e}\n"
|
| 90 |
+
progress(0.15 + 0.6 * (idx + 1) / len(frame_paths), desc=f"Swapping frames ({idx+1}/{len(frame_paths)})")
|
| 91 |
cap = cv2.VideoCapture(dst_video_path)
|
| 92 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 93 |
cap.release()
|
| 94 |
frames_to_video(swapped_dir, output_video_path, fps)
|
| 95 |
log += f"Combined swapped frames into video {output_video_path}\n"
|
| 96 |
+
progress(0.8, desc="Muxing audio")
|
| 97 |
|
| 98 |
# Add audio from original video
|
| 99 |
ok, audio_log = add_audio_to_video(dst_video_path, output_video_path, final_output_path)
|
|
|
|
| 115 |
log += "Cleaned up temp files and folders.\n"
|
| 116 |
except Exception as cleanup_error:
|
| 117 |
log += f"Cleanup error: {cleanup_error}\n"
|
| 118 |
+
progress(1, desc="Done")
|
| 119 |
return final_output_path, log
|
| 120 |
|
| 121 |
def add_audio_to_video(original_video_path, video_no_audio_path, output_path):
|
|
|
|
| 140 |
except subprocess.CalledProcessError as e:
|
| 141 |
return False, e.stderr.decode()
|
| 142 |
|
| 143 |
+
def swap_multi_src_single_dst(src_imgs, dst_img, dst_idx, progress=gr.Progress(track_tqdm=True)):
|
| 144 |
log = ""
|
| 145 |
results = []
|
| 146 |
src_dir = "MultiSrcSingleDst/src"
|
|
|
|
| 156 |
dst_path = os.path.join(dst_dir, "data_dst.jpg")
|
| 157 |
cv2.imwrite(dst_path, dst_img_bgr)
|
| 158 |
log += f"Saved destination image to {dst_path}\n"
|
| 159 |
+
progress(0.05, desc="Saved destination image")
|
| 160 |
|
| 161 |
for i, src_img in enumerate(src_imgs):
|
| 162 |
if isinstance(src_img, tuple):
|
|
|
|
| 174 |
except Exception as e:
|
| 175 |
results.append(f"Error: {e}")
|
| 176 |
log += f"Error swapping source {i}: {e}\n"
|
| 177 |
+
progress((i + 1) / len(src_imgs), desc=f"Swapping source {i+1}/{len(src_imgs)}")
|
| 178 |
+
progress(1, desc="Done")
|
| 179 |
return results, log
|
| 180 |
|
| 181 |
+
def swap_multi_src_multi_dst(src_imgs, dst_imgs, dst_indices, progress=gr.Progress(track_tqdm=True)):
|
| 182 |
log = ""
|
| 183 |
results = []
|
| 184 |
src_dir = "MultiSrcMultiDst/src"
|
|
|
|
| 193 |
else:
|
| 194 |
dst_indices_list = [int(idx) for idx in dst_indices]
|
| 195 |
|
| 196 |
+
total = max(1, len(src_imgs) * len(dst_imgs))
|
| 197 |
+
count = 0
|
| 198 |
for i, src_img in enumerate(src_imgs):
|
| 199 |
if isinstance(src_img, tuple):
|
| 200 |
src_img = src_img[0]
|
|
|
|
| 243 |
except Exception as e:
|
| 244 |
results.append(f"Error: {e}")
|
| 245 |
log += f"Error swapping src {i} with dst {j}: {e}\n"
|
| 246 |
+
count += 1
|
| 247 |
+
progress(count / total, desc=f"Swapping ({count}/{total})")
|
| 248 |
+
progress(1, desc="Done")
|
| 249 |
return results, log
|
| 250 |
|
| 251 |
+
def swap_single_src_multi_dst(src_img, dst_imgs, dst_indices, progress=gr.Progress(track_tqdm=True)):
|
| 252 |
log = ""
|
| 253 |
results = []
|
| 254 |
src_dir = "SingleSrcMultiDst/src"
|
|
|
|
| 264 |
src_img_bgr = cv2.cvtColor(src_img, cv2.COLOR_RGB2BGR)
|
| 265 |
cv2.imwrite(src_path, src_img_bgr)
|
| 266 |
log += f"Saved source image to {src_path}\n"
|
| 267 |
+
progress(0.05, desc="Saved source image")
|
| 268 |
|
| 269 |
if isinstance(dst_indices, str):
|
| 270 |
dst_indices_list = [int(idx.strip()) for idx in dst_indices.split(",") if idx.strip().isdigit()]
|
|
|
|
| 288 |
except Exception as e:
|
| 289 |
results.append(f"Error: {e}")
|
| 290 |
log += f"Error swapping with destination {j}: {e}\n"
|
| 291 |
+
progress((j + 1) / len(dst_imgs), desc=f"Swapping destination {j+1}/{len(dst_imgs)}")
|
| 292 |
+
progress(1, desc="Done")
|
| 293 |
return results, log
|
| 294 |
|
| 295 |
with gr.Blocks() as demo:
|