|
import gradio as gr |
|
import cv2 |
|
import requests |
|
import os |
|
|
|
def download_video(url): |
|
|
|
try: |
|
|
|
response = requests.get(url, stream=True) |
|
|
|
|
|
if response.status_code == 200: |
|
|
|
filename = 'video.mp4' |
|
with open(filename, 'wb') as f: |
|
|
|
for chunk in response.iter_content(chunk_size=8192): |
|
if chunk: |
|
f.write(chunk) |
|
|
|
print(f"Video saved as {filename}") |
|
return filename |
|
else: |
|
print(f"Download failed with status code {response.status_code}") |
|
|
|
except requests.exceptions.RequestException as e: |
|
print(f"An error occurred: {e}") |
|
|
|
|
|
|
|
|
|
def extract_frames(video_path): |
|
video_path = download_video(video_path) |
|
|
|
cap = cv2.VideoCapture(video_path) |
|
if not cap.isOpened(): |
|
|
|
return [None] * 4 |
|
|
|
|
|
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) |
|
if total_frames == 0: |
|
|
|
cap.release() |
|
return [None] * 4 |
|
|
|
|
|
indices = [int(total_frames * i / 4) for i in range(4)] |
|
frames = [] |
|
|
|
|
|
for idx in indices: |
|
cap.set(cv2.CAP_PROP_POS_FRAMES, idx) |
|
ret, frame = cap.read() |
|
if ret: |
|
|
|
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) |
|
frames.append(frame) |
|
else: |
|
|
|
frames.append(None) |
|
|
|
|
|
cap.release() |
|
|
|
|
|
return tuple(frames) |
|
|
|
|
|
iface = gr.Interface( |
|
fn=extract_frames, |
|
inputs=gr.Text(label="Upload a video"), |
|
outputs=[gr.Image(label=f"Frame at {i*25}%") for i in range(4)], |
|
title="Video Frame Extractor", |
|
description="Upload a video to extract four frames from different parts of the video." |
|
) |
|
|
|
|
|
iface.launch() |