Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,55 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
-
import subprocess
|
| 4 |
-
import tempfile
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
def
|
|
|
|
| 8 |
if not video_path:
|
| 9 |
-
return
|
| 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 |
-
outputs=gr.Image(label="Last Frame"),
|
| 53 |
-
title="Instant Last Frame Extractor",
|
| 54 |
-
description="Works instantly even on 6 MB videos!",
|
| 55 |
-
allow_flagging="never",
|
| 56 |
-
cache_examples=False
|
| 57 |
)
|
| 58 |
|
| 59 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
def extract_last_frame(video_path):
|
| 7 |
+
# Check if video_path is None or invalid
|
| 8 |
if not video_path:
|
| 9 |
+
return "Error: No video file uploaded."
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
# Open the video file
|
| 13 |
+
cap = cv2.VideoCapture(video_path)
|
| 14 |
+
|
| 15 |
+
# Check if video opened successfully
|
| 16 |
+
if not cap.isOpened():
|
| 17 |
+
return "Error: Could not open video file."
|
| 18 |
+
|
| 19 |
+
# Get the total number of frames
|
| 20 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 21 |
+
|
| 22 |
+
# Set the frame position to the last frame
|
| 23 |
+
cap.set(cv2.CAP_PROP_POS_FRAMES, total_frames - 1)
|
| 24 |
+
|
| 25 |
+
# Read the last frame
|
| 26 |
+
ret, frame = cap.read()
|
| 27 |
+
|
| 28 |
+
# Release the video capture object
|
| 29 |
+
cap.release()
|
| 30 |
+
|
| 31 |
+
if not ret:
|
| 32 |
+
return "Error: Could not read the last frame."
|
| 33 |
+
|
| 34 |
+
# Convert the frame from BGR (OpenCV format) to RGB
|
| 35 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 36 |
+
|
| 37 |
+
# Convert to PIL Image for Gradio compatibility
|
| 38 |
+
pil_image = Image.fromarray(frame_rgb)
|
| 39 |
+
|
| 40 |
+
return pil_image
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"Error: {str(e)}"
|
| 44 |
+
|
| 45 |
+
# Create the Gradio interface
|
| 46 |
+
interface = gr.Interface(
|
| 47 |
+
fn=extract_last_frame, # Function to call
|
| 48 |
+
inputs=gr.Video(label="Upload Video"), # Input component for video upload
|
| 49 |
+
outputs=gr.Image(label="Last Frame"), # Output component for displaying the image
|
| 50 |
+
title="Extract Last Frame from Video",
|
| 51 |
+
description="Upload a video file to extract and display its last frame."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
)
|
| 53 |
|
| 54 |
+
# Launch the Gradio app
|
| 55 |
+
interface.launch(max_file_size="6MB")
|