jonathanagustin commited on
Commit
3834bae
1 Parent(s): eaaac1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -16
app.py CHANGED
@@ -1,5 +1,5 @@
1
  """
2
- This module integrates real-time object detection into live YouTube streams using the YOLO model, and provides an interactive user interface through Gradio. It allows users to search for live YouTube streams and apply object detection to these streams in real time.
3
 
4
  Main Features:
5
  - Search for live YouTube streams using specific queries.
@@ -31,8 +31,11 @@ import innertube
31
  import numpy as np
32
  from PIL import Image
33
  from ultralytics import YOLO
 
 
34
  from pytube import YouTube
35
 
 
36
  logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
37
 
38
 
@@ -93,22 +96,26 @@ class SearchService:
93
 
94
  @staticmethod
95
  def get_stream(youtube_url: str) -> Optional[str]:
96
- """Retrieves the stream URL for a given YouTube video URL using pytube.
97
 
98
  :param youtube_url: The URL of the YouTube video.
99
  :type youtube_url: str
100
- :return: The stream URL if available, otherwise None.
101
  :rtype: Optional[str]
102
  """
103
  try:
104
  yt = YouTube(youtube_url)
105
- # Live streams have a 'LIVE' attribute in the 'streaming_data' key
106
- if 'hlsManifestUrl' in yt.watch_html:
107
- # Extract the HLS manifest URL
108
- hls_manifest_url = yt.vid_info['streamingData']['hlsManifestUrl']
109
- return hls_manifest_url
 
 
 
 
110
  else:
111
- logging.warning(f"No live stream found for: {youtube_url}")
112
  return None
113
  except Exception as e:
114
  logging.warning(f"An error occurred while getting stream: {e}")
@@ -156,7 +163,6 @@ class LiveYouTubeObjectDetector:
156
  if not stream_url:
157
  return None
158
  try:
159
- # Use cv2 VideoCapture with the HLS manifest URL
160
  cap = cv2.VideoCapture(stream_url)
161
  ret, frame = cap.read()
162
  cap.release()
@@ -194,10 +200,11 @@ class LiveYouTubeObjectDetector:
194
  @staticmethod
195
  def get_live_streams(query=""):
196
  return SearchService.search(query if query else "world live cams", SearchFilter.LIVE)
197
-
198
  def render(self):
199
  with gr.Blocks(title="Object Detection in Live YouTube Streams",
200
- css="footer {visibility: hidden}", analytics_enabled=False) as app:
 
201
  self.page_title.render()
202
  with gr.Column():
203
  with gr.Group():
@@ -211,7 +218,7 @@ class LiveYouTubeObjectDetector:
211
  self.search_button.render()
212
  with gr.Row():
213
  self.gallery.render()
214
-
215
  @self.gallery.select(inputs=None, outputs=[self.annotated_image, self.stream_input], scroll_to_output=True)
216
  def detect_objects_from_gallery_item(evt: gr.SelectData):
217
  if evt.index is not None and evt.index < len(self.streams):
@@ -221,18 +228,19 @@ class LiveYouTubeObjectDetector:
221
  annotated_image_result = self.detect_objects(stream_url)
222
  return annotated_image_result, stream_url
223
  return self.create_black_image(), ""
224
-
225
  @self.search_button.click(inputs=[self.search_input], outputs=[self.gallery])
226
  def search_live_streams(query):
227
  self.streams = self.get_live_streams(query)
228
  gallery_items = [(stream["thumbnail_url"], stream["title"]) for stream in self.streams]
229
  return gallery_items
230
-
231
  @self.submit_button.click(inputs=[self.stream_input], outputs=[self.annotated_image])
232
  def detect_objects_from_url(url):
233
  return self.detect_objects(url)
234
-
235
  app.queue().launch(show_api=False, debug=True)
236
 
 
237
  if __name__ == "__main__":
238
  LiveYouTubeObjectDetector().render()
 
1
  """
2
+ This module integrates real-time object detection into live YouTube streams using the YOLO model and provides an interactive user interface through Gradio. It allows users to search for live YouTube streams and apply object detection to these streams in real time.
3
 
4
  Main Features:
5
  - Search for live YouTube streams using specific queries.
 
31
  import numpy as np
32
  from PIL import Image
33
  from ultralytics import YOLO
34
+
35
+ # Import pytube
36
  from pytube import YouTube
37
 
38
+ # Set up logging
39
  logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
40
 
41
 
 
96
 
97
  @staticmethod
98
  def get_stream(youtube_url: str) -> Optional[str]:
99
+ """Retrieves the live stream URL for a given YouTube video URL using pytube.
100
 
101
  :param youtube_url: The URL of the YouTube video.
102
  :type youtube_url: str
103
+ :return: The HLS manifest URL if available, otherwise None.
104
  :rtype: Optional[str]
105
  """
106
  try:
107
  yt = YouTube(youtube_url)
108
+ if yt.is_live:
109
+ streaming_data = yt.player_response.get('streamingData', {})
110
+ hls_manifest_url = streaming_data.get('hlsManifestUrl')
111
+ if hls_manifest_url:
112
+ logging.debug(f"Found HLS manifest URL for live stream: {hls_manifest_url}")
113
+ return hls_manifest_url
114
+ else:
115
+ logging.warning(f"HLS manifest URL not found for live stream: {youtube_url}")
116
+ return None
117
  else:
118
+ logging.warning(f"Video is not a live stream: {youtube_url}")
119
  return None
120
  except Exception as e:
121
  logging.warning(f"An error occurred while getting stream: {e}")
 
163
  if not stream_url:
164
  return None
165
  try:
 
166
  cap = cv2.VideoCapture(stream_url)
167
  ret, frame = cap.read()
168
  cap.release()
 
200
  @staticmethod
201
  def get_live_streams(query=""):
202
  return SearchService.search(query if query else "world live cams", SearchFilter.LIVE)
203
+
204
  def render(self):
205
  with gr.Blocks(title="Object Detection in Live YouTube Streams",
206
+ css="footer {visibility: hidden}",
207
+ analytics_enabled=False) as app:
208
  self.page_title.render()
209
  with gr.Column():
210
  with gr.Group():
 
218
  self.search_button.render()
219
  with gr.Row():
220
  self.gallery.render()
221
+
222
  @self.gallery.select(inputs=None, outputs=[self.annotated_image, self.stream_input], scroll_to_output=True)
223
  def detect_objects_from_gallery_item(evt: gr.SelectData):
224
  if evt.index is not None and evt.index < len(self.streams):
 
228
  annotated_image_result = self.detect_objects(stream_url)
229
  return annotated_image_result, stream_url
230
  return self.create_black_image(), ""
231
+
232
  @self.search_button.click(inputs=[self.search_input], outputs=[self.gallery])
233
  def search_live_streams(query):
234
  self.streams = self.get_live_streams(query)
235
  gallery_items = [(stream["thumbnail_url"], stream["title"]) for stream in self.streams]
236
  return gallery_items
237
+
238
  @self.submit_button.click(inputs=[self.stream_input], outputs=[self.annotated_image])
239
  def detect_objects_from_url(url):
240
  return self.detect_objects(url)
241
+
242
  app.queue().launch(show_api=False, debug=True)
243
 
244
+
245
  if __name__ == "__main__":
246
  LiveYouTubeObjectDetector().render()