Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import cv2 | |
| import numpy as np | |
| from PIL import Image | |
| def extract_last_frame(video_path): | |
| # Check if video_path is None or invalid | |
| if not video_path: | |
| return "Error: No video file uploaded." | |
| try: | |
| # Open the video file | |
| cap = cv2.VideoCapture(video_path) | |
| # Check if video opened successfully | |
| if not cap.isOpened(): | |
| return "Error: Could not open video file." | |
| # Get the total number of frames | |
| total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) | |
| # Set the frame position to the last frame | |
| cap.set(cv2.CAP_PROP_POS_FRAMES, total_frames - 1) | |
| # Read the last frame | |
| ret, frame = cap.read() | |
| # Release the video capture object | |
| cap.release() | |
| if not ret: | |
| return "Error: Could not read the last frame." | |
| # Convert the frame from BGR (OpenCV format) to RGB | |
| frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | |
| # Convert to PIL Image for Gradio compatibility | |
| pil_image = Image.fromarray(frame_rgb) | |
| return pil_image | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # Create the Gradio interface | |
| interface = gr.Interface( | |
| fn=extract_last_frame, # Function to call | |
| inputs=gr.Video(label="Upload Video"), # Input component for video upload | |
| outputs=gr.Image(label="Last Frame"), # Output component for displaying the image | |
| title="Extract Last Frame from Video", | |
| description="Upload a video file to extract and display its last frame." | |
| ) | |
| # Launch the Gradio app | |
| interface.launch(max_file_size="9MB") |