Spaces:
Running
Running
Deepro Bardhan
commited on
Commit
·
c2faec4
1
Parent(s):
6d83718
added checkbox to check if user want to delete the video frames dir in video swapping
Browse files- .gitignore +1 -0
- app.py +20 -18
.gitignore
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
venv310/
|
| 2 |
SinglePhoto/
|
|
|
|
| 3 |
VideoSwapping/
|
| 4 |
VideoSwappingAllFaces/
|
| 5 |
MultiSrcSingleDst/
|
|
|
|
| 1 |
venv310/
|
| 2 |
SinglePhoto/
|
| 3 |
+
MultiSrc/
|
| 4 |
VideoSwapping/
|
| 5 |
VideoSwappingAllFaces/
|
| 6 |
MultiSrcSingleDst/
|
app.py
CHANGED
|
@@ -54,7 +54,7 @@ def swap_single_photo(src_img, src_idx, dst_img, dst_idx, progress=gr.Progress(t
|
|
| 54 |
log += f"Elapsed time: {elapsed:.2f} seconds\n"
|
| 55 |
return None, log
|
| 56 |
|
| 57 |
-
def swap_video(src_img, src_idx, video, dst_idx, progress=gr.Progress()):
|
| 58 |
log = ""
|
| 59 |
start_time = time.time()
|
| 60 |
src_path = "VideoSwapping/data_src.jpg"
|
|
@@ -82,12 +82,10 @@ def swap_video(src_img, src_idx, video, dst_idx, progress=gr.Progress()):
|
|
| 82 |
|
| 83 |
from VideoSwapping import extract_frames, frames_to_video
|
| 84 |
|
| 85 |
-
# Extract frames only if not already present
|
| 86 |
frame_paths = extract_frames(dst_video_path, frames_dir)
|
| 87 |
log += f"Extracted {len(frame_paths)} frames to {frames_dir}\n"
|
| 88 |
progress(0.15, desc="Extracted frames")
|
| 89 |
|
| 90 |
-
# Prepare swapped frames list and resume if possible
|
| 91 |
swapped_files = set(os.listdir(swapped_dir))
|
| 92 |
total_frames = len(frame_paths)
|
| 93 |
start_loop_time = time.time()
|
|
@@ -128,21 +126,23 @@ def swap_video(src_img, src_idx, video, dst_idx, progress=gr.Progress()):
|
|
| 128 |
log += f"Combined swapped frames into video {output_video_path}\n"
|
| 129 |
progress(0.8, desc="Muxing audio")
|
| 130 |
|
| 131 |
-
# Add audio from original video
|
| 132 |
ok, audio_log = add_audio_to_video(dst_video_path, output_video_path, final_output_path)
|
| 133 |
if ok:
|
| 134 |
log += f"Added audio to {final_output_path}\n"
|
| 135 |
else:
|
| 136 |
log += f"Audio muxing failed: {audio_log}\n"
|
| 137 |
-
final_output_path = output_video_path
|
| 138 |
|
| 139 |
try:
|
| 140 |
if os.path.exists(src_path):
|
| 141 |
os.remove(src_path)
|
| 142 |
if os.path.exists(dst_video_path):
|
| 143 |
os.remove(dst_video_path)
|
| 144 |
-
if os.path.exists(frames_dir):
|
| 145 |
shutil.rmtree(frames_dir)
|
|
|
|
|
|
|
|
|
|
| 146 |
if os.path.exists(swapped_dir):
|
| 147 |
shutil.rmtree(swapped_dir)
|
| 148 |
log += "Cleaned up temp files and folders.\n"
|
|
@@ -154,9 +154,6 @@ def swap_video(src_img, src_idx, video, dst_idx, progress=gr.Progress()):
|
|
| 154 |
return final_output_path, log
|
| 155 |
|
| 156 |
def add_audio_to_video(original_video_path, video_no_audio_path, output_path):
|
| 157 |
-
"""
|
| 158 |
-
Uses ffmpeg to mux audio from original_video_path into video_no_audio_path.
|
| 159 |
-
"""
|
| 160 |
cmd = [
|
| 161 |
"ffmpeg",
|
| 162 |
"-y",
|
|
@@ -327,11 +324,7 @@ def swap_single_src_multi_dst(src_img, dst_imgs, dst_indices, progress=gr.Progre
|
|
| 327 |
progress(1, desc="Done")
|
| 328 |
return results, log
|
| 329 |
|
| 330 |
-
def swap_video_all_faces(src_img, video, num_faces_to_swap, progress=gr.Progress()):
|
| 331 |
-
"""
|
| 332 |
-
Swaps the single source image to all faces in each frame of the destination video.
|
| 333 |
-
For each frame, swaps one by one, always using the latest swapped image for the next face.
|
| 334 |
-
"""
|
| 335 |
log = ""
|
| 336 |
start_time = time.time()
|
| 337 |
src_path = "VideoSwappingAllFaces/data_src.jpg"
|
|
@@ -423,8 +416,11 @@ def swap_video_all_faces(src_img, video, num_faces_to_swap, progress=gr.Progress
|
|
| 423 |
os.remove(src_path)
|
| 424 |
if os.path.exists(dst_video_path):
|
| 425 |
os.remove(dst_video_path)
|
| 426 |
-
if os.path.exists(frames_dir):
|
| 427 |
shutil.rmtree(frames_dir)
|
|
|
|
|
|
|
|
|
|
| 428 |
if os.path.exists(swapped_dir):
|
| 429 |
shutil.rmtree(swapped_dir)
|
| 430 |
log += "Cleaned up temp files and folders.\n"
|
|
@@ -510,7 +506,7 @@ def swap_faces_custom(src_imgs, dst_img, mapping_str, progress=gr.Progress(track
|
|
| 510 |
log += f"Elapsed time: {elapsed:.2f} seconds\n"
|
| 511 |
return output_path, log
|
| 512 |
|
| 513 |
-
def swap_video_custom_mapping(src_imgs, video, mapping_str, progress=gr.Progress()):
|
| 514 |
"""
|
| 515 |
Swaps faces in each frame of the video according to the user mapping.
|
| 516 |
src_imgs: list of source images (numpy arrays)
|
|
@@ -635,8 +631,11 @@ def swap_video_custom_mapping(src_imgs, video, mapping_str, progress=gr.Progress
|
|
| 635 |
try:
|
| 636 |
if os.path.exists(dst_video_path):
|
| 637 |
os.remove(dst_video_path)
|
| 638 |
-
if os.path.exists(frames_dir):
|
| 639 |
shutil.rmtree(frames_dir)
|
|
|
|
|
|
|
|
|
|
| 640 |
if os.path.exists(swapped_dir):
|
| 641 |
shutil.rmtree(swapped_dir)
|
| 642 |
log += "Cleaned up temp files and folders.\n"
|
|
@@ -647,7 +646,7 @@ def swap_video_custom_mapping(src_imgs, video, mapping_str, progress=gr.Progress
|
|
| 647 |
log += f"Elapsed time: {elapsed:.2f} seconds\n"
|
| 648 |
return final_output_path, log
|
| 649 |
|
| 650 |
-
#
|
| 651 |
with gr.Blocks() as demo:
|
| 652 |
gr.Markdown(wellcomingMessage)
|
| 653 |
with gr.Tab("Single Photo Swapping"):
|
|
@@ -725,6 +724,7 @@ with gr.Blocks() as demo:
|
|
| 725 |
gr.Number(value=1, label="Source Face Index"),
|
| 726 |
gr.Video(label="Target Video"),
|
| 727 |
gr.Number(value=1, label="Destination Face Index"),
|
|
|
|
| 728 |
],
|
| 729 |
outputs=[
|
| 730 |
gr.Video(label="Swapped Video"),
|
|
@@ -738,6 +738,7 @@ with gr.Blocks() as demo:
|
|
| 738 |
gr.Image(label="Source Image"),
|
| 739 |
gr.Video(label="Target Video"),
|
| 740 |
gr.Number(value=1, label="Number of Faces to Swap"),
|
|
|
|
| 741 |
],
|
| 742 |
outputs=[
|
| 743 |
gr.Video(label="Swapped Video"),
|
|
@@ -751,6 +752,7 @@ with gr.Blocks() as demo:
|
|
| 751 |
gr.Gallery(label="Source Images", type="numpy", columns=3),
|
| 752 |
gr.Video(label="Target Video"),
|
| 753 |
gr.Textbox(label="Mapping (comma-separated, e.g. 2,1,3)"),
|
|
|
|
| 754 |
],
|
| 755 |
outputs=[
|
| 756 |
gr.Video(label="Swapped Video"),
|
|
|
|
| 54 |
log += f"Elapsed time: {elapsed:.2f} seconds\n"
|
| 55 |
return None, log
|
| 56 |
|
| 57 |
+
def swap_video(src_img, src_idx, video, dst_idx, delete_frames_dir=True, progress=gr.Progress()):
|
| 58 |
log = ""
|
| 59 |
start_time = time.time()
|
| 60 |
src_path = "VideoSwapping/data_src.jpg"
|
|
|
|
| 82 |
|
| 83 |
from VideoSwapping import extract_frames, frames_to_video
|
| 84 |
|
|
|
|
| 85 |
frame_paths = extract_frames(dst_video_path, frames_dir)
|
| 86 |
log += f"Extracted {len(frame_paths)} frames to {frames_dir}\n"
|
| 87 |
progress(0.15, desc="Extracted frames")
|
| 88 |
|
|
|
|
| 89 |
swapped_files = set(os.listdir(swapped_dir))
|
| 90 |
total_frames = len(frame_paths)
|
| 91 |
start_loop_time = time.time()
|
|
|
|
| 126 |
log += f"Combined swapped frames into video {output_video_path}\n"
|
| 127 |
progress(0.8, desc="Muxing audio")
|
| 128 |
|
|
|
|
| 129 |
ok, audio_log = add_audio_to_video(dst_video_path, output_video_path, final_output_path)
|
| 130 |
if ok:
|
| 131 |
log += f"Added audio to {final_output_path}\n"
|
| 132 |
else:
|
| 133 |
log += f"Audio muxing failed: {audio_log}\n"
|
| 134 |
+
final_output_path = output_video_path
|
| 135 |
|
| 136 |
try:
|
| 137 |
if os.path.exists(src_path):
|
| 138 |
os.remove(src_path)
|
| 139 |
if os.path.exists(dst_video_path):
|
| 140 |
os.remove(dst_video_path)
|
| 141 |
+
if delete_frames_dir and os.path.exists(frames_dir):
|
| 142 |
shutil.rmtree(frames_dir)
|
| 143 |
+
log += "Deleted video_frames directory.\n"
|
| 144 |
+
elif not delete_frames_dir:
|
| 145 |
+
log += "Kept video_frames directory as requested.\n"
|
| 146 |
if os.path.exists(swapped_dir):
|
| 147 |
shutil.rmtree(swapped_dir)
|
| 148 |
log += "Cleaned up temp files and folders.\n"
|
|
|
|
| 154 |
return final_output_path, log
|
| 155 |
|
| 156 |
def add_audio_to_video(original_video_path, video_no_audio_path, output_path):
|
|
|
|
|
|
|
|
|
|
| 157 |
cmd = [
|
| 158 |
"ffmpeg",
|
| 159 |
"-y",
|
|
|
|
| 324 |
progress(1, desc="Done")
|
| 325 |
return results, log
|
| 326 |
|
| 327 |
+
def swap_video_all_faces(src_img, video, num_faces_to_swap, delete_frames_dir=True, progress=gr.Progress()):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
log = ""
|
| 329 |
start_time = time.time()
|
| 330 |
src_path = "VideoSwappingAllFaces/data_src.jpg"
|
|
|
|
| 416 |
os.remove(src_path)
|
| 417 |
if os.path.exists(dst_video_path):
|
| 418 |
os.remove(dst_video_path)
|
| 419 |
+
if delete_frames_dir and os.path.exists(frames_dir):
|
| 420 |
shutil.rmtree(frames_dir)
|
| 421 |
+
log += "Deleted video_frames directory.\n"
|
| 422 |
+
elif not delete_frames_dir:
|
| 423 |
+
log += "Kept video_frames directory as requested.\n"
|
| 424 |
if os.path.exists(swapped_dir):
|
| 425 |
shutil.rmtree(swapped_dir)
|
| 426 |
log += "Cleaned up temp files and folders.\n"
|
|
|
|
| 506 |
log += f"Elapsed time: {elapsed:.2f} seconds\n"
|
| 507 |
return output_path, log
|
| 508 |
|
| 509 |
+
def swap_video_custom_mapping(src_imgs, video, mapping_str, delete_frames_dir=True, progress=gr.Progress()):
|
| 510 |
"""
|
| 511 |
Swaps faces in each frame of the video according to the user mapping.
|
| 512 |
src_imgs: list of source images (numpy arrays)
|
|
|
|
| 631 |
try:
|
| 632 |
if os.path.exists(dst_video_path):
|
| 633 |
os.remove(dst_video_path)
|
| 634 |
+
if delete_frames_dir and os.path.exists(frames_dir):
|
| 635 |
shutil.rmtree(frames_dir)
|
| 636 |
+
log += "Deleted video_frames directory.\n"
|
| 637 |
+
elif not delete_frames_dir:
|
| 638 |
+
log += "Kept video_frames directory as requested.\n"
|
| 639 |
if os.path.exists(swapped_dir):
|
| 640 |
shutil.rmtree(swapped_dir)
|
| 641 |
log += "Cleaned up temp files and folders.\n"
|
|
|
|
| 646 |
log += f"Elapsed time: {elapsed:.2f} seconds\n"
|
| 647 |
return final_output_path, log
|
| 648 |
|
| 649 |
+
# Gradio UI
|
| 650 |
with gr.Blocks() as demo:
|
| 651 |
gr.Markdown(wellcomingMessage)
|
| 652 |
with gr.Tab("Single Photo Swapping"):
|
|
|
|
| 724 |
gr.Number(value=1, label="Source Face Index"),
|
| 725 |
gr.Video(label="Target Video"),
|
| 726 |
gr.Number(value=1, label="Destination Face Index"),
|
| 727 |
+
gr.Checkbox(label="Delete video_frames directory after processing", value=True)
|
| 728 |
],
|
| 729 |
outputs=[
|
| 730 |
gr.Video(label="Swapped Video"),
|
|
|
|
| 738 |
gr.Image(label="Source Image"),
|
| 739 |
gr.Video(label="Target Video"),
|
| 740 |
gr.Number(value=1, label="Number of Faces to Swap"),
|
| 741 |
+
gr.Checkbox(label="Delete video_frames directory after processing", value=True)
|
| 742 |
],
|
| 743 |
outputs=[
|
| 744 |
gr.Video(label="Swapped Video"),
|
|
|
|
| 752 |
gr.Gallery(label="Source Images", type="numpy", columns=3),
|
| 753 |
gr.Video(label="Target Video"),
|
| 754 |
gr.Textbox(label="Mapping (comma-separated, e.g. 2,1,3)"),
|
| 755 |
+
gr.Checkbox(label="Delete video_frames directory after processing", value=True)
|
| 756 |
],
|
| 757 |
outputs=[
|
| 758 |
gr.Video(label="Swapped Video"),
|