File size: 10,607 Bytes
97ed94f
3834bae
97ed94f
 
 
13d6ce1
97ed94f
 
 
 
8f53b45
97ed94f
13d6ce1
 
8f53b45
 
 
97ed94f
 
 
 
7dbaf21
b864380
8f53b45
 
97ed94f
87a479d
7dbaf21
 
8f53b45
7dbaf21
 
8f53b45
13d6ce1
8f53b45
 
 
 
 
 
 
 
 
 
 
 
 
 
b864380
87a479d
b864380
 
8f53b45
 
644db4b
 
 
 
8f53b45
b864380
644db4b
8f53b45
644db4b
 
 
8f53b45
 
 
 
 
 
 
 
 
 
 
 
 
 
644db4b
 
b864380
 
 
8f53b45
 
 
 
 
 
 
b864380
 
 
97ed94f
3834bae
eb624ad
97ed94f
 
3834bae
97ed94f
 
b864380
13d6ce1
eb624ad
3263472
 
3834bae
 
 
 
 
 
 
 
b864380
3834bae
b864380
 
7dbaf21
b864380
 
87a479d
8f53b45
 
 
97ed94f
b864380
 
5c6a649
8f53b45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87a479d
b864380
7dbaf21
 
8f53b45
b864380
7dbaf21
 
87a479d
b864380
8f53b45
b864380
 
 
8f53b45
 
 
 
 
 
13d6ce1
056a3fd
b864380
 
 
 
97ed94f
8f53b45
 
b864380
8f53b45
 
 
7dbaf21
 
 
 
e42b13d
 
8f53b45
 
b864380
97ed94f
7dbaf21
8f53b45
87a479d
7dbaf21
87a479d
8f53b45
 
 
3834bae
b864380
644db4b
3834bae
 
8f53b45
 
 
8b391b7
8f53b45
 
 
 
 
 
 
 
 
3834bae
8f53b45
 
 
 
 
 
eaaac1a
 
 
3834bae
8f53b45
 
 
 
 
3834bae
8f53b45
 
 
3834bae
8f53b45
 
3834bae
999040d
13d6ce1
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
"""
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.

Main Features:
- Search for live YouTube streams using specific queries.
- Retrieve live stream URLs using the `pytube` library.
- Perform real-time object detection on live streams using the YOLO model.
- Display the live stream and object detection results through a Gradio interface.

Dependencies:
- cv2 (OpenCV): Used for image processing tasks.
- Gradio: Provides the interactive web-based user interface.
- `pytube`: Used for retrieving live stream URLs from YouTube.
- innertube: Used for interacting with YouTube's internal API.
- numpy: Utilized for numerical operations on image data.
- PIL (Pillow): A Python Imaging Library for opening, manipulating, and saving images.
- ultralytics YOLO: The YOLO model implementation for object detection.

Usage:
Run this file to launch the Gradio interface, which allows users to input search queries for YouTube live streams, select a stream, and perform object detection on the selected live stream.
"""

import logging
import sys
from enum import Enum
from typing import Any, Dict, List, Optional, Tuple

import cv2
import gradio as gr
import innertube
import numpy as np
from PIL import Image
from ultralytics import YOLO
from pytube import YouTube

logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)


class SearchFilter(Enum):
    LIVE = ("EgJAAQ%3D%3D", "Live")
    VIDEO = ("EgIQAQ%3D%3D", "Video")

    def __init__(self, code, human_readable):
        self.code = code
        self.human_readable = human_readable

    def __str__(self):
        return self.human_readable


class SearchService:
    @staticmethod
    def search(query: Optional[str], filter: SearchFilter = SearchFilter.VIDEO):
        response = SearchService._search(query, filter)
        results = SearchService.parse(response)
        return results

    @staticmethod
    def parse(data: Dict[str, Any]) -> List[Dict[str, str]]:
        results = []
        try:
            contents = data["contents"]["twoColumnSearchResultsRenderer"]["primaryContents"]["sectionListRenderer"]["contents"]
            for content in contents:
                items = content.get("itemSectionRenderer", {}).get("contents", [])
                for item in items:
                    if "videoRenderer" in item:
                        renderer = item["videoRenderer"]
                        video_id = renderer.get("videoId", "")
                        thumbnails = renderer.get("thumbnail", {}).get("thumbnails", [])
                        thumbnail_url = thumbnails[-1]["url"] if thumbnails else ""
                        title_runs = renderer.get("title", {}).get("runs", [])
                        title = "".join(run.get("text", "") for run in title_runs)
                        results.append(
                            {
                                "video_id": video_id,
                                "thumbnail_url": thumbnail_url,
                                "title": title,
                            }
                        )
        except Exception as e:
            logging.error(f"Error parsing search results: {e}")
        return results

    @staticmethod
    def _search(query: Optional[str] = None, filter: SearchFilter = SearchFilter.VIDEO) -> Dict[str, Any]:
        client = innertube.InnerTube(client_name="WEB", client_version="2.20230920.00.00")
        response = client.search(query=query, params=filter.code if filter else None)
        return response

    @staticmethod
    def get_youtube_url(video_id: str) -> str:
        return f"https://www.youtube.com/watch?v={video_id}"

    @staticmethod
    def get_stream(youtube_url: str) -> Optional[str]:
        """Retrieves the live stream URL for a given YouTube video URL using pytube.
    
        :param youtube_url: The URL of the YouTube video.
        :type youtube_url: str
        :return: The HLS manifest URL if available, otherwise None.
        :rtype: Optional[str]
        """
        try:
            yt = YouTube(youtube_url)
            _ = yt.streams
            is_live = yt.player_response.get('videoDetails', {}).get('isLiveContent', False)
            if is_live:
                streaming_data = yt.player_response.get('streamingData', {})
                hls_manifest_url = streaming_data.get('hlsManifestUrl')
                if hls_manifest_url:
                    logging.debug(f"Found HLS manifest URL for live stream: {hls_manifest_url}")
                    return hls_manifest_url
                else:
                    logging.warning(f"HLS manifest URL not found for live stream: {youtube_url}")
                    return None
            else:
                logging.warning(f"Video is not a live stream: {youtube_url}")
                return None
        except Exception as e:
            logging.warning(f"An error occurred while getting stream: {e}")
            return None


INITIAL_STREAMS = SearchService.search("world live cams", SearchFilter.LIVE)


class LiveYouTubeObjectDetector:
    def __init__(self):
        logging.getLogger().setLevel(logging.DEBUG)
        self.model = YOLO("yolo11n.pt")
        self.streams = INITIAL_STREAMS

        # Gradio UI
        initial_gallery_items = [(stream["thumbnail_url"], stream["title"]) for stream in self.streams]
        self.gallery = gr.Gallery(label="Live YouTube Videos",
                                  value=initial_gallery_items,
                                  show_label=True,
                                  columns=[4],
                                  rows=[5],
                                  object_fit="contain",
                                  height="auto",
                                  allow_preview=False)
        self.search_input = gr.Textbox(label="Search Live YouTube Videos")
        self.stream_input = gr.Textbox(label="URL of Live YouTube Video")
        self.annotated_image = gr.AnnotatedImage(show_label=False)
        self.search_button = gr.Button("Search", size="lg")
        self.submit_button = gr.Button("Detect Objects", variant="primary", size="lg")
        self.page_title = gr.HTML("<center><h1><b>Object Detection in Live YouTube Streams</b></h1></center>")

    def detect_objects(self, url: str) -> Tuple[Image.Image, List[Tuple[Tuple[int, int, int, int], str]]]:
        stream_url = SearchService.get_stream(url)
        if not stream_url:
            logging.error(f"Unable to find a stream for: {url}")
            return self.create_black_image()
        frame = self.get_frame(stream_url)
        if frame is None:
            logging.error(f"Unable to capture frame for: {url}")
            return self.create_black_image()
        return self.annotate(frame)

    def get_frame(self, stream_url: str) -> Optional[np.ndarray]:
        if not stream_url:
            return None
        try:
            cap = cv2.VideoCapture(stream_url)
            ret, frame = cap.read()
            cap.release()
            if ret and frame is not None:
                return cv2.resize(frame, (1920, 1080))
            else:
                logging.warning("Unable to process the live stream with cv2.VideoCapture.")
                return None
        except Exception as e:
            logging.warning(f"An error occurred while capturing the frame: {e}")
            return None

    def annotate(self, frame: np.ndarray) -> Tuple[Image.Image, List[Tuple[Tuple[int, int, int, int], str]]]:
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        predictions = self.model.predict(frame_rgb)
        annotations = []
        result = predictions[0]

        boxes = result.boxes
        for box in boxes:
            x1, y1, x2, y2 = box.xyxy[0].tolist()
            class_id = int(box.cls[0])
            class_name = self.model.names[class_id]
            bbox_coords = (int(x1), int(y1), int(x2), int(y2))
            annotations.append((bbox_coords, class_name))

        return Image.fromarray(frame_rgb), annotations

    @staticmethod
    def create_black_image() -> Tuple[Image.Image, List]:
        black_image = np.zeros((1080, 1920, 3), dtype=np.uint8)
        pil_black_image = Image.fromarray(black_image)
        return pil_black_image, []

    @staticmethod
    def get_live_streams(query=""):
        return SearchService.search(query if query else "world live cams", SearchFilter.LIVE)

    def render(self):
        with gr.Blocks(title="Object Detection in Live YouTube Streams",
                       css="footer {visibility: hidden}",
                       analytics_enabled=False) as app:
            self.page_title.render()
            with gr.Column():
                with gr.Group():
                    with gr.Row():
                        self.stream_input.render()
                        self.submit_button.render()
                self.annotated_image.render()
            with gr.Group():
                with gr.Row():
                    self.search_input.render()
                    self.search_button.render()
            with gr.Row():
                self.gallery.render()

            @self.gallery.select(inputs=None, outputs=[self.annotated_image, self.stream_input], scroll_to_output=True)
            def detect_objects_from_gallery_item(evt: gr.SelectData):
                if evt.index is not None and evt.index < len(self.streams):
                    selected_stream = self.streams[evt.index]
                    stream_url = SearchService.get_youtube_url(selected_stream["video_id"])
                    self.stream_input.value = stream_url
                    annotated_image_result = self.detect_objects(stream_url)
                    return annotated_image_result, stream_url
                return self.create_black_image(), ""

            @self.search_button.click(inputs=[self.search_input], outputs=[self.gallery])
            def search_live_streams(query):
                self.streams = self.get_live_streams(query)
                gallery_items = [(stream["thumbnail_url"], stream["title"]) for stream in self.streams]
                return gallery_items

            @self.submit_button.click(inputs=[self.stream_input], outputs=[self.annotated_image])
            def detect_objects_from_url(url):
                return self.detect_objects(url)

        app.queue().launch(show_api=False, debug=True)


if __name__ == "__main__":
    LiveYouTubeObjectDetector().render()