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
123456venv310/SinglePhoto/MultiSrc/VideoSwapping/VideoSwappingAllFaces/MultiSrcSingleDst/53 unmodified lines545556575758596021 unmodified lines82838485858687889089909134 unmodified lines1261271281311291301311321331371341351361371381391401441411421431441451461471485 unmodified lines154155156157158159157158159164 unmodified lines32432532633033133233333432732832933085 unmodified lines41641741842641942042142242342442542679 unmodified lines506507508513509510511512118 unmodified lines6316326336386346356366376386396406414 unmodified lines64664764865064965065165271 unmodified lines7247257267277287297307 unmodified lines7387397407417427437447 unmodified lines75275375475575675775853 unmodified lines log += f"Elapsed time: {elapsed:.2f} seconds\n" return None, log
def swap_video(src_img, src_idx, video, dst_idx, progress=gr.Progress()):def 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 lines
from VideoSwapping import extract_frames, frames_to_video
# Extract frames only if not already present frame_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 possible swapped_files = set(os.listdir(swapped_dir)) total_frames = len(frame_paths) start_loop_time = time.time()34 unmodified lines log += f"Combined swapped frames into video {output_video_path}\n" progress(0.8, desc="Muxing audio")
# Add audio from original video 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_path # fallback to video without audio final_output_path = output_video_path
try: 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): 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 lines return final_output_path, log
def 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 lines progress(1, desc="Done") return results, log
def 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. """def 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 lines os.remove(src_path) if os.path.exists(dst_video_path): os.remove(dst_video_path) if os.path.exists(frames_dir): 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 lines log += f"Elapsed time: {elapsed:.2f} seconds\n" return output_path, log
def swap_video_custom_mapping(src_imgs, video, mapping_str, progress=gr.Progress()):def 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 lines try: if os.path.exists(dst_video_path): os.remove(dst_video_path) if os.path.exists(frames_dir): 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 lines log += f"Elapsed time: {elapsed:.2f} seconds\n" return final_output_path, log
# Add this to your Gradio UI:# Gradio UIwith gr.Blocks() as demo: gr.Markdown(wellcomingMessage) with gr.Tab("Single Photo Swapping"):71 unmodified lines gr.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 lines gr.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 lines gr.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