# This is a Gradio application for interpolating frames in a video. import gradio as gr import numpy as np import cv2 # Function to interpolate frames in a video def interpolate_frames(video_file, num_interpolations): cap = cv2.VideoCapture(video_file) frames = [] while cap.isOpened(): ret, frame = cap.read() if not ret: break frames.append(frame) cap.release() # Convert frames to numpy array frames = np.array(frames) # Interpolate frames interpolated_frames = [] for i in range(len(frames) - 1): for j in range(num_interpolations + 1): alpha = j / (num_interpolations + 1) interpolated_frame = cv2.addWeighted(frames[i], 1 - alpha, frames[i + 1], alpha, 0) interpolated_frames.append(interpolated_frame) # Save the interpolated video output_video = "interpolated_video.mp4" fourcc = cv2.VideoWriter_fourcc(*'mp4v') out = cv2.VideoWriter(output_video, fourcc, 30.0, (frames.shape[2], frames.shape[1])) for frame in interpolated_frames: out.write(frame) out.release() return output_video # Create a Gradio interface with gr.Blocks() as demo: gr.Markdown("## Video Frame Interpolation") video_input = gr.Video(label="Input Video", sources=["upload", "webcam"]) num_interpolations = gr.Slider(1, 10, step=1, label="Number of Interpolations") video_output = gr.Video(label="Interpolated Video", streaming=True, autoplay=True) start_button = gr.Button("Start Interpolation") # Define the event listener for the start button start_button.click(interpolate_frames, inputs=[video_input, num_interpolations], outputs=video_output) # Launch the interface if __name__ == "__main__": demo.launch(show_error=True)