sinks
UDPSink
¶
Source code in inference/core/interfaces/stream/sinks.py
228 229 230 231 232 233 234 235 236 237 238 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 |
|
init(ip_address, port)
classmethod
¶
Creates InferencePipeline
predictions sink capable of sending model predictions over network
using UDP socket.
As an inference
user, please use .init() method instead of constructor to instantiate objects.
Args:
ip_address (str): IP address to send predictions
port (int): Port to send predictions
Returns: Initialised object of UDPSink
class.
Source code in inference/core/interfaces/stream/sinks.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
|
send_predictions(predictions, video_frame)
¶
Method to send predictions via UDP socket. Useful in combination with InferencePipeline
as
a sink for predictions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions |
Union[dict, List[Optional[dict]]]
|
Roboflow predictions, the function support single prediction
processing and batch processing since version |
required |
video_frame |
Union[VideoFrame, List[Optional[VideoFrame]]]
|
frame of video with its basic metadata emitted
by |
required |
Side effects: Sends serialised predictions
and video_frame
metadata via the UDP socket as
JSON string. It adds key named "inference_metadata" into predictions
dict (mutating its
state). "inference_metadata" contain id of the frame, frame grabbing timestamp and message
emission time in datetime iso format.
Example
import cv2
from inference.core.interfaces.stream.inference_pipeline import InferencePipeline
from inference.core.interfaces.stream.sinks import UDPSink
udp_sink = UDPSink.init(ip_address="127.0.0.1", port=9090)
pipeline = InferencePipeline.init(
model_id="your-model/3",
video_reference="./some_file.mp4",
on_prediction=udp_sink.send_predictions,
)
pipeline.start()
pipeline.join()
UDPSink
used in this way will emit predictions to receiver automatically.
Source code in inference/core/interfaces/stream/sinks.py
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 |
|
VideoFileSink
¶
Source code in inference/core/interfaces/stream/sinks.py
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 |
|
init(video_file_name, annotator=None, display_size=(1280, 720), fps_monitor=DEFAULT_FPS_MONITOR, display_statistics=False, output_fps=25, quiet=False, video_frame_size=(1280, 720))
classmethod
¶
Creates InferencePipeline
predictions sink capable of saving model predictions into video file.
It works both for pipelines with single input video and multiple ones.
As an inference
user, please use .init() method instead of constructor to instantiate objects.
Args:
video_file_name (str): name of the video file to save predictions
annotator (Union[BaseAnnotator, List[BaseAnnotator]]): instance of class inheriting from supervision BaseAnnotator
or list of such instances. If nothing is passed chain of sv.BoundingBoxAnnotator()
and sv.LabelAnnotator()
is used.
display_size (Tuple[int, int]): tuple in format (width, height) to resize visualisation output. Should
be set to the same value as display_size
for InferencePipeline with single video source, otherwise
it represents the size of single visualisation tile (whole tiles mosaic will be scaled to
video_frame_size
)
fps_monitor (Optional[sv.FPSMonitor]): FPS monitor used to monitor throughput
display_statistics (bool): Flag to decide if throughput and latency can be displayed in the result image,
if enabled, throughput will only be presented if fps_monitor
is not None
output_fps (int): desired FPS of output file
quiet (bool): Flag to decide whether to log progress
video_frame_size (Tuple[int, int]): The size of frame in target video file.
Attributes:
Name | Type | Description |
---|---|---|
on_prediction |
Callable[[dict, VideoFrame], None]
|
callable to be used as a sink for predictions |
Returns: Initialized object of VideoFileSink
class.
Example
import cv2
from inference import InferencePipeline
from inference.core.interfaces.stream.sinks import VideoFileSink
video_sink = VideoFileSink.init(video_file_name="output.avi")
pipeline = InferencePipeline.init(
model_id="your-model/3",
video_reference="./some_file.mp4",
on_prediction=video_sink.on_prediction,
)
pipeline.start()
pipeline.join()
video_sink.release()
VideoFileSink
used in this way will save predictions to video file automatically.
Source code in inference/core/interfaces/stream/sinks.py
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
|
release()
¶
Releases VideoWriter object.
Source code in inference/core/interfaces/stream/sinks.py
505 506 507 508 509 510 |
|
active_learning_sink(predictions, video_frame, active_learning_middleware, model_type, disable_preproc_auto_orient=False)
¶
Function to serve as Active Learning sink for InferencePipeline.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions |
Union[dict, List[Optional[dict]]]
|
Roboflow predictions, the function support single prediction
processing and batch processing since version |
required |
video_frame |
Union[VideoFrame, List[Optional[VideoFrame]]]
|
frame of video with its basic metadata emitted
by |
required |
active_learning_middleware |
ActiveLearningMiddleware
|
instance of middleware to register data. |
required |
model_type |
str
|
Type of Roboflow model in use |
required |
disable_preproc_auto_orient |
bool
|
Flag to denote how image is preprocessed which is important in Active Learning. |
False
|
Returns: None Side effects: Can register data and predictions in Roboflow backend if that's the evaluation of sampling engine.
Source code in inference/core/interfaces/stream/sinks.py
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
|
multi_sink(predictions, video_frame, sinks)
¶
Helper util useful to combine multiple sinks together, while using InferencePipeline
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
video_frame |
VideoFrame
|
frame of video with its basic metadata emitted by |
required |
predictions |
dict
|
Roboflow object detection predictions with Bounding Boxes |
required |
sinks |
List[Callable[[VideoFrame, dict], None]]
|
list of sinks to be used. Each will be executed one-by-one in the order pointed in input list, all errors will be caught and reported via logger, without re-raising. |
required |
Returns: None Side effects: Uses all sinks in context if (video_frame, predictions) input.
Example
from functools import partial
import cv2
from inference import InferencePipeline
from inference.core.interfaces.stream.sinks import UDPSink, render_boxes
udp_sink = UDPSink(ip_address="127.0.0.1", port=9090)
on_prediction = partial(multi_sink, sinks=[udp_sink.send_predictions, render_boxes])
pipeline = InferencePipeline.init(
model_id="your-model/3",
video_reference="./some_file.mp4",
on_prediction=on_prediction,
)
pipeline.start()
pipeline.join()
As a result, predictions will both be sent via UDP socket and displayed in the screen.
Source code in inference/core/interfaces/stream/sinks.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
|
render_boxes(predictions, video_frame, annotator=None, display_size=(1280, 720), fps_monitor=DEFAULT_FPS_MONITOR, display_statistics=False, on_frame_rendered=display_image)
¶
Helper tool to render object detection predictions on top of video frame. It is designed
to be used with InferencePipeline
, as sink for predictions. By default it uses
standard sv.BoundingBoxAnnotator()
chained with sv.LabelAnnotator()
to draw bounding boxes and resizes prediction to 1280x720 (keeping aspect ratio and adding black padding).
One may configure default behaviour, for instance to display latency and throughput statistics.
In batch mode it will display tiles of frames and overlay predictions.
This sink is only partially compatible with stubs and classification models (it will not fail, although predictions will not be displayed).
Since version 0.9.18
, when multi-source InferencePipeline was introduced - it support batch input, without
changes to old functionality when single (predictions, video_frame) is used.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions |
Union[dict, List[Optional[dict]]]
|
Roboflow predictions, the function support single prediction
processing and batch processing since version |
required |
video_frame |
Union[VideoFrame, List[Optional[VideoFrame]]]
|
frame of video with its basic metadata emitted
by |
required |
annotator |
Union[BaseAnnotator, List[BaseAnnotator]]
|
instance of class inheriting from supervision BaseAnnotator
or list of such instances. If nothing is passed chain of |
None
|
display_size |
Tuple[int, int]
|
tuple in format (width, height) to resize visualisation output |
(1280, 720)
|
fps_monitor |
Optional[FPSMonitor]
|
FPS monitor used to monitor throughput |
DEFAULT_FPS_MONITOR
|
display_statistics |
bool
|
Flag to decide if throughput and latency can be displayed in the result image,
if enabled, throughput will only be presented if |
False
|
on_frame_rendered |
Callable[[Union[ImageWithSourceID, List[ImageWithSourceID]]], None]
|
callback to be called once frame is rendered - by default, function will display OpenCV window. It expects optional integer identifier with np.ndarray or list of those elements. Identifier is supposed to refer to either source_id (for sequential input) or position in the batch (from 0 to batch_size-1). |
display_image
|
Side effects: on_frame_rendered() is called against the tuple (stream_id, np.ndarray) produced from video frame and predictions.
Example
from functools import partial
import cv2
from inference import InferencePipeline
from inference.core.interfaces.stream.sinks import render_boxes
output_size = (640, 480)
video_sink = cv2.VideoWriter("output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 25.0, output_size)
on_prediction = partial(
render_boxes,
display_size=output_size,
on_frame_rendered=lambda frame_data: video_sink.write(frame_data[1])
)
pipeline = InferencePipeline.init(
model_id="your-model/3",
video_reference="./some_file.mp4",
on_prediction=on_prediction,
)
pipeline.start()
pipeline.join()
video_sink.release()
In this example, render_boxes()
is used as a sink for InferencePipeline
predictions - making frames with
predictions displayed to be saved into video file. Please note that this is oversimplified example of usage
which will not be robust against multiple streams - better implementation available in VideoFileSink
class.
Source code in inference/core/interfaces/stream/sinks.py
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 |
|