import cv2 import gradio as gr import os import tempfile from pathlib import Path def images_to_video(input_files, fps=30): # Create a temporary directory to store images with tempfile.TemporaryDirectory() as temp_dir: # Save uploaded files to temp directory for i, file_info in enumerate(input_files): file_path = os.path.join(temp_dir, f"image_{i}{Path(file_info.name).suffix}") with open(file_path, 'wb') as file: file.write(file_info.read()) # Get all the image files from temp directory images = [img for img in os.listdir(temp_dir) if img.lower().endswith(('.png', '.jpg', '.jpeg'))] if not images: raise ValueError("No images found to create a video.") images.sort() # Sort the images by name # Read the first image to set the video size frame = cv2.imread(os.path.join(temp_dir, images[0])) height, width, layers = frame.shape size = (width, height) # Create a unique filename for the output video video_filename = os.path.join('/mnt/data', 'output_video.mp4') # Initialize the video writer out = cv2.VideoWriter(video_filename, cv2.VideoWriter_fourcc(*'mp4v'), fps, size) # Loop through all images and add to the video for image in images: frame = cv2.imread(os.path.join(temp_dir, image)) out.write(frame) # Release the video writer out.release() # Return the path of the created video file return video_filename # Create the Gradio interface iface = gr.Interface( fn=images_to_video, inputs=[gr.inputs.Image(type="file", label="Upload Images", multiple=True)], outputs=gr.outputs.Video(label="Output Video"), title="Images to Video Converter", description="Upload multiple images to create a video." ) # Launch the interface iface.launch()