| import gradio as gr |
| import cv2 |
| import numpy as np |
| from PIL import Image |
|
|
| def extract_last_frame(video_path): |
| |
| if not video_path: |
| return "Error: No video file uploaded." |
|
|
| try: |
| |
| cap = cv2.VideoCapture(video_path) |
| |
| |
| if not cap.isOpened(): |
| return "Error: Could not open video file." |
|
|
| |
| total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) |
| |
| |
| cap.set(cv2.CAP_PROP_POS_FRAMES, total_frames - 1) |
| |
| |
| ret, frame = cap.read() |
| |
| |
| cap.release() |
| |
| if not ret: |
| return "Error: Could not read the last frame." |
| |
| |
| frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) |
| |
| |
| pil_image = Image.fromarray(frame_rgb) |
| |
| return pil_image |
| |
| except Exception as e: |
| return f"Error: {str(e)}" |
|
|
| |
| interface = gr.Interface( |
| fn=extract_last_frame, |
| inputs=gr.Video(label="Upload Video"), |
| outputs=gr.Image(label="Last Frame"), |
| title="Extract Last Frame from Video", |
| description="Upload a video file to extract and display its last frame." |
| ) |
|
|
| |
| interface.launch(max_file_size="9MB") |