Spaces:
Runtime error
Runtime error
Ayushnangia
commited on
Commit
·
af5763a
1
Parent(s):
7740995
app and requriements
Browse files- app.py +107 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import cv2
|
3 |
+
import gradio as gr
|
4 |
+
from raft import Raft
|
5 |
+
import yt_dlp
|
6 |
+
|
7 |
+
def download_youtube_video(youtube_url, output_filename):
|
8 |
+
ydl_opts = {
|
9 |
+
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
10 |
+
'outtmpl': output_filename,
|
11 |
+
}
|
12 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
13 |
+
ydl.download([youtube_url])
|
14 |
+
|
15 |
+
def process_video(youtube_url, start_time, flow_frame_offset):
|
16 |
+
model_path = 'raft_small_iter10_240x320.onnx'
|
17 |
+
flow_estimator = Raft(model_path)
|
18 |
+
|
19 |
+
output_filename = 'downloaded_video.mp4'
|
20 |
+
processed_output = 'processed_video.mp4'
|
21 |
+
|
22 |
+
# Download video
|
23 |
+
if os.path.exists(output_filename):
|
24 |
+
os.remove(output_filename)
|
25 |
+
download_youtube_video(youtube_url, output_filename)
|
26 |
+
|
27 |
+
cap = cv2.VideoCapture(output_filename)
|
28 |
+
if not cap.isOpened():
|
29 |
+
return "Error: Could not open video."
|
30 |
+
|
31 |
+
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
32 |
+
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
33 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
34 |
+
|
35 |
+
fourcc = cv2.VideoWriter_fourcc(*'XVID')
|
36 |
+
out = cv2.VideoWriter(processed_output, fourcc, fps, (frame_width, frame_height))
|
37 |
+
|
38 |
+
cap.set(cv2.CAP_PROP_POS_FRAMES, start_time * fps)
|
39 |
+
|
40 |
+
frame_list = []
|
41 |
+
frame_num = 0
|
42 |
+
|
43 |
+
while cap.isOpened():
|
44 |
+
ret, prev_frame = cap.read()
|
45 |
+
if not ret:
|
46 |
+
break
|
47 |
+
|
48 |
+
frame_list.append(prev_frame)
|
49 |
+
frame_num += 1
|
50 |
+
|
51 |
+
if frame_num <= flow_frame_offset:
|
52 |
+
continue
|
53 |
+
|
54 |
+
flow_map = flow_estimator(frame_list[0], frame_list[-1])
|
55 |
+
flow_img = flow_estimator.draw_flow()
|
56 |
+
|
57 |
+
alpha = 0.5
|
58 |
+
combined_img = cv2.addWeighted(frame_list[0], alpha, flow_img, (1 - alpha), 0)
|
59 |
+
|
60 |
+
if combined_img is None:
|
61 |
+
break
|
62 |
+
|
63 |
+
out.write(combined_img)
|
64 |
+
frame_list.pop(0)
|
65 |
+
|
66 |
+
cap.release()
|
67 |
+
out.release()
|
68 |
+
|
69 |
+
return processed_output
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
|
74 |
+
examples = [
|
75 |
+
["https://www.youtube.com/watch?v=is38pqgbj6A", 5, 50, "output_1.mp4"],
|
76 |
+
["https://www.youtube.com/watch?v=AdbrfoxiAtk", 0, 60, "output_2.mp4"],
|
77 |
+
["https://www.youtube.com/watch?v=vWGg0iPmI8k", 13, 70, "output_3.mp4"],
|
78 |
+
]
|
79 |
+
|
80 |
+
with gr.Blocks() as app:
|
81 |
+
gr.Markdown("### Optical Flow Video Processing\n"
|
82 |
+
"Enter a YouTube URL, set the start time and flow frame offset, "
|
83 |
+
"then click 'Process Video' to see the optical flow processing.")
|
84 |
+
|
85 |
+
with gr.Row():
|
86 |
+
with gr.Column():
|
87 |
+
youtube_url = gr.Textbox(label="YouTube URL", placeholder="Enter YouTube Video URL Here")
|
88 |
+
start_time = gr.Slider(minimum=0, maximum=60, label="Start Time (seconds)", step=1)
|
89 |
+
flow_frame_offset = gr.Slider(minimum=1, maximum=100, label="Flow Frame Offset", step=1)
|
90 |
+
submit_button = gr.Button("Process Video")
|
91 |
+
|
92 |
+
with gr.Column():
|
93 |
+
output_video = gr.Video(label="Processed Video")
|
94 |
+
|
95 |
+
submit_button.click(
|
96 |
+
fn=process_video,
|
97 |
+
inputs=[youtube_url, start_time, flow_frame_offset],
|
98 |
+
outputs=output_video
|
99 |
+
)
|
100 |
+
|
101 |
+
gr.Examples(examples=examples,
|
102 |
+
inputs=[youtube_url, start_time, flow_frame_offset],
|
103 |
+
fn=process_video,
|
104 |
+
outputs=output_video,
|
105 |
+
cache_examples=False)
|
106 |
+
|
107 |
+
app.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
opencv-python
|
2 |
+
onnx
|
3 |
+
onnxruntime
|
4 |
+
onnxruntime-gpu
|
5 |
+
imread-from-url
|