Spaces:
Runtime error
Runtime error
Commit ·
c2faec4
1
Parent(s): 6d83718
added checkbox to check if user want to delete the video frames dir in video swapping
Browse filesFiles changed (2)
- .gitignore+1
- app.py+20-18
12345venv310/SinglePhoto/VideoSwapping/VideoSwappingAllFaces/MultiSrcSingleDst/123456venv310/SinglePhoto/MultiSrc/VideoSwapping/VideoSwappingAllFaces/MultiSrcSingleDst/
53 unmodified lines5455565758596021 unmodified lines82838485868788899091929334 unmodified lines1281291301311321331341351361371381391401411421431441451461471485 unmodified lines154155156157158159160161162164 unmodified lines32732832933033133233333433533633785 unmodified lines42342442542642742842943079 unmodified lines510511512513514515516118 unmodified lines6356366376386396406416424 unmodified lines64764864965065165265371 unmodified lines7257267277287297307 unmodified lines7387397407417427437 unmodified lines75175275375475575653 unmodified lineslog += f"Elapsed time: {elapsed:.2f} seconds\n"return None, logdef swap_video(src_img, src_idx, video, dst_idx, progress=gr.Progress()):log = ""start_time = time.time()src_path = "VideoSwapping/data_src.jpg"21 unmodified linesfrom VideoSwapping import extract_frames, frames_to_video# Extract frames only if not already presentframe_paths = extract_frames(dst_video_path, frames_dir)log += f"Extracted {len(frame_paths)} frames to {frames_dir}\n"progress(0.15, desc="Extracted frames")# Prepare swapped frames list and resume if possibleswapped_files = set(os.listdir(swapped_dir))total_frames = len(frame_paths)start_loop_time = time.time()34 unmodified lineslog += f"Combined swapped frames into video {output_video_path}\n"progress(0.8, desc="Muxing audio")# Add audio from original videook, audio_log = add_audio_to_video(dst_video_path, output_video_path, final_output_path)if ok:log += f"Added audio to {final_output_path}\n"else:log += f"Audio muxing failed: {audio_log}\n"final_output_path = output_video_path # fallback to video without audiotry:if os.path.exists(src_path):os.remove(src_path)if os.path.exists(dst_video_path):os.remove(dst_video_path)if os.path.exists(frames_dir):shutil.rmtree(frames_dir)if os.path.exists(swapped_dir):shutil.rmtree(swapped_dir)log += "Cleaned up temp files and folders.\n"5 unmodified linesreturn final_output_path, logdef add_audio_to_video(original_video_path, video_no_audio_path, output_path):"""Uses ffmpeg to mux audio from original_video_path into video_no_audio_path."""cmd = ["ffmpeg","-y",164 unmodified linesprogress(1, desc="Done")return results, logdef swap_video_all_faces(src_img, video, num_faces_to_swap, progress=gr.Progress()):"""Swaps the single source image to all faces in each frame of the destination video.For each frame, swaps one by one, always using the latest swapped image for the next face."""log = ""start_time = time.time()src_path = "VideoSwappingAllFaces/data_src.jpg"85 unmodified linesos.remove(src_path)if os.path.exists(dst_video_path):os.remove(dst_video_path)if os.path.exists(frames_dir):shutil.rmtree(frames_dir)if os.path.exists(swapped_dir):shutil.rmtree(swapped_dir)log += "Cleaned up temp files and folders.\n"79 unmodified lineslog += f"Elapsed time: {elapsed:.2f} seconds\n"return output_path, logdef swap_video_custom_mapping(src_imgs, video, mapping_str, progress=gr.Progress()):"""Swaps faces in each frame of the video according to the user mapping.src_imgs: list of source images (numpy arrays)118 unmodified linestry:if os.path.exists(dst_video_path):os.remove(dst_video_path)if os.path.exists(frames_dir):shutil.rmtree(frames_dir)if os.path.exists(swapped_dir):shutil.rmtree(swapped_dir)log += "Cleaned up temp files and folders.\n"4 unmodified lineslog += f"Elapsed time: {elapsed:.2f} seconds\n"return final_output_path, log# Add this to your Gradio UI:with gr.Blocks() as demo:gr.Markdown(wellcomingMessage)with gr.Tab("Single Photo Swapping"):71 unmodified linesgr.Number(value=1, label="Source Face Index"),gr.Video(label="Target Video"),gr.Number(value=1, label="Destination Face Index"),],outputs=[gr.Video(label="Swapped Video"),7 unmodified linesgr.Image(label="Source Image"),gr.Video(label="Target Video"),gr.Number(value=1, label="Number of Faces to Swap"),],outputs=[gr.Video(label="Swapped Video"),7 unmodified linesgr.Gallery(label="Source Images", type="numpy", columns=3),gr.Video(label="Target Video"),gr.Textbox(label="Mapping (comma-separated, e.g. 2,1,3)"),],outputs=[gr.Video(label="Swapped Video"),53 unmodified lines5455565758596021 unmodified lines8283848586878889909134 unmodified lines1261271281291301311321331341351361371381391401411421431441451461471485 unmodified lines154155156157158159164 unmodified lines32432532632732832933085 unmodified lines41641741841942042142242342442542679 unmodified lines506507508509510511512118 unmodified lines6316326336346356366376386396406414 unmodified lines64664764864965065165271 unmodified lines7247257267277287297307 unmodified lines7387397407417427437447 unmodified lines75275375475575675775853 unmodified lineslog += f"Elapsed time: {elapsed:.2f} seconds\n"return None, logdef swap_video(src_img, src_idx, video, dst_idx, delete_frames_dir=True, progress=gr.Progress()):log = ""start_time = time.time()src_path = "VideoSwapping/data_src.jpg"21 unmodified linesfrom VideoSwapping import extract_frames, frames_to_videoframe_paths = extract_frames(dst_video_path, frames_dir)log += f"Extracted {len(frame_paths)} frames to {frames_dir}\n"progress(0.15, desc="Extracted frames")swapped_files = set(os.listdir(swapped_dir))total_frames = len(frame_paths)start_loop_time = time.time()34 unmodified lineslog += f"Combined swapped frames into video {output_video_path}\n"progress(0.8, desc="Muxing audio")ok, audio_log = add_audio_to_video(dst_video_path, output_video_path, final_output_path)if ok:log += f"Added audio to {final_output_path}\n"else:log += f"Audio muxing failed: {audio_log}\n"final_output_path = output_video_pathtry:if os.path.exists(src_path):os.remove(src_path)if os.path.exists(dst_video_path):os.remove(dst_video_path)if delete_frames_dir and os.path.exists(frames_dir):shutil.rmtree(frames_dir)log += "Deleted video_frames directory.\n"elif not delete_frames_dir:log += "Kept video_frames directory as requested.\n"if os.path.exists(swapped_dir):shutil.rmtree(swapped_dir)log += "Cleaned up temp files and folders.\n"5 unmodified linesreturn final_output_path, logdef add_audio_to_video(original_video_path, video_no_audio_path, output_path):cmd = ["ffmpeg","-y",164 unmodified linesprogress(1, desc="Done")return results, logdef swap_video_all_faces(src_img, video, num_faces_to_swap, delete_frames_dir=True, progress=gr.Progress()):log = ""start_time = time.time()src_path = "VideoSwappingAllFaces/data_src.jpg"85 unmodified linesos.remove(src_path)if os.path.exists(dst_video_path):os.remove(dst_video_path)if delete_frames_dir and os.path.exists(frames_dir):shutil.rmtree(frames_dir)log += "Deleted video_frames directory.\n"elif not delete_frames_dir:log += "Kept video_frames directory as requested.\n"if os.path.exists(swapped_dir):shutil.rmtree(swapped_dir)log += "Cleaned up temp files and folders.\n"79 unmodified lineslog += f"Elapsed time: {elapsed:.2f} seconds\n"return output_path, logdef swap_video_custom_mapping(src_imgs, video, mapping_str, delete_frames_dir=True, progress=gr.Progress()):"""Swaps faces in each frame of the video according to the user mapping.src_imgs: list of source images (numpy arrays)118 unmodified linestry:if os.path.exists(dst_video_path):os.remove(dst_video_path)if delete_frames_dir and os.path.exists(frames_dir):shutil.rmtree(frames_dir)log += "Deleted video_frames directory.\n"elif not delete_frames_dir:log += "Kept video_frames directory as requested.\n"if os.path.exists(swapped_dir):shutil.rmtree(swapped_dir)log += "Cleaned up temp files and folders.\n"4 unmodified lineslog += f"Elapsed time: {elapsed:.2f} seconds\n"return final_output_path, log# Gradio UIwith gr.Blocks() as demo:gr.Markdown(wellcomingMessage)with gr.Tab("Single Photo Swapping"):71 unmodified linesgr.Number(value=1, label="Source Face Index"),gr.Video(label="Target Video"),gr.Number(value=1, label="Destination Face Index"),gr.Checkbox(label="Delete video_frames directory after processing", value=True)],outputs=[gr.Video(label="Swapped Video"),7 unmodified linesgr.Image(label="Source Image"),gr.Video(label="Target Video"),gr.Number(value=1, label="Number of Faces to Swap"),gr.Checkbox(label="Delete video_frames directory after processing", value=True)],outputs=[gr.Video(label="Swapped Video"),7 unmodified linesgr.Gallery(label="Source Images", type="numpy", columns=3),gr.Video(label="Target Video"),gr.Textbox(label="Mapping (comma-separated, e.g. 2,1,3)"),gr.Checkbox(label="Delete video_frames directory after processing", value=True)],outputs=[gr.Video(label="Swapped Video"),
.gitignore+1
app.py+20-18