utils
RateLimiter
¶
Implements rate upper-bound rate limiting by ensuring estimate_next_tick_delay() to be at min 1 / desired_fps, not letting the client obeying outcomes to exceed assumed rate.
Source code in inference/core/interfaces/camera/utils.py
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
|
VideoSourcesManager
¶
This class should be treated as internal building block of stream multiplexer - not for external use.
Source code in inference/core/interfaces/camera/utils.py
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 |
|
get_video_frames_generator(video, max_fps=None, limiter_strategy=None)
¶
Util function to create a frames generator from VideoSource
with possibility to
limit FPS of consumed frames and dictate what to do if frames are produced to fast.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
video
|
Union[VideoSource, str, int]
|
Either instance of VideoSource or video reference accepted by VideoSource.init(...) |
required |
max_fps
|
Optional[Union[float, int]]
|
value of maximum FPS rate of generated frames - can be used to limit generation frequency |
None
|
limiter_strategy
|
Optional[FPSLimiterStrategy]
|
strategy used to deal with frames decoding exceeding
limit of |
None
|
Returns: generator of VideoFrame
Example
from inference.core.interfaces.camera.utils import get_video_frames_generator
for frame in get_video_frames_generator(
video="./some.mp4",
max_fps=50,
):
pass
Source code in inference/core/interfaces/camera/utils.py
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 |
|
multiplex_videos(videos, max_fps=None, limiter_strategy=None, batch_collection_timeout=None, force_stream_reconnection=True, should_stop=never_stop, on_reconnection_error=log_error)
¶
Function that is supposed to provide a generator over frames from multiple video sources. It is capable to
initialise VideoSource
from references to video files or streams and grab frames from all the sources -
each running individual decoding on separate thread. In each cycle it attempts to grab frames from all sources
(and wait at max batch_collection_timeout
for whole batch to be collected). If frame from specific source
cannot be collected in that time - it is simply not included in returned list. If after batch collection list of
frames is empty - new collection start immediately. Collection does not account for
sources that lost connectivity (example: streams that went offline). If that does not happen and stream has
large latency - without reasonable batch_collection_timeout
it will slow down processing - so please
set it up in PROD solutions. In case of video streams (not video files) - given that
force_stream_reconnection=True
function will attempt to re-connect to disconnected source using background thread,
not impairing batch frames collection and that source is not going to block frames retrieval even if infinite
batch_collection_timeout=None
is set. Similarly, when processing files - video file that is shorter than other
passed into processing will not block the whole flow after End Of Stream (EOS).
All sources must be accessible on start - if that's not the case - logic function raises SourceConnectionError
and closes all video sources it opened on it own. Disconnections at later stages are handled by re-connection
mechanism.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
videos
|
List[Union[VideoSource, str, int]]
|
List with references to video sources. Elements can be
pre-initialised |
required |
max_fps
|
Optional[Union[float, int]]
|
Upper-bound of processing speed - to be used when one wants at max
|
None
|
limiter_strategy
|
Optional[FPSLimiterStrategy]
|
strategy used to deal with frames decoding exceeding
limit of |
None
|
batch_collection_timeout
|
Optional[float]
|
maximum await time to get batch of predictions from all sources.
|
None
|
force_stream_reconnection
|
bool
|
Flag to decide on reconnection to streams (files are never re-connected) |
True
|
should_stop
|
Callable[[], bool]
|
external stop signal that is periodically checked - to denote that video consumption stopped - make the function to return True |
never_stop
|
on_reconnection_error
|
Callable[[Optional[int], SourceConnectionError], None]
|
Function that will be called whenever source cannot re-connect after disconnection. First parameter is source_id, second is connection error instance. |
log_error
|
Returns Generator[List[VideoFrame], None, None]: allowing to iterate through frames from multiple video sources.
Raises:
Type | Description |
---|---|
SourceConnectionError
|
when one or more source is not reachable at start of generation |
Example
from inference.core.interfaces.camera.utils import multiplex_videos
for frames in multiplex_videos(videos=["./some.mp4", "./other.mp4"]):
for frame in frames:
pass # do something with frame
Source code in inference/core/interfaces/camera/utils.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
|