SPACERUNNER99 commited on
Commit
e6b775c
·
verified ·
1 Parent(s): b5173c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -49
app.py CHANGED
@@ -1,50 +1,80 @@
1
- import gradio as gr
2
- import cv2
3
-
4
- def extract_frames(video_path):
5
- # Open the video file
6
- cap = cv2.VideoCapture(video_path)
7
- if not cap.isOpened():
8
- # If video cannot be opened, return four None values
9
- return [None] * 4
10
-
11
- # Get the total number of frames
12
- total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
13
- if total_frames == 0:
14
- # If video has no frames, release and return None values
15
- cap.release()
16
- return [None] * 4
17
-
18
- # Calculate frame indices at 0%, 25%, 50%, and 75%
19
- indices = [int(total_frames * i / 4) for i in range(4)]
20
- frames = []
21
-
22
- # Extract frames at the calculated indices
23
- for idx in indices:
24
- cap.set(cv2.CAP_PROP_POS_FRAMES, idx)
25
- ret, frame = cap.read()
26
- if ret:
27
- # Convert from BGR (OpenCV format) to RGB (Gradio format)
28
- frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
29
- frames.append(frame)
30
- else:
31
- # If frame extraction fails, append None
32
- frames.append(None)
33
-
34
- # Release the video capture object
35
- cap.release()
36
-
37
- # Return the four frames as a tuple for Gradio outputs
38
- return tuple(frames)
39
-
40
- # Create the Gradio interface
41
- iface = gr.Interface(
42
- fn=extract_frames,
43
- inputs=gr.Video(label="Upload a video"),
44
- outputs=[gr.Image(label=f"Frame at {i*25}%") for i in range(4)],
45
- title="Video Frame Extractor",
46
- description="Upload a video to extract four frames from different parts of the video."
47
- )
48
-
49
- # Launch the web app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()