Last active
June 1, 2022 11:27
-
-
Save MovsisyanM/f4f11f903fc52d4d7c25f32f1b24fda0 to your computer and use it in GitHub Desktop.
Infinite wait - deepstream-test1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Creating a deepstream pipeline" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import sys\n", | |
"sys.path.append('../')\n", | |
"import gi\n", | |
"import configparser\n", | |
"gi.require_version('Gst', '1.0')\n", | |
"from gi.repository import GLib, Gst\n", | |
"from ctypes import *\n", | |
"import time\n", | |
"import sys\n", | |
"import math\n", | |
"import platform\n", | |
"from common.is_aarch_64 import is_aarch64\n", | |
"from common.bus_call import bus_call\n", | |
"from common.FPS import PERF_DATA\n", | |
"\n", | |
"import cv2\n", | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import gi\n", | |
"\n", | |
"perf_data = None\n", | |
"\n", | |
"MAX_DISPLAY_LEN=64\n", | |
"\n", | |
"PGIE_CLASS_ID_VEHICLE = 0\n", | |
"PGIE_CLASS_ID_BICYCLE = 1\n", | |
"PGIE_CLASS_ID_PERSON = 2\n", | |
"PGIE_CLASS_ID_ROADSIGN = 3\n", | |
"pgie_classes_str= [\"Vehicle\", \"TwoWheeler\", \"Person\",\"RoadSign\"]\n", | |
"\n", | |
"MUXER_OUTPUT_WIDTH=1920\n", | |
"MUXER_OUTPUT_HEIGHT=1080\n", | |
"MUXER_BATCH_TIMEOUT_USEC=4000000\n", | |
"\n", | |
"TILED_OUTPUT_WIDTH=1280\n", | |
"TILED_OUTPUT_HEIGHT=720\n", | |
"\n", | |
"GST_CAPS_FEATURES_NVMM=\"memory:NVMM\"\n", | |
"\n", | |
"OSD_PROCESS_MODE= 0\n", | |
"OSD_DISPLAY_TEXT= 1\n", | |
"\n", | |
"args = [\"testing.py\", \"/home/mher/projects/counter/sample_h264.mp4\", \"/home/mher/projects/counter/sample_pyout_h264.mp4\"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from common.is_aarch_64 import is_aarch64\n", | |
"from common.bus_call import bus_call\n", | |
"from common.FPS import PERF_DATA\n", | |
"\n", | |
"PGIE_CLASS_ID_VEHICLE = 2\n", | |
"PGIE_CLASS_ID_PERSON = 0\n", | |
"TILED_OUTPUT_WIDTH = 1920\n", | |
"TILED_OUTPUT_HEIGHT = 1080" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def osd_sink_pad_buffer_probe(pad,info,u_data):\n", | |
" frame_number=0\n", | |
" print(frame_number)\n", | |
" #Intiallizing object counter with 0.\n", | |
" obj_counter = {\n", | |
" PGIE_CLASS_ID_VEHICLE:0,\n", | |
" PGIE_CLASS_ID_PERSON:0,\n", | |
" PGIE_CLASS_ID_BICYCLE:0,\n", | |
" PGIE_CLASS_ID_ROADSIGN:0\n", | |
" }\n", | |
" num_rects=0\n", | |
"\n", | |
" gst_buffer = info.get_buffer()\n", | |
" if not gst_buffer:\n", | |
" print(\"Unable to get GstBuffer \")\n", | |
" return\n", | |
"\n", | |
" # Retrieve batch metadata from the gst_buffer\n", | |
" # Note that pyds.gst_buffer_get_nvds_batch_meta() expects the\n", | |
" # C address of gst_buffer as input, which is obtained with hash(gst_buffer)\n", | |
" batch_meta = pyds.gst_buffer_get_nvds_batch_meta(hash(gst_buffer))\n", | |
" l_frame = batch_meta.frame_meta_list\n", | |
" while l_frame is not None:\n", | |
" try:\n", | |
" # Note that l_frame.data needs a cast to pyds.NvDsFrameMeta\n", | |
" # The casting is done by pyds.glist_get_nvds_frame_meta()\n", | |
" # The casting also keeps ownership of the underlying memory\n", | |
" # in the C code, so the Python garbage collector will leave\n", | |
" # it alone.\n", | |
" #frame_meta = pyds.glist_get_nvds_frame_meta(l_frame.data)\n", | |
" frame_meta = pyds.NvDsFrameMeta.cast(l_frame.data)\n", | |
" except StopIteration:\n", | |
" break\n", | |
"\n", | |
" frame_number=frame_meta.frame_num\n", | |
" num_rects = frame_meta.num_obj_meta\n", | |
" l_obj=frame_meta.obj_meta_list\n", | |
" while l_obj is not None:\n", | |
" try:\n", | |
" # Casting l_obj.data to pyds.NvDsObjectMeta\n", | |
" #obj_meta=pyds.glist_get_nvds_object_meta(l_obj.data)\n", | |
" obj_meta=pyds.NvDsObjectMeta.cast(l_obj.data)\n", | |
" except StopIteration:\n", | |
" break\n", | |
" obj_counter[obj_meta.class_id] += 1\n", | |
" obj_meta.rect_params.border_color.set(0.0, 0.0, 1.0, 0.0)\n", | |
" try: \n", | |
" l_obj=l_obj.next\n", | |
" except StopIteration:\n", | |
" break\n", | |
"\n", | |
" # Acquiring a display meta object. The memory ownership remains in\n", | |
" # the C code so downstream plugins can still access it. Otherwise\n", | |
" # the garbage collector will claim it when this probe function exits.\n", | |
" display_meta=pyds.nvds_acquire_display_meta_from_pool(batch_meta)\n", | |
" display_meta.num_labels = 1\n", | |
" py_nvosd_text_params = display_meta.text_params[0]\n", | |
" # Setting display text to be shown on screen\n", | |
" # Note that the pyds module allocates a buffer for the string, and the\n", | |
" # memory will not be claimed by the garbage collector.\n", | |
" # Reading the display_text field here will return the C address of the\n", | |
" # allocated string. Use pyds.get_string() to get the string content.\n", | |
" py_nvosd_text_params.display_text = \"Frame Number={} Number of Objects={} Vehicle_count={} Person_count={}\".format(frame_number, num_rects, obj_counter[PGIE_CLASS_ID_VEHICLE], obj_counter[PGIE_CLASS_ID_PERSON])\n", | |
"\n", | |
" # Now set the offsets where the string should appear\n", | |
" py_nvosd_text_params.x_offset = 10\n", | |
" py_nvosd_text_params.y_offset = 12\n", | |
"\n", | |
" # Font , font-color and font-size\n", | |
" py_nvosd_text_params.font_params.font_name = \"Serif\"\n", | |
" py_nvosd_text_params.font_params.font_size = 10\n", | |
" # set(red, green, blue, alpha); set to White\n", | |
" py_nvosd_text_params.font_params.font_color.set(1.0, 1.0, 1.0, 1.0)\n", | |
"\n", | |
" # Text background color\n", | |
" py_nvosd_text_params.set_bg_clr = 1\n", | |
" # set(red, green, blue, alpha); set to Black\n", | |
" py_nvosd_text_params.text_bg_clr.set(0.0, 0.0, 0.0, 1.0)\n", | |
" # Using pyds.get_string() to get display_text as string\n", | |
" print(pyds.get_string(py_nvosd_text_params.display_text))\n", | |
" pyds.nvds_add_display_meta_to_frame(frame_meta, display_meta)\n", | |
" try:\n", | |
" l_frame=l_frame.next\n", | |
" except StopIteration:\n", | |
" break\n", | |
"\n", | |
" return Gst.PadProbeReturn.OK" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Creating Pipeline \n", | |
" \n" | |
] | |
} | |
], | |
"source": [ | |
"# Standard GStreamer initialization\n", | |
"Gst.init(None)\n", | |
"\n", | |
"# Create gstreamer elements\n", | |
"# Create Pipeline element that will form a connection of other elements\n", | |
"print(\"Creating Pipeline \\n \")\n", | |
"pipeline = Gst.Pipeline()\n", | |
"if not pipeline:\n", | |
" print(\" Unable to create Pipeline \\n\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Creating Source \n", | |
" \n" | |
] | |
} | |
], | |
"source": [ | |
"# Source element for reading from the file\n", | |
"print(\"Creating Source \\n \")\n", | |
"source = Gst.ElementFactory.make(\"filesrc\", \"file-source\")\n", | |
"if not source:\n", | |
" print(\" Unable to create Source \\n\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Creating H264Parser \n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"# Since the data format in the input file is elementary h264 stream,\n", | |
"# we need a h264parser\n", | |
"print(\"Creating H264Parser \\n\")\n", | |
"h264parser = Gst.ElementFactory.make(\"h264parse\", \"h264-parser\")\n", | |
"if not h264parser:\n", | |
" print(\" Unable to create h264 parser \\n\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Creating Decoder \n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"# Use nvdec_h264 for hardware accelerated decode on GPU\n", | |
"print(\"Creating Decoder \\n\")\n", | |
"decoder = Gst.ElementFactory.make(\"nvv4l2decoder\", \"nvv4l2-decoder\")\n", | |
"if not decoder:\n", | |
" print(\" Unable to create Nvv4l2 Decoder \\n\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Creating Muxer \n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"# Create nvstreammux instance to form batches from one or more sources.\n", | |
"print(\"Creating Muxer \\n\")\n", | |
"streammux = Gst.ElementFactory.make(\"nvstreammux\", \"Stream-muxer\")\n", | |
"if not streammux:\n", | |
" print(\" Unable to create NvStreamMux \\n\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Creating pgie\n" | |
] | |
} | |
], | |
"source": [ | |
"# Use nvinfer to run inferencing on decoder's output,\n", | |
"# behaviour of inferencing is set through config file\n", | |
"print(\"Creating pgie\")\n", | |
"pgie = Gst.ElementFactory.make(\"nvinfer\", \"primary-inference\")\n", | |
"if not pgie:\n", | |
" print(\" Unable to create pgie \\n\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Creating nvvidconv\n" | |
] | |
} | |
], | |
"source": [ | |
"# Use convertor to convert from NV12 to RGBA as required by nvosd\n", | |
"print(\"Creating nvvidconv\")\n", | |
"nvvidconv = Gst.ElementFactory.make(\"nvvideoconvert\", \"convertor\")\n", | |
"if not nvvidconv:\n", | |
" print(\" Unable to create nvvidconv \\n\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Creating OSD\n" | |
] | |
} | |
], | |
"source": [ | |
"# Create OSD to draw on the converted RGBA buffer\n", | |
"print(\"Creating OSD\")\n", | |
"nvosd = Gst.ElementFactory.make(\"nvdsosd\", \"onscreendisplay\")\n", | |
"if not nvosd:\n", | |
" print(\" Unable to create nvosd \\n\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Finally render the osd output\n", | |
"# if is_aarch64():\n", | |
"# transform = Gst.ElementFactory.make(\"nvegltransform\", \"nvegl-transform\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Creating File sink \n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"print(\"Creating File sink \\n\")\n", | |
"sink = Gst.ElementFactory.make(\"filesink\", \"nvvideo-renderer\")\n", | |
"if not sink:\n", | |
" print(\" Unable to create file sink \\n\")\n", | |
"\n", | |
"# print(\"Creating EGL sink \\n\")\n", | |
"# sink = Gst.ElementFactory.make(\"nveglglessink\", \"nvvideo-renderer\")\n", | |
"# if not sink:\n", | |
"# print(\" Unable to create egl sink \\n\")\n", | |
"\n", | |
"# print(\"Creating fake sink \\n\")\n", | |
"# sink = Gst.ElementFactory.make(\"fakesink\", \"nvvideo-renderer\")\n", | |
"# if not sink:\n", | |
"# print(\" Unable to create fake sink \\n\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Playing file /home/mher/projects/counter/sample_h264.mp4 \n", | |
"Adding elements to Pipeline \n", | |
"\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 15, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"print(\"Playing file %s \" %args[1])\n", | |
"source.set_property('location', args[1])\n", | |
"sink.set_property(\"location\", args[2])\n", | |
"streammux.set_property('width', 1920)\n", | |
"streammux.set_property('height', 1080)\n", | |
"streammux.set_property('batch-size', 1)\n", | |
"streammux.set_property('batched-push-timeout', 4000000)\n", | |
"pgie.set_property('config-file-path', \"/home/mher/projects/counter/deepstream-analytics/dsnvanalytics_pgie_config.txt\")\n", | |
"\n", | |
"\n", | |
"print(\"Adding elements to Pipeline \\n\")\n", | |
"pipeline.add(source)\n", | |
"pipeline.add(h264parser)\n", | |
"pipeline.add(decoder)\n", | |
"pipeline.add(streammux)\n", | |
"pipeline.add(pgie)\n", | |
"pipeline.add(nvvidconv)\n", | |
"# pipeline.add(nvosd)\n", | |
"pipeline.add(sink)\n", | |
"# if is_aarch64():\n", | |
"# pipeline.add(transform)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Linking elements in the Pipeline \n", | |
"\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 16, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# we link the elements together\n", | |
"# file-source -> h264-parser -> nvh264-decoder ->\n", | |
"# nvinfer -> nvvidconv -> nvosd -> video-renderer\n", | |
"print(\"Linking elements in the Pipeline \\n\")\n", | |
"\n", | |
"source.link(h264parser)\n", | |
"h264parser.link(decoder)\n", | |
"\n", | |
"sinkpad = streammux.get_request_pad(\"sink_0\")\n", | |
"if not sinkpad:\n", | |
" print(\" Unable to get the sink pad of streammux \\n\")\n", | |
" \n", | |
"srcpad = decoder.get_static_pad(\"src\")\n", | |
"if not srcpad:\n", | |
" print(\" Unable to get source pad of decoder \\n\")\n", | |
" \n", | |
"srcpad.link(sinkpad)\n", | |
"streammux.link(pgie)\n", | |
"pgie.link(nvvidconv)\n", | |
"\n", | |
"nvvidconv.link(sink)\n", | |
"# nvosd.link(sink)\n", | |
"# nvvidconv.link(nvosd)\n", | |
"# if is_aarch64():\n", | |
"# nvosd.link(transform)\n", | |
"# transform.link(sink)\n", | |
"# else:\n", | |
"# nvosd.link(sink)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1" | |
] | |
}, | |
"execution_count": 17, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# create an event loop and feed gstreamer bus mesages to it\n", | |
"loop = GLib.MainLoop()\n", | |
"bus = pipeline.get_bus()\n", | |
"bus.add_signal_watch()\n", | |
"bus.connect(\"message\", bus_call, loop)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Lets add probe to get informed of the meta data generated, we add probe to\n", | |
"# the sink pad of the osd element, since by that time, the buffer would have\n", | |
"# had got all the metadata.\n", | |
"# osdsinkpad = nvosd.get_static_pad(\"sink\")\n", | |
"# if not osdsinkpad:\n", | |
"# print(\" Unable to get sink pad of nvosd \\n\")\n", | |
"\n", | |
"# osdsinkpad.add_probe(Gst.PadProbeType.BUFFER, osd_sink_pad_buffer_probe, 0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Starting pipeline \n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"# start play back and listen to events\n", | |
"print(\"Starting pipeline \\n\")\n", | |
"pipeline.set_state(Gst.State.PLAYING)\n", | |
"try:\n", | |
" loop.run()\n", | |
"except Exception as e:\n", | |
" print(e)\n", | |
"# cleanup\n", | |
"pipeline.set_state(Gst.State.NULL)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# from os.path import exists\n", | |
"\n", | |
"# file_exists = exists(path_to_file)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.8.10" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment