File size: 2,640 Bytes
e6b775c
 
 
 
 
 
 
 
 
e2aa3d7
e6b775c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45513cc
e6b775c
 
 
 
 
 
4d647f3
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import gradio as gr
import cv2
import requests
import os

def download_video(url):
    # Set up headers to mimic a browser request
    try:
        # Send a GET request to the URL
        response = requests.get(url, stream=True)

        # Check if the request was successful (status code 200)
        if response.status_code == 200:
            # Define the file path for saving the video; filename can be adjusted as needed
            filename = 'video.mp4'
            with open(filename, 'wb') as f:
                # Write the content in chunks to handle large files efficiently
                for chunk in response.iter_content(chunk_size=8192):
                    if chunk:
                        f.write(chunk)
                        # Optional: Add progress indicator or logging here
            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)
    # Open the video file
    cap = cv2.VideoCapture(video_path)
    if not cap.isOpened():
        # If video cannot be opened, return four None values
        return [None] * 4
    
    # Get the total number of frames
    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    if total_frames == 0:
        # If video has no frames, release and return None values
        cap.release()
        return [None] * 4
    
    # Calculate frame indices at 0%, 25%, 50%, and 75%
    indices = [int(total_frames * i / 4) for i in range(4)]
    frames = []
    
    # Extract frames at the calculated indices
    for idx in indices:
        cap.set(cv2.CAP_PROP_POS_FRAMES, idx)
        ret, frame = cap.read()
        if ret:
            # Convert from BGR (OpenCV format) to RGB (Gradio format)
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            frames.append(frame)
        else:
            # If frame extraction fails, append None
            frames.append(None)
    
    # Release the video capture object
    cap.release()
    
    # Return the four frames as a tuple for Gradio outputs
    return tuple(frames)

# Create the Gradio interface
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."
)

# Launch the web app
iface.launch()