jonathanagustin
commited on
Commit
•
1366c9e
1
Parent(s):
8b391b7
Update app.py
Browse files
app.py
CHANGED
@@ -4,12 +4,6 @@ import sys
|
|
4 |
import zipfile
|
5 |
from enum import Enum
|
6 |
from typing import Any, Dict, List, Optional
|
7 |
-
|
8 |
-
os.system("python3 -m pip uninstall -y typing-extensions")
|
9 |
-
os.system("python3 -m pip install -U typing-extensions")
|
10 |
-
os.system(
|
11 |
-
"python3 -m pip install -q --progress-bar off streamlink gradio tiktoken ultralytics pillow innertube opencv-python"
|
12 |
-
)
|
13 |
import cv2
|
14 |
import gradio as gr
|
15 |
import innertube
|
@@ -34,7 +28,6 @@ class SearchFilter(Enum):
|
|
34 |
def __str__(self):
|
35 |
return self.human_readable
|
36 |
|
37 |
-
|
38 |
class SearchService:
|
39 |
@staticmethod
|
40 |
def search(
|
@@ -135,10 +128,9 @@ class YouTubeObjectDetection:
|
|
135 |
)
|
136 |
self.current_page_token = None
|
137 |
self.streams = INITIAL_STREAMS
|
|
|
138 |
# Gradio UI Elements
|
139 |
-
initial_gallery_items = [
|
140 |
-
(stream["thumbnail_url"], stream["title"]) for stream in self.streams
|
141 |
-
]
|
142 |
self.gallery = gr.Gallery(
|
143 |
label="Live YouTube Videos",
|
144 |
value=initial_gallery_items,
|
@@ -151,11 +143,9 @@ class YouTubeObjectDetection:
|
|
151 |
)
|
152 |
self.search_input = gr.Textbox(label="Search Live YouTube Videos")
|
153 |
self.stream_input = gr.Textbox(label="URL of Live YouTube Video")
|
154 |
-
self.
|
155 |
self.search_button = gr.Button("Search", size="lg")
|
156 |
self.submit_button = gr.Button("Detect Objects", variant="primary", size="lg")
|
157 |
-
self.prev_page_button = gr.Button("Previous Page", interactive=False)
|
158 |
-
self.next_page_button = gr.Button("Next Page", interactive=False)
|
159 |
|
160 |
@staticmethod
|
161 |
def download_font(url, save_path):
|
@@ -183,9 +173,7 @@ class YouTubeObjectDetection:
|
|
183 |
if ret:
|
184 |
return cv2.resize(frame, (1920, 1080))
|
185 |
else:
|
186 |
-
logging.warning(
|
187 |
-
"Unable to process the HLS stream with cv2.VideoCapture."
|
188 |
-
)
|
189 |
return None
|
190 |
except Exception as e:
|
191 |
logging.warning(f"An error occurred while capturing the frame: {e}")
|
@@ -220,9 +208,7 @@ class YouTubeObjectDetection:
|
|
220 |
|
221 |
def fetch_live_streams(self, query=""):
|
222 |
streams = []
|
223 |
-
results = SearchService.search(
|
224 |
-
query if query else "world live cams", SearchFilter.LIVE
|
225 |
-
)
|
226 |
for result in results:
|
227 |
if "video_id" in result and "thumbnail_urls" in result:
|
228 |
streams.append(
|
@@ -250,7 +236,7 @@ class YouTubeObjectDetection:
|
|
250 |
with gr.Row():
|
251 |
self.stream_input.render()
|
252 |
self.submit_button.render()
|
253 |
-
self.
|
254 |
with gr.Group():
|
255 |
with gr.Row():
|
256 |
self.search_input.render()
|
@@ -259,7 +245,7 @@ class YouTubeObjectDetection:
|
|
259 |
self.gallery.render()
|
260 |
|
261 |
@self.gallery.select(
|
262 |
-
inputs=None, outputs=[self.
|
263 |
)
|
264 |
def on_gallery_select(evt: gr.SelectData):
|
265 |
selected_index = evt.index
|
@@ -272,9 +258,7 @@ class YouTubeObjectDetection:
|
|
272 |
return frame_output, stream_url
|
273 |
return None, ""
|
274 |
|
275 |
-
@self.search_button.click(
|
276 |
-
inputs=[self.search_input], outputs=[self.gallery]
|
277 |
-
)
|
278 |
def on_search_click(query):
|
279 |
self.streams = self.fetch_live_streams(query)
|
280 |
gallery_items = [
|
@@ -283,9 +267,7 @@ class YouTubeObjectDetection:
|
|
283 |
]
|
284 |
return gallery_items
|
285 |
|
286 |
-
@self.submit_button.click(
|
287 |
-
inputs=[self.stream_input], outputs=[self.output_image]
|
288 |
-
)
|
289 |
def annotate_stream(url):
|
290 |
return self.capture_frame(url)
|
291 |
|
|
|
4 |
import zipfile
|
5 |
from enum import Enum
|
6 |
from typing import Any, Dict, List, Optional
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
import cv2
|
8 |
import gradio as gr
|
9 |
import innertube
|
|
|
28 |
def __str__(self):
|
29 |
return self.human_readable
|
30 |
|
|
|
31 |
class SearchService:
|
32 |
@staticmethod
|
33 |
def search(
|
|
|
128 |
)
|
129 |
self.current_page_token = None
|
130 |
self.streams = INITIAL_STREAMS
|
131 |
+
|
132 |
# Gradio UI Elements
|
133 |
+
initial_gallery_items = [(stream["thumbnail_url"], stream["title"]) for stream in self.streams]
|
|
|
|
|
134 |
self.gallery = gr.Gallery(
|
135 |
label="Live YouTube Videos",
|
136 |
value=initial_gallery_items,
|
|
|
143 |
)
|
144 |
self.search_input = gr.Textbox(label="Search Live YouTube Videos")
|
145 |
self.stream_input = gr.Textbox(label="URL of Live YouTube Video")
|
146 |
+
self.annotated_image = gr.AnnotatedImage(show_label=False)
|
147 |
self.search_button = gr.Button("Search", size="lg")
|
148 |
self.submit_button = gr.Button("Detect Objects", variant="primary", size="lg")
|
|
|
|
|
149 |
|
150 |
@staticmethod
|
151 |
def download_font(url, save_path):
|
|
|
173 |
if ret:
|
174 |
return cv2.resize(frame, (1920, 1080))
|
175 |
else:
|
176 |
+
logging.warning("Unable to process the HLS stream with cv2.VideoCapture.")
|
|
|
|
|
177 |
return None
|
178 |
except Exception as e:
|
179 |
logging.warning(f"An error occurred while capturing the frame: {e}")
|
|
|
208 |
|
209 |
def fetch_live_streams(self, query=""):
|
210 |
streams = []
|
211 |
+
results = SearchService.search(query if query else "world live cams", SearchFilter.LIVE)
|
|
|
|
|
212 |
for result in results:
|
213 |
if "video_id" in result and "thumbnail_urls" in result:
|
214 |
streams.append(
|
|
|
236 |
with gr.Row():
|
237 |
self.stream_input.render()
|
238 |
self.submit_button.render()
|
239 |
+
self.annotated_image.render()
|
240 |
with gr.Group():
|
241 |
with gr.Row():
|
242 |
self.search_input.render()
|
|
|
245 |
self.gallery.render()
|
246 |
|
247 |
@self.gallery.select(
|
248 |
+
inputs=None, outputs=[self.annotated_image, self.stream_input]
|
249 |
)
|
250 |
def on_gallery_select(evt: gr.SelectData):
|
251 |
selected_index = evt.index
|
|
|
258 |
return frame_output, stream_url
|
259 |
return None, ""
|
260 |
|
261 |
+
@self.search_button.click(inputs=[self.search_input], outputs=[self.gallery])
|
|
|
|
|
262 |
def on_search_click(query):
|
263 |
self.streams = self.fetch_live_streams(query)
|
264 |
gallery_items = [
|
|
|
267 |
]
|
268 |
return gallery_items
|
269 |
|
270 |
+
@self.submit_button.click(inputs=[self.stream_input], outputs=[self.annotated_image])
|
|
|
|
|
271 |
def annotate_stream(url):
|
272 |
return self.capture_frame(url)
|
273 |
|