Introduction to Flow APIs
Flow APIs effectively abstract away the underlying pipeline details, allowing developers to focus solely on the goals of their specific tasks in a pythonic style. These high-level APIs emphasize “what to do” rather than “how to do it,” enabling developers to express their intentions in a more intuitive and concise manner. This abstraction simplifies the development process and improves code readability and maintainability.
Besides the explicitly declared arguments, Flow APIs also allow veterans to override standard Deepstream Element properties through kwargs, and the only trick is all the ’-’ in a property name must be replaced with ’_’.
For instance, users can specify the GPU they want to use when they create a flow for capturing:
Within the service maker a wide range of common operations in the multimedia and deep learning fields are supported by Flow.
Capture
The ‘capture’ method appends a capture operation to an empty flow. This operation takes a list of URIs as input, from which multiple streams carrying decoded data will be created within the flow. Alternatively, a SourceManager object can be provided to add/remove sources during runtime.
By default, capture creates video streams. To capture audio streams, use
MediaType.AUDIO:
For files containing both audio and video, request both media types and select the branch needed by the following operation:
Inject
The ‘inject’ method appends an injection operation to an empty flow. The operation takes a list of BufferProvider objects and creates multiple streams thereafter.
In addition to overriding the ‘generate’ method to create buffers, BufferProvider objects must carry the following members to instruct the injection process:
Select
The ‘select’ method derives a new flow containing only a subset of the streams in the current flow. It is most commonly used to pick a single media branch after capturing both audio and video, but it can also filter streams by their originating source element.
Retrieve
The ‘retrieve’ method appends a data retriever to the current flow. The operation takes a instance of BufferRetriever which implements the ‘consume’ method to access buffers. Invocation of the method result in the end of a flow and no more operation can be appended.
Decode
The ‘decode’ method appends a decoding operation to the current flow. The operation adds a decoder to each upstream. It is very useful in the case that the data is injected via buffer providers.
decode can also be used after injecting encoded or raw audio buffers when
the provider describes the audio format with media_type, format,
sample_rate, channels, and layout.
Batch
The ‘batch’ method appends a batching operation to the current flow to combine all the streams to a single batched one. This operation takes standard keyword arguments including’batch_size’, ‘width’, and ‘height’ as parameters. If these parameters are not given, the operation sets the batch size to the number of streams and the width x height to 1920 x 1080 by default.
Audio batching requires nvstreammux2. Set USE_NEW_NVSTREAMMUX=yes before
starting a process that uses batch on audio streams.
Batched Capture
The ‘batch_capture’ method appends a operation to capture to an empty flow and batches the inputs. The operation takes either a list of URIs or a source config file as input and forming a batched stream thereafter.
batch_capture supports one media type for each batched flow. Use
MediaType.AUDIO for audio-only batched capture:
Render
The ‘render’ method appends a renderer to the current flow to display, play,
stream, or discard the media. The operation takes a render mode to decide how to
render the data. DISPLAY is the default for video, DISCARD uses a
fakesink, and STREAM creates an RTSP output where supported. Moreover,
optional named arguments cover all the standard sink control parameters.
Invocation of the method result in the end of a flow and no more operation can
be appended.
For audio playback, provide an audio sink element when using display mode:
Encode
The ‘encode’ method appends an encoder to the current flow to encode media into a file or RTSP stream. The operation takes a destination URI prefixed with ‘file://’ or ‘rtsp://’. If the prefix is missing, ‘file://’ is implied. In the case of RTSP stream, a port number must appear in the URI. Moreover, optional parameters for encoding control are supported too. Audio-only flows support WAV file output. Mixed audio/video flows can be encoded into an MP4 container.
Infer
The ‘infer’ method enables the inference in the current flow. The operation
takes a ‘config’ parameter for the model configuration file. Optional standard
nvinfer parameters can be added to override the values in the configuration
file. For audio streams, infer uses nvinferaudio and supports audio
properties such as audio_framesize, audio_hopsize,
audio_transform, and audio_caps.
Track
The ‘track’ method appends a tracker to the current flow for tracking the detected object. The operation must come after ‘infer’ for detection data. Standard nvtracker parameters must be appropriately set to make the tracker work correctly.
Publish
The ‘publish’ method appends a procedure to the current flow for publishing events to the remote server. The operations takes the following parameters to set up the communication between the pipeline and the remote server.
Invocation of the method result in the end of a flow and no more operation can be appended.
Preprocess
The ‘preprocess’ method appends a preprocess operation to the current flow. The operation takes a ‘config’ parameter for the preprocess configuration file. An optional tensor generator callback can be provided to generate custom tensors alongside the preprocessed images.
Attach
The ‘attach’ method attach a Probe to the current flow. Two parameters required:
Fork
The ‘fork’ method forks the current flow so that more than one flow can be appended.
The output RTSP stream can be received from “rtsp://localhost:8554/ds-test”
Analyze
The ‘analyze’ method appends an analytics operation to the current flow, which outputs frame level and tracked object level information on Region of Interest (RoI) Filtering, Overcrowding Detection, Direction Detection, and Line Crossing. The operation must come after ‘infer’ and ‘track’. The operation takes a ‘config’ parameter for the NvDsAnalytics configuration file.