Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,80 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import cv2
|
| 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 |
iface.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import requests
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
def download_video(url):
|
| 7 |
+
# Set up headers to mimic a browser request
|
| 8 |
+
try:
|
| 9 |
+
# Send a GET request to the URL
|
| 10 |
+
response = requests.get(url, headers=headers, stream=True)
|
| 11 |
+
|
| 12 |
+
# Check if the request was successful (status code 200)
|
| 13 |
+
if response.status_code == 200:
|
| 14 |
+
# Define the file path for saving the video; filename can be adjusted as needed
|
| 15 |
+
filename = 'video.mp4'
|
| 16 |
+
with open(filename, 'wb') as f:
|
| 17 |
+
# Write the content in chunks to handle large files efficiently
|
| 18 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 19 |
+
if chunk:
|
| 20 |
+
f.write(chunk)
|
| 21 |
+
# Optional: Add progress indicator or logging here
|
| 22 |
+
print(f"Video saved as {filename}")
|
| 23 |
+
return filename
|
| 24 |
+
else:
|
| 25 |
+
print(f"Download failed with status code {response.status_code}")
|
| 26 |
+
|
| 27 |
+
except requests.exceptions.RequestException as e:
|
| 28 |
+
print(f"An error occurred: {e}")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def extract_frames(video_path):
|
| 34 |
+
video_path = download_video(video_path)
|
| 35 |
+
# Open the video file
|
| 36 |
+
cap = cv2.VideoCapture(video_path)
|
| 37 |
+
if not cap.isOpened():
|
| 38 |
+
# If video cannot be opened, return four None values
|
| 39 |
+
return [None] * 4
|
| 40 |
+
|
| 41 |
+
# Get the total number of frames
|
| 42 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 43 |
+
if total_frames == 0:
|
| 44 |
+
# If video has no frames, release and return None values
|
| 45 |
+
cap.release()
|
| 46 |
+
return [None] * 4
|
| 47 |
+
|
| 48 |
+
# Calculate frame indices at 0%, 25%, 50%, and 75%
|
| 49 |
+
indices = [int(total_frames * i / 4) for i in range(4)]
|
| 50 |
+
frames = []
|
| 51 |
+
|
| 52 |
+
# Extract frames at the calculated indices
|
| 53 |
+
for idx in indices:
|
| 54 |
+
cap.set(cv2.CAP_PROP_POS_FRAMES, idx)
|
| 55 |
+
ret, frame = cap.read()
|
| 56 |
+
if ret:
|
| 57 |
+
# Convert from BGR (OpenCV format) to RGB (Gradio format)
|
| 58 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 59 |
+
frames.append(frame)
|
| 60 |
+
else:
|
| 61 |
+
# If frame extraction fails, append None
|
| 62 |
+
frames.append(None)
|
| 63 |
+
|
| 64 |
+
# Release the video capture object
|
| 65 |
+
cap.release()
|
| 66 |
+
|
| 67 |
+
# Return the four frames as a tuple for Gradio outputs
|
| 68 |
+
return tuple(frames)
|
| 69 |
+
|
| 70 |
+
# Create the Gradio interface
|
| 71 |
+
iface = gr.Interface(
|
| 72 |
+
fn=extract_frames,
|
| 73 |
+
inputs=gr.text(label="Upload a video"),
|
| 74 |
+
outputs=[gr.Image(label=f"Frame at {i*25}%") for i in range(4)],
|
| 75 |
+
title="Video Frame Extractor",
|
| 76 |
+
description="Upload a video to extract four frames from different parts of the video."
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# Launch the web app
|
| 80 |
iface.launch()
|