File size: 1,699 Bytes
7137422
7633e59
 
7137422
 
7633e59
 
7137422
7633e59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7137422
 
7633e59
e1fa7eb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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")