-
-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathrandom_number_generator.py
More file actions
221 lines (153 loc) · 5.12 KB
/
Copy pathrandom_number_generator.py
File metadata and controls
221 lines (153 loc) · 5.12 KB
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
import yt_dlp
import cv2
import numpy as np
# -----------------YouTube livestream URLs------------------ #
URLs = [
# Jellyfish Tank
"https://www.youtube.com/watch?v=1rvCfsW_qTA",
# Tropical Reef Aquarium
"https://www.youtube.com/watch?v=DHUnz4dyb54",
# Polar Bear Tundra
"https://www.youtube.com/watch?v=kvJnsuyE0cs",
# Great Green Macaw Nest
"https://www.youtube.com/watch?v=OUc-R6Mtg0E",
# Bison Grasslands National Park
"https://www.youtube.com/watch?v=g6wxYiESkE4",
# Puffin Skomer Island
"https://www.youtube.com/watch?v=1pFTuCQGVZ0",
# Panda Chengdu Base
"https://www.youtube.com/watch?v=SUXPnIEpbn4",
# Grace the Gorilla
"https://www.youtube.com/watch?v=672cUSe69J0",
# Bald Eagle Big Bear
"https://www.youtube.com/watch?v=B4-L2nfGcuE"
]
# yt-dlp set options
options = {
"quiet": True,
"no_warnings": True,
"logger": None
}
def get_single_frame():
"""
Capture a single frame from a random available YouTube livestream.
The function randomly iterates through a predefined list of livestream URLs,
attempts to extract the direct stream URL using yt-dlp, and captures
one video frame using OpenCV.
Returns:
numpy.ndarray:
A single video frame if capture succeeds.
None:
Returned if all livestreams are offline or frame capture fails.
"""
random_urls = np.random.permutation(URLs)
for url in random_urls:
try:
# Extracts livestream URL and metadata without downloading
with yt_dlp.YoutubeDL(options) as ydl:
info = ydl.extract_info(url, download=False)
stream_url = info["url"]
capture_video = cv2.VideoCapture(stream_url)
ret, frame = capture_video.read()
capture_video.release()
if ret:
print(f"Frame captured Successfully: {url}")
return frame
print(f"Frame capture failed: {url}")
except Exception as e:
print(f"{url} livestream currently offline")
print("All livestreams are offline")
return None
def get_rand_px():
"""
Extract a random BGR channel value from a random pixel
within a captured livestream frame.
A single frame is captured from the first available livestream.
The function randomly selects:
- a row position
- a column position
- a color channel (B, G, or R)
Returns:
int:
Random pixel channel value between 0 and 255.
None:
Returned if frame capture fails.
"""
frame = get_single_frame()
if frame is None:
return None
# Get a random pixel's BGR value
h, w, px = frame.shape
pixel_value = int(frame[np.random.randint(0, h), np.random.randint(0, w),
np.random.randint(0, px)])
print(pixel_value)
return pixel_value
def sum_all(nums):
"""
Calculate the sum of all generated random numbers.
Returns:
int:
Sum of all values stored in random_numbers or
the single stored value if only one number exists.
"""
return sum(nums)
def avg(nums):
"""
Calculate the average of all generated random numbers.
Returns:
int:
Rounded average of all values stored in random_numbers or
the single stored value if only one number exists.
"""
return round(sum(nums)/len(nums))
def concat(nums):
"""
Concatenate all generated random numbers into a single integer.
Example:
[12, 45, 78] -> 124578
Returns:
int:
Concatenated integer of all random numbers or
the single stored value if only one number exists.
"""
return int("".join(map(str, nums)))
# -------------------------PROGRAM---------------------------- #
def main():
random_numbers = []
size = input("How many random numbers?\n> ")
while not size.isdigit() or not (1 <= int(size) <= 10):
print("Error: Please enter an integer between 1 and 10.")
size = input("How many random numbers? ")
size = int(size)
for i in range(size):
random_numbers.append(get_rand_px())
while True:
print("\n>>> COMMANDS <<<")
print("\n1 : Sum")
print("2 : Average")
print("3 : Concatenate")
print("4 : Print")
print("5 : Exit")
COM = input("> ")
while not COM.isdigit() or not (1 <= int(COM) <= 5):
print("Error: Please select a number between 1 and 5.")
print("\n1 : Sum")
print("2 : Average")
print("3 : Concatenate")
print("4 : Print all")
print("5 : Exit")
COM = input("> ")
COM = int(COM)
if COM == 1:
print(sum_all(random_numbers))
elif COM == 2:
print(avg(random_numbers))
elif COM == 3:
print(concat(random_numbers))
elif COM == 4:
print(*random_numbers)
elif COM == 5:
print("Good Bye")
break
if __name__ == "__main__":
main()