### Install Sphinx and Extensions Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/docs/README.md Installs Sphinx and required extensions for documentation generation using pip. ```bash pip3 install sphinx pip3 install breathe pip3 install recommonmark pip3 install sphinx_rtd_theme ``` -------------------------------- ### Build and Install Gst-python using Meson and Ninja Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/bindings/README.md Builds and installs the gst-python library using Meson and Ninja. This process involves navigating to the gst-python directory, setting up the build, and then installing it. ```bash cd bindings/3rdparty/gstreamer/subprojects/gst-python/ meson setup build cd build ninja ninja install ``` -------------------------------- ### Install QEMU and Register for Cross-Compilation Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/bindings/README.md Installs necessary QEMU packages and registers the emulator for cross-architecture execution within Docker. This is a prerequisite for emulating the aarch64 environment on an x86 host. ```bash # Install qemu packages sudo apt-get install qemu-system qemu-utils # Execute the registering scripts docker run --rm --privileged dockerhub.nvidia.com/multiarch/qemu-user-static --reset -p yes # Verify qemu installation docker run --platform linux/aarch64 --rm -t nvcr.io/nvidia/deepstream:9.0-samples-multiarch uname -m #aarch64 ``` -------------------------------- ### PGIE Configuration File Example Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_launchpad.ipynb This is an example INI-style configuration file for the Gst-nvinfer plugin. It specifies parameters such as the GPU ID, model paths, network mode, and class attributes for object detection and classification. ```bash [property] gpu-id=0 # Device ID of GPU to use for inference net-scale-factor=0.00392156862745098 # Pixel scaling factor onnx-file=../../../../samples/models/Primary_Detector/resnet18_trafficcamnet_pruned.onnx # Pathname of the TAO toolkit encoded model. model-engine-file=../../../../samples/models/Primary_Detector/resnet18_trafficcamnet_pruned.onnx_b1_gpu0_int8.engine # Path to serialized model engine file labelfile-path=../../../../samples/models/Primary_Detector/labels.txt # Path to text file containing labels int8-calib-file=../../../../samples/models/Primary_Detector/cal_trt.bin # Path to calibration file batch-size=1 # Number of frames/objects to be inferred together in a batch process-mode=1 # Infer Processing Mode 1=Primary Mode 2=Secondary Mode model-color-format=0 # Color format required by the model (ignored if input-tensor-meta enabled) network-mode=1 # Data format to be used by inference 0=FP32, 1=INT8, 2=FP16 mode num-detected-classes=4 # Number of classes detected by the network interval=0 # Number of consecutive batches to be skipped for inference gie-unique-id=1 # Unique ID to assign to GIE #scaling-filter=0 #scaling-compute-hw=0 cluster-mode=2 # Clustering algorithm to use. [class-attrs-all] pre-cluster-threshold=0.2 # Detection threshold to be applied prior to clustering eps=0.2 # Epsilon values for OpenCV grouprectangles() function and DBSCAN algorithm group-threshold=1 # Threshold value for rectangle merging for OpenCV grouprectangles() function ``` -------------------------------- ### Install Jupyter Notebook Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_test_1.ipynb Use this command to install the Jupyter Notebook environment. This is often a prerequisite for running interactive DeepStream samples. ```bash pip3 install notebook ``` -------------------------------- ### Install DeepStream Python Bindings Wheel Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/bindings/README.md Navigate to the dist directory and install the generated pip wheel for DeepStream Python bindings. ```bash cd dist/ pip3 install "./pyds-1.2.3-*.whl" ``` -------------------------------- ### Install PyPA Build Tool Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/bindings/README.md Installs the PyPA build tool using pip3, which is required for the build process. ```bash pip3 install build ``` -------------------------------- ### Activate Environment and Install Dependencies Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/tests/integration/README.md Activate the Python virtual environment and install the DeepStream Python bindings wheel, pytest, pygobject, cuda-python, and a specific version of numpy. Then, navigate to the integration test directory. ```bash . env/bin/activate pip install /opt/nvidia/deepstream/deepstream/sources/deepstream_python_apps/bindings/dist/pyds-1.2.3*.whl pip install pytest pygobject cuda-python numpy==1.26.0 cd /opt/nvidia/deepstream/deepstream/sources/deepstream_python_apps/tests/integration ``` -------------------------------- ### Set up Python 3.12 Virtual Environment Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/tests/integration/README.md Install the python3-venv package and create a virtual environment for Python 3.12. ```bash apt install python3-venv python3 -m venv env ``` -------------------------------- ### Setting up GStreamer Event Loop and Bus Messages Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_test_4.ipynb Initialize a GLib MainLoop, get the pipeline's bus, add a signal watch, and connect a callback for bus messages. Also, set up a probe on the OSD sink pad. ```python loop = GLib.MainLoop() bus = pipeline.get_bus() bus.add_signal_watch() bus.connect("message", bus_call, loop) osdsinkpad = nvosd.get_static_pad("sink") if not osdsinkpad: sys.stderr.write(" Unable to get sink pad of nvosd \n") osdsinkpad.add_probe(Gst.PadProbeType.BUFFER, osd_sink_pad_buffer_probe, 0) ``` -------------------------------- ### Starting and Running the GStreamer Pipeline Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_test_4.ipynb Set the GStreamer pipeline state to PLAYING and run the GLib MainLoop to process events. Includes basic error handling for loop execution. ```python print("Starting pipeline \n") # start play back and listed to events pipeline.set_state(Gst.State.PLAYING) try: loop.run() except: pass ``` -------------------------------- ### Install pyds Wheel Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/FAQ.md Install the pyds wheel using pip3 to ensure the pyds.so library is available in the correct site-packages directory. ```bash $ pip3 install ./pyds-1.2.0*.whl ``` -------------------------------- ### Start and Run DeepStream Pipeline Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_test_1.ipynb Initiates the DeepStream pipeline by setting its state to PLAYING and runs the event loop. Includes cleanup by setting the pipeline state to NULL upon completion or interruption. ```python print("Starting pipeline \n") pipeline.set_state(Gst.State.PLAYING) try: loop.run() except: pass # cleaning up as the pipeline comes to an end pipeline.set_state(Gst.State.NULL) ``` -------------------------------- ### Run Pytest Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/tests/integration/README.md Execute the integration tests using pytest after setting up the environment and installing dependencies. ```bash pytest test.py ``` -------------------------------- ### Install Base Dependencies for Ubuntu 24.04 Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/bindings/README.md Installs essential packages required for compiling bindings on Ubuntu 24.04 with Python 3.12. Includes Python GObject, development headers, Git, Meson, CMake, and GStreamer libraries. ```bash apt install python3-gi python3-dev python3-gst-1.0 python-gi-dev git meson \ python3 python3-pip python3-venv cmake g++ build-essential libglib2.0-dev \ libglib2.0-dev-bin libgstreamer1.0-dev libtool m4 autoconf automake libgirepository-2.0-dev libcairo2-dev ``` -------------------------------- ### Install DeepStream Python Bindings Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/HOWTO.md Install the DeepStream Python bindings module into the standard Python path. This is necessary for Python applications to access DeepStream's MetaData and other functionalities. ```bash cd /opt/nvidia/deepstream/deepstream/lib python3 setup.py install ``` -------------------------------- ### Launch the Cross-Compile Docker Container Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/bindings/README.md Starts the previously built cross-compilation Docker container, mounting a local directory to persist the generated Python wheel package. The tag must match the one used during the build step. ```bash # Create a directory to mount to the container and store your pyds wheel package in mkdir export_pyds # Run the container. Make sure the tag matches the one from Generate step above docker run --platform linux/aarch64 -it --entrypoint bash -v $PWD/export_pyds:/export_pyds deepstream-9.0-ubuntu24.04-python-l4t ``` -------------------------------- ### Install Gst Python Dependencies Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/HOWTO.md Installs necessary development packages and sets environment variables for Gst Python compilation. This is required if Gst Python is not pre-installed. ```bash $ sudo apt-get install python-gi-dev $ export GST_LIBS="-lgstreamer-1.0 -lgobject-2.0 -lglib-2.0" $ export GST_CFLAGS="-pthread -I/usr/include/gstreamer-1.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include" $ git clone https://github.com/GStreamer/gst-python.git $ cd gst-python $ git checkout 5343aeb $ ./autogen.sh PYTHON=python3 $ ./configure PYTHON=python3 $ make $ sudo make install ``` -------------------------------- ### Upgrade Pip Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/bindings/README.md If the wheel installation fails, upgrade pip to the latest version using this command. ```bash python3 -m pip install --upgrade pip ``` -------------------------------- ### DeepStream Pipeline Construction and Execution Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_launchpad.ipynb This snippet shows how to construct a DeepStream pipeline by adding and linking various elements like streammux, pgie, tracker, sgie, tiler, nvvidconv, nvosd, encoder, mux, and sink. It also sets up a main loop to process bus messages and includes probe functionality for buffer inspection. Finally, it starts the pipeline and cleans it up. ```python # Add sink to pipeline pipeline.add(sink) # Note that the sources were already linked to the streammux # Link streammux > pgie streammux.link(pgie) # If tracker is enabled, link pgie > tracker > sgie1 > sgie2 > tiler if enable_tracker: pgie.link(tracker) tracker.link(sgie1) sgie1.link(sgie2) sgie2.link(tiler) # Otherwise, linke pgie > tiler else: pgie.link(tiler) # Link tiler > nvvidconv > nvosd > nvvidconv2 > encoder > parser1 > mux > sink tiler.link(nvvidconv) nvvidconv.link(nvosd) nvosd.link(nvvidconv2) nvvidconv2.link(encoder) encoder.link(parser1) parser1.link(mux) mux.link(sink) loop = GLib.MainLoop() # Create a mainloop bus = pipeline.get_bus() # Retrieve the bus from the pipeline bus.add_signal_watch() # Add a watch for new messages on the bus bus.connect ("message", bus_call, loop) # Connect the loop to the callback function # We add probe to the sink pad of the tiler element, since by that time, # the buffer would have had gotten all the metadata. # Retrieve the sink pad of the tiler tilersinkpad = tiler.get_static_pad("sink") if not tilersinkpad: sys.stderr.write(" Unable to get sink pad of tiler \n") # Add probe function to the sink pad of the tiler tilersinkpad.add_probe(Gst.PadProbeType.BUFFER, tiler_sink_pad_buffer_probe, 0) print("Starting pipeline \n") pipeline.set_state(Gst.State.PLAYING) try: loop.run() except: pass # cleaning up as the pipeline comes to an end pipeline.set_state(Gst.State.NULL) ``` -------------------------------- ### Build HTML Documentation Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/docs/README.md Generates the HTML documentation after setting up the project and dependencies. ```bash make html ``` -------------------------------- ### Initialize GStreamer and Create Pipeline Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_launchpad.ipynb Standard GStreamer initialization and pipeline creation. Ensures the GStreamer environment is set up before adding elements. ```python # Standard initialization procedure Gst.init(None) # Create gstreamer elements # Create Pipeline element that will form a connection of other elements print("Creating Pipeline \n ") pipeline = Gst.Pipeline() if not pipeline: sys.stderr.write(" Unable to create Pipeline \n") number_sources = len(input_file_list) # Declare number of input sources ``` -------------------------------- ### Specify Input and Configuration Files Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_test_1.ipynb Sets the paths for the input video stream and the primary GPU inference engine (PGIE) configuration file. ```python input_file = "/opt/nvidia/deepstream/deepstream/samples/streams/sample_720p.h264" # path to input stream config_file = "../apps/deepstream-test1/dstest1_pgie_config.txt" # path to pgie config file ``` -------------------------------- ### Create and Configure NvOSD (On-Screen Display) Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_launchpad.ipynb Creates an 'nvdsosd' element for displaying overlays and sets its 'process-mode' and 'display-text' properties. Ensure 'OSD_PROCESS_MODE' and 'OSD_DISPLAY_TEXT' are defined. ```python print("Creating nvosd \n ") # Create nvdsosd element nvosd = Gst.ElementFactory.make("nvdsosd", "onscreendisplay") if not nvosd: sys.stderr.write(" Unable to create nvosd \n") # Set nvosd properties nvosd.set_property('process-mode',OSD_PROCESS_MODE) nvosd.set_property('display-text',OSD_DISPLAY_TEXT) # Add nvosd to pipeline pipeline.add(nvosd) ``` -------------------------------- ### Create Video Conversion, Encoding, Muxing, and Parsing Elements Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_launchpad.ipynb This snippet demonstrates the creation of nvvideoconvert, nvv4l2h264enc, qtmux, and h264parse elements. These are essential for converting video formats, encoding to H.264, multiplexing audio/video, and parsing the encoded stream for file output. ```python print("Creating nvvidconv \n ") # Create nvvideoconvert element nvvidconv2 = Gst.ElementFactory.make("nvvideoconvert", "convertor2") if not nvvidconv2: sys.stderr.write(" Unable to create nvvidconv2 \n") print("Creating nvv4l2h264enc \n ") # Create nvv4l2h264enc element encoder = Gst.ElementFactory.make("nvv4l2h264enc", "encoder") if not encoder: sys.stderr.write(" Unable to create encoder \n") print("Creating qtmux \n ") # Create qtmux element mux = Gst.ElementFactory.make("qtmux", "muxer") if not mux: sys.stderr.write(" Unable to create muxer \n") # Create h264parse element print("Creating h264parse\n ") parser1 = Gst.ElementFactory.make("h264parse", "h264-parser2") if not parser1: sys.stderr.write(" Unable to create parser1 \n") # Add all to pipeline pipeline.add(nvvidconv2) pipeline.add(encoder) pipeline.add(mux) pipeline.add(parser1) ``` -------------------------------- ### Bind NvDsInferDataType Enumerator Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/bindings/BINDINGSGUIDE.md Binds the NvDsInferDataType enumerator using pybind11. This example shows how to define enum values and export them for Python use. ```c++ py::enum_(m, "NvDsInferDataType", pydsdoc::NvInferDoc::NvDsInferDataTypeDoc::descr) .value("FLOAT", FLOAT, pydsdoc::NvInferDoc::NvDsInferDataTypeDoc::FLOAT) .value("HALF", HALF, pydsdoc::NvInferDoc::NvDsInferDataTypeDoc::HALF) .value("INT8", INT8, pydsdoc::NvInferDoc::NvDsInferDataTypeDoc::INT8) .value("INT32", INT32, pydsdoc::NvInferDoc::NvDsInferDataTypeDoc::INT32) .export_values(); ``` -------------------------------- ### Downgrade NumPy to Version 1.26.0 Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/README.md If NumPy 2.x is installed, downgrade to version 1.26.0 using this command to ensure compatibility with DeepStream Python bindings. ```bash pip3 install --force-reinstall numpy==1.26.0 ``` -------------------------------- ### Create and Configure SGIE Inference Engines Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_launchpad.ipynb Creates two 'nvinfer' elements for secondary inference and sets their configuration file paths. Ensure 'sgie1_config_file' and 'sgie2_config_file' are defined. ```python print("Creating nvinfer (SGIE1) \n ") # Create nvinfer element sgie1 = Gst.ElementFactory.make("nvinfer", "secondary1-nvinference-engine") if not sgie1: sys.stderr.write(" Unable to make sgie1 \n") print("Creating nvinfer (SGIE2) \n ") # Create nvinfer element sgie2 = Gst.ElementFactory.make("nvinfer", "secondary2-nvinference-engine") if not sgie2: sys.stderr.write(" Unable to make sgie2 \n") # Set config file for each SGIE sgie1.set_property('config-file-path', sgie1_config_file) sgie2.set_property('config-file-path', sgie2_config_file) # Add SGIEs to pipeline pipeline.add(sgie1) pipeline.add(sgie2) ``` -------------------------------- ### Binding a String Property in Python Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/bindings/BINDINGSGUIDE.md Example of how to bind a string data member (sensorStr) of a C++ struct (NvDsEventMsgMeta) to Python using the STRING_PROPERTY macro and the get_string function. ```c++ py::class_(m, "NvDsEventMsgMeta", pydsdoc::metaschema::EventmsgDoc::descr) ... .def_property("sensorStr", STRING_PROPERTY(NvDsEventMsgMeta, sensorStr)) ... ``` -------------------------------- ### Create and Activate Python Virtual Environment for pyds Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/FAQ.md For Python 3.12, create and activate a virtual environment using 'venv' to install and import pip packages like pyds. ```bash sudo apt install python3-venv # Create a venv for pyds python3 -m venv pyds # Activate the environment source ./pyds/bin/activate ``` -------------------------------- ### Create and Link Source Bins to Streammux Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_launchpad.ipynb Iterates through input files, creates a source bin for each, and links it to the nvstreammux. Handles live streams (RTSP) and file inputs. ```python # Iterate through each input source for i in range(number_sources): print("Creating source_bin ",i," \n ") uri_name=input_file_list[i] # Retrieve input file URI if uri_name.find("rtsp://") == 0 : is_live = True source_bin=create_source_bin(i, uri_name) # Call helper to create source bin if not source_bin: sys.stderr.write("Unable to create source bin \n") pipeline.add(source_bin) # Add source bin to pipeline padname="sink_%u" %i sinkpad= streammux.request_pad_simple(padname) # Retrieve a sink pad from the streammux element if not sinkpad: sys.stderr.write("Unable to create sink pad bin \n") srcpad=source_bin.get_static_pad("src") # Retrieve the source pad of the source bin if not srcpad: sys.stderr.write("Unable to create src pad bin \n") srcpad.link(sinkpad) # Link the source pad of the source bin to the sink pad of the streammux ``` -------------------------------- ### Launch DeepStream Test-1 App Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/bindings/README.md Navigate to the deepstream-test1 application directory and run the test script with an input file. ```bash cd apps/deepstream-test1 python3 deepstream_test_1.py ``` -------------------------------- ### Compile Bindings with Custom DeepStream Path on Jetson Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/bindings/README.md Compiles the Python bindings natively on Jetson devices, specifying a custom DeepStream library path. Ensure the path matches your DeepStream installation. ```bash cd deepstream_python_apps/bindings export CMAKE_ARGS="-DDS_PATH=/opt/nvidia/deepstream/deepstream/" export CMAKE_BUILD_PARALLEL_LEVEL=$(nproc) python3 -m build ``` -------------------------------- ### Create and Configure H.264 Encoder and Muxer Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_launchpad.ipynb Creates an 'nvv4l2h264enc' element for H.264 encoding, a 'qtmux' element for multiplexing, and an 'h264parse' element. These are essential for creating an H.264 video file. ```python print("Creating nvvidconv \n ") # Create nvvideoconvert element nvvidconv2 = Gst.ElementFactory.make("nvvideoconvert", "convertor2") if not nvvidconv2: sys.stderr.write(" Unable to create nvvidconv2 \n") print("Creating nvv4l2h264enc \n ") # Create nvv4l2h264enc element encoder = Gst.ElementFactory.make("nvv4l2h264enc", "encoder") if not encoder: sys.stderr.write(" Unable to create encoder \n") print("Creating qtmux \n ") # Create qtmux element mux = Gst.ElementFactory.make("qtmux", "muxer") if not mux: sys.stderr.write(" Unable to create muxer \n") # Create h264parse element print("Creating h264parse\n ") parser1 = Gst.ElementFactory.make("h264parse", "h264-parser2") if not parser1: sys.stderr.write(" Unable to create parser1 \n") # Add all to pipeline pipeline.add(nvvidconv2) pipeline.add(encoder) pipeline.add(mux) pipeline.add(parser1) ``` -------------------------------- ### Get String Function for Python Bindings Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/bindings/BINDINGSGUIDE.md This function binds a C++ function to Python that returns a C-style string from a given memory address. It's used to retrieve string references from the C side. ```c++ m.def("get_string", [](size_t ptr) { return (char *) ptr; // Return reference to string from address }, "ptr"_a, py::return_value_policy::reference, // Owned by C side pydsdoc::methodsDoc::get_string); ``` -------------------------------- ### Create and Configure File Sink Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_launchpad.ipynb Creates a 'filesink' element to write the output to a specified file. Sets the 'location' and 'sync' properties. Ensure 'output_file' is defined. ```python print("Creating FileSink \n") # Create filesink sink = Gst.ElementFactory.make("filesink", "nvvideo-renderer") # Set filesink properties sink.set_property('location', output_file) sink.set_property("sync",0) ``` -------------------------------- ### Retrieve String Value from Address Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/HOWTO.md Convert a C string address (integer) into a readable Python string. This function is used in conjunction with reading the string field's address to get the actual string content. ```python print(pyds.get_string(obj.type)) ``` -------------------------------- ### Set File Paths and Additional Options Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_launchpad.ipynb Defines input stream URIs, output file path, configuration files for GIEs and tracker, and various visual properties for bounding boxes and display text. ```python # File paths input_file_list = ["file:///opt/nvidia/deepstream/deepstream/samples/streams/sample_720p.h264", \ "file:///opt/nvidia/deepstream/deepstream/samples/streams/sample_720p.h264", \ "file:///opt/nvidia/deepstream/deepstream/samples/streams/sample_720p.h264", \ "file:///opt/nvidia/deepstream/deepstream/samples/streams/sample_720p.h264"] # List of paths to input streams in URI format. Here we use 4 of the same stream as input. output_file = "output.mp4" # Output file location pgie_config_file = "./configs/dslaunchpad_pgie_config.txt" # Path to pgie config file tracker_config_file = "./configs/dslaunchpad_tracker_config.txt" # Path to tracker config file sgie1_config_file = "./configs/dslaunchpad_sgie1_config.txt" # Path to config file for first sgie sgie2_config_file = "./configs/dslaunchpad_sgie2_config.txt" # Path to config file for second sgie # Tracker options enable_tracker = 1 # Enable/disable tracker and SGIEs. 0 for disable, 1 for enable # Note: for colors, each Red/Green/Blue/Alpha (RGBA) value should be between 0.0 and 1.0, inclusive. # Bounding box options bbox_border_color = {"R": 1.0, "G": 0.0, "B": 0.0, "A": 1.0} # Color of bounding box. Set to red bbox_has_bg_color = False # Bool for whether bounding box has background color bbox_bg_color = {"R": 1.0, "G": 0.0, "B": 0.0, "A": 0.2} # Color of bbox background. Set to red with transparency # Display text options, to be added to the frame. text_x_offset = 10 # Offset in the x direction where string should appear text_y_offset = 12 # Offset in the y direction where string should appear text_font_name = "Serif" # Font name text_font_size = 10 # Font size text_font_color = {"R" : 1.0, "G": 1.0, "B": 1.0, "A": 1.0} # Color of text font. Set to white text_set_bg_color = True # Bool for whether text box has background color text_bg_color = {"R": 0.0, "G": 0.0, "B": 0.0, "A": 1.0} # Color of text box background. Set to black ``` -------------------------------- ### Quick Build for x86-ubuntu-24.04 Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/bindings/README.md Compiles the Python bindings for x86-ubuntu-24.04 with Python 3.12 and DeepStream 9.0. Ensure you are in the 'bindings' directory and set the parallel build level. ```bash cd deepstream_python_apps/bindings export CMAKE_BUILD_PARALLEL_LEVEL=$(nproc) python3 -m build ``` -------------------------------- ### Create and Configure NVMultiStreamTiler Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_launchpad.ipynb Creates an 'nvmultistreamtiler' element and sets its rows, columns, width, and height properties based on the number of input sources. Ensure 'number_sources', 'TILED_OUTPUT_WIDTH', and 'TILED_OUTPUT_HEIGHT' are defined. ```python print("Creating tiler \n ") # Create nvmultistreamtiler element tiler=Gst.ElementFactory.make("nvmultistreamtiler", "nvtiler") if not tiler: sys.stderr.write(" Unable to create tiler \n") # Calculate number of rows and columns based on number of input sources tiler_rows=int(math.sqrt(number_sources)) tiler_columns=int(math.ceil((1.0*number_sources)/tiler_rows)) # Set tiler properties tiler.set_property("rows",tiler_rows) tiler.set_property("columns",tiler_columns) tiler.set_property("width", TILED_OUTPUT_WIDTH) tiler.set_property("height", TILED_OUTPUT_HEIGHT) # Add tiler to pipeline pipeline.add(tiler) ``` -------------------------------- ### DeepStream Test 4 Configuration Variables Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_test_4.ipynb Sets up configuration file paths, input file, protocol library, Kafka connection string, topic, display setting, and schema type for DeepStream test 4. ```python MAX_DISPLAY_LEN = 64 MAX_TIME_STAMP_LEN = 32 PGIE_CLASS_ID_VEHICLE = 0 PGIE_CLASS_ID_BICYCLE = 1 PGIE_CLASS_ID_PERSON = 2 PGIE_CLASS_ID_ROADSIGN = 3 MUXER_OUTPUT_WIDTH = 1920 MUXER_OUTPUT_HEIGHT = 1080 MUXER_BATCH_TIMEOUT_USEC = 33000 cfg_file = "../apps/deepstream-test4/cfg_kafka.txt" input_file = "/opt/nvidia/deepstream/deepstream/samples/streams/sample_720p.h264" proto_lib = "/opt/nvidia/deepstream/deepstream/lib/libnvds_kafka_proto.so" conn_str = "localhost;2181;testTopic" # make sure to change to your own connection string topic = "topic" no_display = False schema_type = 0 ``` -------------------------------- ### Setting PGIE and Streammux Properties Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_test_4.ipynb Configure properties for PGIE, Streammux, message converter, and message broker. Ensure input file and configuration paths are correctly set. ```python print("Playing file %s " % input_file) source.set_property('location', input_file) streammux.set_property('width', 1920) streammux.set_property('height', 1080) streammux.set_property('batch-size', 1) streammux.set_property('batched-push-timeout', 33000) pgie.set_property('config-file-path', PGIE_CONFIG_FILE) msgconv.set_property('config', MSCONV_CONFIG_FILE) msgconv.set_property('payload-type', schema_type) msgbroker.set_property('proto-lib', proto_lib) msgbroker.set_property('conn-str', conn_str) if cfg_file is not None: msgbroker.set_property('config', cfg_file) if topic is not None: msgbroker.set_property('topic', topic) msgbroker.set_property('sync', False) ``` -------------------------------- ### Prepare JetPack SDK Files for Cross-Compilation Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/bindings/README.md Organizes downloaded NVIDIA SDK Manager components into a specific directory structure required for the Docker build process. This ensures all necessary low-level libraries are available. ```bash # create directories as follows: mkdir -p deepstream_python_apps/bindings/docker/jetpack_files # would be /opt/nvidia/deepstream/deepstream/sources/deepstream_python_apps/ # where you downloaded the deepstream_python_apps repository mv ~/Downloads/nvidia/sdkm_downloads/* /deepstream_python_apps/bindings/docker/jetpack_files ``` -------------------------------- ### DeepStream Source Bin Creation and Callbacks Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_launchpad.ipynb This Python code defines helper functions for creating GStreamer source bins using `uridecodebin`. It includes callbacks to handle new pads and child elements added to the decodebin, ensuring proper linking with NVIDIA decoders and setting properties like `drop-on-latency`. ```python def cb_newpad(decodebin, decoder_src_pad,data): print("In cb_newpad\n") caps=decoder_src_pad.get_current_caps() if not caps: caps = decoder_src_pad.query_caps() gststruct=caps.get_structure(0) gstname=gststruct.get_name() source_bin=data features=caps.get_features(0) # Need to check if the pad created by the decodebin is for video and not # audio. print("gstname=",gstname) if(gstname.find("video")!=-1): # Link the decodebin pad only if decodebin has picked nvidia # decoder plugin nvdec_*. We do this by checking if the pad caps contain # NVMM memory features. print("features=",features) if features.contains("memory:NVMM"): # Get the source bin ghost pad bin_ghost_pad=source_bin.get_static_pad("src") if not bin_ghost_pad.set_target(decoder_src_pad): sys.stderr.write("Failed to link decoder src pad to source bin ghost pad\n") else: sys.stderr.write(" Error: Decodebin did not pick nvidia decoder plugin.\n") def decodebin_child_added(child_proxy,Object,name,user_data): print("Decodebin child added:", name, "\n") # Connect callback to internal decodebin signal if(name.find("decodebin") != -1): Object.connect("child-added",decodebin_child_added,user_data) if "source" in name: source_element = child_proxy.get_by_name("source") if source_element.find_property('drop-on-latency') != None: Object.set_property("drop-on-latency", True) def create_source_bin(index,uri): print("Creating source bin") # Create a source GstBin to abstract this bin's content from the rest of the # pipeline bin_name="source-bin-%02d" %index print(bin_name) nbin=Gst.Bin.new(bin_name) if not nbin: sys.stderr.write(" Unable to create source bin \n") # Source element for reading from the uri. # We will use decodebin and let it figure out the container format of the # stream and the codec and plug the appropriate demux and decode plugins. uri_decode_bin=Gst.ElementFactory.make("uridecodebin", "uri-decode-bin") if not uri_decode_bin: sys.stderr.write(" Unable to create uri decode bin \n") # We set the input uri to the source element uri_decode_bin.set_property("uri",uri) # Connect to the "pad-added" signal of the decodebin which generates a # callback once a new pad for raw data has been created by the decodebin uri_decode_bin.connect("pad-added",cb_newpad,nbin) uri_decode_bin.connect("child-added",decodebin_child_added,nbin) # We need to create a ghost pad for the source bin which will act as a proxy # for the video decoder src pad. The ghost pad will not have a target right # now. Once the decode bin creates the video decoder and generates the # cb_newpad callback, we will set the ghost pad target to the video decoder # src pad. Gst.Bin.add(nbin,uri_decode_bin) bin_pad=nbin.add_pad(Gst.GhostPad.new_no_target("src",Gst.PadDirection.SRC)) if not bin_pad: sys.stderr.write(" Failed to add ghost pad in source bin \n") return None return nbin ``` -------------------------------- ### Download and Compile ReID Model Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/tests/integration/README.md Download the ReID model and compile it into an engine file using trtexec. This is a prerequisite for running tests that involve object tracking. ```bash cd /opt/nvidia/deepstream/deepstream/samples/models mkdir Tracker && cd Tracker wget --content-disposition 'https://api.ngc.nvidia.com/v2/models/org/nvidia/team/tao/reidentificationnet/deployable_v1.2/files?redirect=true&path=resnet50_market1501_aicity156.onnx' -O resnet50_market1501_aicity156.onnx export PATH=$PATH:/usr/src/tensorrt/bin trtexec --minShapes=input:1x3x256x128 --optShapes=input:8x3x256x128 --maxShapes=input:16x3x256x128 --fp16 --saveEngine=resnet50_market1501_aicity156.onnx_b16_gpu0_fp16.engine --onnx=resnet50_market1501_aicity156.onnx ``` -------------------------------- ### Quick Build for SBSA-ubuntu-24.04 Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/bindings/README.md Compiles the Python bindings natively on an SBSA or DGX Spark Platform with Python 3.12 and DeepStream 9.0. Set the CMAKE_ARGS to indicate the SBSA platform and configure the parallel build level. ```bash cd deepstream_python_apps/bindings export CMAKE_ARGS="-DIS_SBSA=on" export CMAKE_BUILD_PARALLEL_LEVEL=$(nproc) python3 -m build ``` -------------------------------- ### Configure Input Source and Streammux Properties Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_test_1.ipynb Sets the input file location and configures properties for the stream multiplexer and primary GIE. Note the conditional configuration for 'nvstreammux'. ```python # Edit this cell to change the inputs and the stream config print("Playing file %s " % input_file) source.set_property('location', \ input_file) if os.environ.get('USE_NEW_NVSTREAMMUX') != 'yes': # Only set these properties if not using new gst-nvstreammux streammux.set_property('width', 1920) streammux.set_property('height', 1080) streammux.set_property('batched-push-timeout', 4000000) streammux.set_property('batch-size', 1) pgie.set_property('config-file-path', \ config_file) ``` -------------------------------- ### Run DeepStream Pipeline with Output File Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_launchpad.ipynb This snippet demonstrates how to execute a pre-built DeepStream pipeline and specify an output file name for the processed video. Ensure the pipeline is built before calling this function. ```python output_file = "output2.mp4" build_and_run_pipeline() ``` -------------------------------- ### Enable New GStreamer nvstreammux Plugin Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/FAQ.md Set the USE_NEW_NVSTREAMMUX environment variable to 'yes' to enable the new GStreamer nvstreammux plugin. This is necessary to avoid errors related to deprecated properties. ```bash export USE_NEW_NVSTREAMMUX="yes" python3 deepstream_test_1.py ../../../../samples/streams/sample_720p.h264 ``` -------------------------------- ### Create and Configure nvinfer (PGIE) Element Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_launchpad.ipynb This snippet demonstrates how to create the Gst-nvinfer element for primary inference. It sets the configuration file path and adjusts the batch size if necessary to match the number of input sources. ```python print("Creating nvinfer (PGIE) \n ") # Create nvinfer element pgie = Gst.ElementFactory.make("nvinfer", "primary-inference") if not pgie: sys.stderr.write(" Unable to create pgie \n") # Set nvinfer properties pgie.set_property('config-file-path', pgie_config_file) pgie_batch_size=pgie.get_property("batch-size") if(pgie_batch_size != number_sources): print("WARNING: Overriding infer-config batch-size",pgie_batch_size," with number of sources ", number_sources," \n") pgie.set_property("batch-size",number_sources) # Add pgie to pipeline pipeline.add(pgie) ``` -------------------------------- ### Create and Configure Filesink Element Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_launchpad.ipynb This snippet shows how to create a filesink element to write the processed stream to a file. It sets the output file location and configures the sink not to sync to the clock, allowing for faster playback. ```python print("Creating FileSink \n") # Create filesink sink = Gst.ElementFactory.make("filesink", "nvvideo-renderer") # Set filesink properties sink.set_property('location', output_file) sink.set_property("sync",0) # Don't sync to clock, to allow samples to be played as fast as possible # Add sink to pipeline pipeline.add(sink) ``` -------------------------------- ### Display Output Video Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_launchpad.ipynb Instantiate the Video object with the output file name to display the processed video. This is typically done after the pipeline has finished processing. ```python Video(output_file) ``` -------------------------------- ### Create and Configure NVVideoConvert Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_launchpad.ipynb Creates an 'nvvideoconvert' element, which is used for video format conversions within the pipeline. This element is added to the pipeline. ```python print("Creating nvvidconv \n ") # Create nvvideoconvert element nvvidconv = Gst.ElementFactory.make("nvvideoconvert", "convertor") if not nvvidconv: sys.stderr.write(" Unable to create nvvidconv \n") # Add nvvideoconvert to pipeline pipeline.add(nvvidconv) ``` -------------------------------- ### DeepStream Initialization Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_test_1.ipynb Initializes the GStreamer framework and creates the main pipeline element for the DeepStream application. Requires `sys` and `Gst` modules. ```python platform_info = PlatformInfo() # Standard initialization procedure Gst.init(None) # Create gstreamer elements # Create Pipeline element that will form a connection of other elements print("Creating Pipeline \n ") pipeline = Gst.Pipeline() if not pipeline: sys.stderr.write(" Unable to create Pipeline \n") ``` -------------------------------- ### Initialize Git Submodules and Restore Sparse Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/bindings/README.md Initializes and updates Git submodules for gst-python and pybind11, and then restores sparse submodules. This command is run from the deepstream_python_apps directory. ```bash cd /opt/nvidia/deepstream/deepstream/sources/deepstream_python_apps/ git submodule update --init python3 bindings/3rdparty/git-partial-submodule/git-partial-submodule.py restore-sparse ``` -------------------------------- ### Create and Activate Python Virtual Environment Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/README.md Use this command to create a Python virtual environment for DeepStream Python bindings, especially for Python 3.12. Activate the environment using the provided command. ```bash sudo apt install python3-venv # Create a venv for pyds python3 -m venv pyds (for Spark - bug fix) python3 -m venv --system-site-packages pyds # Activate the environment source ./pyds/bin/activate ``` -------------------------------- ### Create and Configure nvstreammux Element Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_launchpad.ipynb Initializes the nvstreammux element for batching frames from multiple sources. Sets properties like resolution, batch size, and timeout. ```python print("Creating streammux \n ") # Create nvstreammux instance to form batches from one or more sources. streammux = Gst.ElementFactory.make("nvstreammux", "Stream-muxer") if not streammux: sys.stderr.write(" Unable to create NvStreamMux \n") # Set streammux properties streammux.set_property('width', 1920) streammux.set_property('height', 1080) streammux.set_property('batch-size', number_sources) streammux.set_property('batched-push-timeout', 4000000) pipeline.add(streammux) # Add streammux to the pipeline ``` -------------------------------- ### configure_source_for_ntp_sync Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/docs/PYTHON_API/Methods/methodsdoc.md Configures a source element for NTP synchronization. ```APIDOC ## configure_source_for_ntp_sync ### Description Configures a source element to synchronize with Network Time Protocol (NTP). ### Method (Not specified, likely a function call) ### Endpoint (Not applicable for SDK methods) ### Parameters (Parameters not specified in the source text) ### Request Example (Not applicable for SDK methods) ### Response (Response details not specified in the source text) ``` -------------------------------- ### Build and Run DeepStream Pipeline Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_launchpad.ipynb Execute the function to build and run the configured DeepStream pipeline. This action processes the input streams according to the specified parameters. ```python build_and_run_pipeline() ``` -------------------------------- ### Create File Source Element Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_test_1.ipynb Creates a file source element for reading data from a file. Ensure the Gst library is imported. ```python print("Creating Source \n ") source = Gst.ElementFactory.make("filesrc", "file-source") if not source: sys.stderr.write(" Unable to create Source \n") ``` -------------------------------- ### Create Event Loop and Watch Bus Messages Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_test_1.ipynb Sets up a GLib MainLoop to process GStreamer bus messages. Connects a callback function to handle messages from the pipeline's bus. ```python loop = GLib.MainLoop() bus = pipeline.get_bus() bus.add_signal_watch() bus.connect ("message", bus_call, loop) ``` -------------------------------- ### Configure DeepStream Pipeline Options Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_launchpad.ipynb Set various parameters for the DeepStream pipeline, including input/output file paths, tracker enablement, bounding box styling, and text overlay properties. Ensure color values are between 0.0 and 1.0. ```python # File paths input_file_list = ["file:///opt/nvidia/deepstream/deepstream/samples/streams/sample_720p.h264", \ "file:///opt/nvidia/deepstream/deepstream/samples/streams/sample_720p.h264"] # List of paths to input streams in URI format output_file = "output7.mp4" # Output file location # Tracker options enable_tracker = 1 # Enable/disable tracker and SGIEs. 0 for disable, 1 for enable # Note: for colors, each Red/Green/Blue/Alpha (RGBA) value should be between 0.0 and 1.0, inclusive. # Bounding box optionsbox_border_color = {"R": 1.0, "G": 0.0, "B": 0.0, "A": 1.0} # Color of bounding box. Set to redbox_has_bg_color = False # Bool for whether bounding box has background colorbox_bg_color = {"R": 1.0, "G": 0.0, "B": 0.0, "A": 0.2} # Color of bbox background. Set to red with transparency # Display text options, to be added to the frame. text_x_offset = 10 # Offset in the x direction where string should appear text_y_offset = 12 # Offset in the y direction where string should appear text_font_name = "Serif" # Font name text_font_size = 10 # Font size text_font_color = {"R" : 1.0, "G": 1.0, "B": 1.0, "A": 1.0} # Color of text font. Set to white text_set_bg_color = True # Bool for whether text box has background color text_bg_color = {"R": 0.0, "G": 0.0, "B": 0.0, "A": 1.0} # Color of text box background. Set to black ``` -------------------------------- ### Declare Tiler and OSD Properties Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_launchpad.ipynb Sets the output width and height for the tiler in pixels and configures the OSD process mode (CPU/GPU) and display text option. ```python TILED_OUTPUT_WIDTH=1920 # Tiler output width TILED_OUTPUT_HEIGHT=1080 # Tiler output height # NvOSD options OSD_PROCESS_MODE= 0 # Process mode for NvOSD plugin. 0 for CPU, 1 for GPU OSD_DISPLAY_TEXT= 1 # Enable/disable display of all text in NvOSD plugin. 0 for disable, 1 for enable ``` -------------------------------- ### DeepStream Test 4 Configuration Files and Class Labels Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/notebooks/deepstream_test_4.ipynb Defines paths for primary GPU inference (PGIE) and message conversion (MSCONV) configuration files, along with a list of class names for object detection. ```python PGIE_CONFIG_FILE = "../apps/deepstream-test4/dstest4_pgie_config.txt" MSCONV_CONFIG_FILE = "../apps/deepstream-test4/dstest4_msgconv_config.txt" pgie_classes_str = ["Vehicle", "TwoWheeler", "Person", "Roadsign"] ``` -------------------------------- ### Run Integration Tests Source: https://github.com/nvidia-ai-iot/deepstream_python_apps/blob/master/tests/integration/README.md Navigate to the integration test directory and execute tests using pytest. ```bash cd tests/integration pytest test.py ```