def getDeathcert(self): # Create the node death payload deathPayload = sparkplug.getNodeDeathPayload() #print(deathPayload) print("Node Death certificates issued") return deathPayload
def loop_start(self): # Prepare last will self.logger.debug("set last will message") deathPayload = sparkplug.getNodeDeathPayload() deathByteArray = bytearray(deathPayload.SerializeToString()) self._client.will_set( "spBv1.0/" + self.group_id + "/NDEATH/" + self.node_id, deathByteArray, 0, False) self._client.connect(self._broker_ip, self._broker_port, self._broker_timeout) time.sleep(.1) self._client.loop() self.publishBirth() self.publishBirth_devices()
addMetric(payload, "button", None, MetricDataType.Boolean, pibrella.button.read()) addMetric(payload, "buzzer_fail", None, MetricDataType.Boolean, 0) addMetric(payload, "buzzer_success", None, MetricDataType.Boolean, 0) # Publish the initial data with the DBIRTH certificate totalByteArray = bytearray(payload.SerializeToString()) client.publish( "spBv1.0/" + myGroupId + "/DBIRTH/" + myNodeName + "/" + mySubNodeName, totalByteArray, 0, False) ###################################################################### # Create the NDEATH payload deathPayload = sparkplug.getNodeDeathPayload() # Start of main program - Set up the MQTT client connection client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message client.username_pw_set(myUsername, myPassword) deathByteArray = bytearray(deathPayload.SerializeToString()) client.will_set("spBv1.0/" + myGroupId + "/NDEATH/" + myNodeName, deathByteArray, 0, False) client.connect(serverUrl, 1883, 60) # Short delay to allow connect callback to occur time.sleep(.1) client.loop()
def main(args): # Check input arguments if len(args) != 2: sys.stderr.write("usage: %s <v4l2-device-path>\n" % args[0]) sys.exit(1) # Standard GStreamer initialization GObject.threads_init() 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") ######################################### # Source element for reading from the file print("Creating Source \n ") source = Gst.ElementFactory.make("v4l2src", "usb-cam-source") if not source: sys.stderr.write(" Unable to create Source \n") caps_v4l2src = Gst.ElementFactory.make("capsfilter", "v4l2src_caps") if not caps_v4l2src: sys.stderr.write(" Unable to create v4l2src capsfilter \n") print("Creating Video Converter \n") # Adding videoconvert -> nvvideoconvert as not all # raw formats are supported by nvvideoconvert; # Say YUYV is unsupported - which is the common # raw format for many logi usb cams # In case we have a camera with raw format supported in # nvvideoconvert, GStreamer plugins' capability negotiation # shall be intelligent enough to reduce compute by # videoconvert doing passthrough (TODO we need to confirm this) # videoconvert to make sure a superset of raw formats are supported vidconvsrc = Gst.ElementFactory.make("videoconvert", "convertor_src1") if not vidconvsrc: sys.stderr.write(" Unable to create videoconvert \n") # nvvideoconvert to convert incoming raw buffers to NVMM Mem (NvBufSurface API) nvvidconvsrc = Gst.ElementFactory.make("nvvideoconvert", "convertor_src2") if not nvvidconvsrc: sys.stderr.write(" Unable to create Nvvideoconvert \n") caps_vidconvsrc = Gst.ElementFactory.make("capsfilter", "nvmm_caps") if not caps_vidconvsrc: sys.stderr.write(" Unable to create capsfilter \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") # Use nvinfer to run inferencing on camera's output, # behaviour of inferencing is set through config file pgie = Gst.ElementFactory.make("nvinfer", "primary-inference") if not pgie: sys.stderr.write(" Unable to create pgie \n") # Use convertor to convert from NV12 to RGBA as required by nvosd nvvidconv = Gst.ElementFactory.make("nvvideoconvert", "convertor") if not nvvidconv: sys.stderr.write(" Unable to create nvvidconv \n") # Create OSD to draw on the converted RGBA buffer nvosd = Gst.ElementFactory.make("nvdsosd", "onscreendisplay") ########################################### if not nvosd: sys.stderr.write(" Unable to create nvosd \n") # Finally render the osd output #if is_aarch64(): #transform = Gst.ElementFactory.make("nvegltransform", "nvegl-transform") nvvidconv_postosd = Gst.ElementFactory.make("nvvideoconvert", "convertor_postosd") if not nvvidconv_postosd: sys.stderr.write(" Unable to create nvvidconv_postosd \n") # Create a caps filter caps = Gst.ElementFactory.make("capsfilter", "filter") caps.set_property( "caps", Gst.Caps.from_string("video/x-raw(memory:NVMM), format=I420")) # Make the encoder bitrate = 4000000 codec = "H264" if codec == "H264": encoder = Gst.ElementFactory.make("nvv4l2h264enc", "encoder") print("Creating H264 Encoder") elif codec == "H265": encoder = Gst.ElementFactory.make("nvv4l2h265enc", "encoder") print("Creating H265 Encoder") if not encoder: sys.stderr.write(" Unable to create encoder") encoder.set_property('bitrate', bitrate) if is_aarch64(): encoder.set_property('preset-level', 1) encoder.set_property('insert-sps-pps', 1) encoder.set_property('bufapi-version', 1) # Make the payload-encode video into RTP packets if codec == "H264": rtppay = Gst.ElementFactory.make("rtph264pay", "rtppay") print("Creating H264 rtppay") elif codec == "H265": rtppay = Gst.ElementFactory.make("rtph265pay", "rtppay") print("Creating H265 rtppay") if not rtppay: sys.stderr.write(" Unable to create rtppay") # Make the UDP sink updsink_port_num = 5400 sink = Gst.ElementFactory.make("udpsink", "udpsink") if not sink: sys.stderr.write(" Unable to create udpsink") sink.set_property('host', '224.224.255.255') sink.set_property('port', updsink_port_num) sink.set_property('async', False) sink.set_property('sync', 0) print("Playing cam %s " % args[1]) caps_v4l2src.set_property( 'caps', Gst.Caps.from_string("video/x-raw, framerate=30/1")) caps_vidconvsrc.set_property( 'caps', Gst.Caps.from_string("video/x-raw(memory:NVMM)")) source.set_property('device', args[1]) streammux.set_property('gpu-id', 0) streammux.set_property('live-source', 1) streammux.set_property('enable-padding', 0) streammux.set_property('nvbuf-memory-type', 0) streammux.set_property('width', 640) streammux.set_property('height', 480) streammux.set_property('batch-size', 1) streammux.set_property('batched-push-timeout', 40000) pgie.set_property('config-file-path', "dstest1_pgie_config.txt") # Set sync = false to avoid late frame drops at the display-sink #sink.set_property('sync', False) print("Adding elements to Pipeline \n") pipeline.add(source) pipeline.add(caps_v4l2src) pipeline.add(vidconvsrc) pipeline.add(nvvidconvsrc) pipeline.add(caps_vidconvsrc) pipeline.add(streammux) pipeline.add(pgie) pipeline.add(nvvidconv) pipeline.add(nvosd) pipeline.add(nvvidconv_postosd) pipeline.add(caps) pipeline.add(encoder) pipeline.add(rtppay) pipeline.add(sink) #if is_aarch64(): #pipeline.add(transform) # we link the elements together # v4l2src -> nvvideoconvert -> mux -> # nvinfer -> nvvideoconvert -> nvosd -> video-renderer print("Linking elements in the Pipeline \n") source.link(caps_v4l2src) caps_v4l2src.link(vidconvsrc) vidconvsrc.link(nvvidconvsrc) nvvidconvsrc.link(caps_vidconvsrc) sinkpad = streammux.get_request_pad("sink_0") if not sinkpad: sys.stderr.write(" Unable to get the sink pad of streammux \n") srcpad = caps_vidconvsrc.get_static_pad("src") if not srcpad: sys.stderr.write(" Unable to get source pad of caps_vidconvsrc \n") srcpad.link(sinkpad) streammux.link(pgie) pgie.link(nvvidconv) nvvidconv.link(nvosd) nvosd.link(nvvidconv_postosd) nvvidconv_postosd.link(caps) caps.link(encoder) encoder.link(rtppay) rtppay.link(sink) # create an event loop and feed gstreamer bus mesages to it loop = GObject.MainLoop() bus = pipeline.get_bus() bus.add_signal_watch() bus.connect("message", bus_call, loop) ########################################### # Start streaming rtsp_port_num = 8554 server = GstRtspServer.RTSPServer.new() server.props.service = "%d" % rtsp_port_num server.attach(None) factory = GstRtspServer.RTSPMediaFactory.new() factory.set_launch( "( udpsrc name=pay0 port=%d buffer-size=524288 caps=\"application/x-rtp, media=video, clock-rate=90000, encoding-name=(string)%s, payload=96 \" )" % (updsink_port_num, codec)) factory.set_shared(True) server.get_mount_points().add_factory("/ds-test", factory) print( "\n *** DeepStream: Launched RTSP Streaming at rtsp://localhost:%d/ds-test ***\n\n" % rtsp_port_num) # Lets add probe to get informed of the meta data generated, we add probe to # the sink pad of the osd element, since by that time, the buffer would have # had got all the metadata. 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) ###################################################################### # Create the node death payload deathPayload = sparkplug.getNodeDeathPayload() # Start of main program - Set up the MQTT client connection client.on_connect = on_connect client.on_message = on_message client.username_pw_set(myUsername, myPassword) deathByteArray = bytearray(deathPayload.SerializeToString()) client.will_set("spBv1.0/" + myGroupId + "/NDEATH/" + myNodeName, deathByteArray, 0, False) client.connect(serverUrl, 1883, 60) # Publish the birth certificates publishBirth() def foo(): # Periodically publish some new data payload = sparkplug.getDdataPayload() # Add some random data to the inputs addMetric(payload, "input/Frame Number", AliasMap.Device_frame_numberx, MetricDataType.Int16, frame_numberx) addMetric(payload, "input/number of objects", AliasMap.Device_num_rectsx, MetricDataType.Int16, num_rectsx) addMetric(payload, "input/Vehicle count", AliasMap.Device_counter1, MetricDataType.Int16, counter1) addMetric(payload, "input/Person count", AliasMap.Device_counter2, MetricDataType.Int16, counter2) # Note this data we're setting to STALE via the propertyset as an example metric = addMetric(payload, None, AliasMap.Device_Metric1, MetricDataType.Boolean, random.choice([True, False])) metric.properties.keys.extend(["Quality"]) propertyValue = metric.properties.values.add() propertyValue.type = ParameterDataType.Int32 propertyValue.int_value = 500 # Publish a message data byteArray = bytearray(payload.SerializeToString()) client.publish( "spBv1.0/" + myGroupId + "/DDATA/" + myNodeName + "/" + myDeviceName, byteArray, 0, False) threading.Timer(WAIT_SECONDS, foo).start() foo() ###################################################################### # start play back and listen to events print("Starting pipeline \n") pipeline.set_state(Gst.State.PLAYING) try: loop.run() except: pass # cleanup pipeline.set_state(Gst.State.NULL)
def main(args): # Standard GStreamer initialization GObject.threads_init() 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") ############################################ # Source element for reading from the file print("Creating Source \n ") source = Gst.ElementFactory.make("filesrc", "file-source") if not source: sys.stderr.write(" Unable to create Source \n") # Since the data format in the input file is elementary h264 stream, # we need a h264parser print("Creating H264Parser \n") h264parser = Gst.ElementFactory.make("h264parse", "h264-parser") if not h264parser: sys.stderr.write(" Unable to create h264 parser \n") # Use nvdec_h264 for hardware accelerated decode on GPU print("Creating Decoder \n") decoder = Gst.ElementFactory.make("nvv4l2decoder", "nvv4l2-decoder") if not decoder: sys.stderr.write(" Unable to create Nvv4l2 Decoder \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") # Use nvinfer to run inferencing on decoder's output, # behaviour of inferencing is set through config file pgie = Gst.ElementFactory.make("nvinfer", "primary-inference") if not pgie: sys.stderr.write(" Unable to create pgie \n") # Use convertor to convert from NV12 to RGBA as required by nvosd nvvidconv = Gst.ElementFactory.make("nvvideoconvert", "convertor") if not nvvidconv: sys.stderr.write(" Unable to create nvvidconv \n") # Create OSD to draw on the converted RGBA buffer nvosd = Gst.ElementFactory.make("nvdsosd", "onscreendisplay") ########################################### if not nvosd: sys.stderr.write(" Unable to create nvosd \n") nvvidconv_postosd = Gst.ElementFactory.make("nvvideoconvert", "convertor_postosd") if not nvvidconv_postosd: sys.stderr.write(" Unable to create nvvidconv_postosd \n") # Create a caps filter caps = Gst.ElementFactory.make("capsfilter", "filter") caps.set_property( "caps", Gst.Caps.from_string("video/x-raw(memory:NVMM), format=I420")) # Make the encoder if codec == "H264": encoder = Gst.ElementFactory.make("nvv4l2h264enc", "encoder") print("Creating H264 Encoder") elif codec == "H265": encoder = Gst.ElementFactory.make("nvv4l2h265enc", "encoder") print("Creating H265 Encoder") if not encoder: sys.stderr.write(" Unable to create encoder") encoder.set_property('bitrate', bitrate) if is_aarch64(): encoder.set_property('preset-level', 1) encoder.set_property('insert-sps-pps', 1) encoder.set_property('bufapi-version', 1) # Make the payload-encode video into RTP packets if codec == "H264": rtppay = Gst.ElementFactory.make("rtph264pay", "rtppay") print("Creating H264 rtppay") elif codec == "H265": rtppay = Gst.ElementFactory.make("rtph265pay", "rtppay") print("Creating H265 rtppay") if not rtppay: sys.stderr.write(" Unable to create rtppay") # Make the UDP sink updsink_port_num = 5400 sink = Gst.ElementFactory.make("udpsink", "udpsink") if not sink: sys.stderr.write(" Unable to create udpsink") sink.set_property('host', '224.224.255.255') sink.set_property('port', updsink_port_num) sink.set_property('async', False) sink.set_property('sync', 1) print("Playing file %s " % stream_path) source.set_property('location', stream_path) streammux.set_property('width', 1920) streammux.set_property('height', 1080) streammux.set_property('batch-size', 1) streammux.set_property('batched-push-timeout', 4000000) pgie.set_property('config-file-path', "dstest1_pgie_config.txt") print("Adding elements to Pipeline \n") pipeline.add(source) pipeline.add(h264parser) pipeline.add(decoder) pipeline.add(streammux) pipeline.add(pgie) pipeline.add(nvvidconv) pipeline.add(nvosd) pipeline.add(nvvidconv_postosd) pipeline.add(caps) pipeline.add(encoder) pipeline.add(rtppay) pipeline.add(sink) # Link the elements together: # file-source -> h264-parser -> nvh264-decoder -> # nvinfer -> nvvidconv -> nvosd -> nvvidconv_postosd -> # caps -> encoder -> rtppay -> udpsink print("Linking elements in the Pipeline \n") source.link(h264parser) h264parser.link(decoder) sinkpad = streammux.get_request_pad("sink_0") if not sinkpad: sys.stderr.write(" Unable to get the sink pad of streammux \n") srcpad = decoder.get_static_pad("src") if not srcpad: sys.stderr.write(" Unable to get source pad of decoder \n") srcpad.link(sinkpad) streammux.link(pgie) pgie.link(nvvidconv) nvvidconv.link(nvosd) nvosd.link(nvvidconv_postosd) nvvidconv_postosd.link(caps) caps.link(encoder) encoder.link(rtppay) rtppay.link(sink) # create an event loop and feed gstreamer bus mesages to it loop = GObject.MainLoop() bus = pipeline.get_bus() bus.add_signal_watch() bus.connect("message", bus_call, loop) # Start streaming rtsp_port_num = 8554 server = GstRtspServer.RTSPServer.new() server.props.service = "%d" % rtsp_port_num server.attach(None) factory = GstRtspServer.RTSPMediaFactory.new() factory.set_launch( "( udpsrc name=pay0 port=%d buffer-size=524288 caps=\"application/x-rtp, media=video, clock-rate=90000, encoding-name=(string)%s, payload=96 \" )" % (updsink_port_num, codec)) factory.set_shared(True) server.get_mount_points().add_factory("/ds-test", factory) print( "\n *** DeepStream: Launched RTSP Streaming at rtsp://localhost:%d/ds-test ***\n\n" % rtsp_port_num) ########################################### # Lets add probe to get informed of the meta data generated, we add probe to # the sink pad of the osd element, since by that time, the buffer would have # had got all the metadata. 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) ###################################################################### # Create the node death payload deathPayload = sparkplug.getNodeDeathPayload() # Start of main program - Set up the MQTT client connection client.on_connect = on_connect client.on_message = on_message client.username_pw_set(myUsername, myPassword) deathByteArray = bytearray(deathPayload.SerializeToString()) client.will_set("spBv1.0/" + myGroupId + "/NDEATH/" + myNodeName, deathByteArray, 0, False) client.connect(serverUrl, 1883, 60) # Publish the birth certificates publishBirth() def foo(): # Periodically publish some new data payload = sparkplug.getDdataPayload() # Add some random data to the inputs addMetric(payload, "input/Frame Number", AliasMap.Device_frame_numberx, MetricDataType.Int16, frame_numberx) addMetric(payload, "input/number of objects", AliasMap.Device_num_rectsx, MetricDataType.Int16, num_rectsx) addMetric(payload, "input/Vehicle count", AliasMap.Device_counter1, MetricDataType.Int16, counter1) addMetric(payload, "input/Person count", AliasMap.Device_counter2, MetricDataType.Int16, counter2) # Note this data we're setting to STALE via the propertyset as an example metric = addMetric(payload, None, AliasMap.Device_Metric1, MetricDataType.Boolean, random.choice([True, False])) metric.properties.keys.extend(["Quality"]) propertyValue = metric.properties.values.add() propertyValue.type = ParameterDataType.Int32 propertyValue.int_value = 500 # Publish a message data byteArray = bytearray(payload.SerializeToString()) client.publish( "spBv1.0/" + myGroupId + "/DDATA/" + myNodeName + "/" + myDeviceName, byteArray, 0, False) threading.Timer(WAIT_SECONDS, foo).start() foo() ###################################################################### # start play back and listen to events print("Starting pipeline \n") pipeline.set_state(Gst.State.PLAYING) try: loop.run() except: pass # cleanup pipeline.set_state(Gst.State.NULL)
addMetric(payload, "Outputs/g", None, MetricDataType.Boolean, pibrella.output.g.read()) addMetric(payload, "Outputs/h", None, MetricDataType.Boolean, pibrella.output.h.read()) addMetric(payload, "Outputs/LEDs/green", None, MetricDataType.Boolean, pibrella.light.green.read()) addMetric(payload, "Outputs/LEDs/red", None, MetricDataType.Boolean, pibrella.light.red.read()) addMetric(payload, "Outputs/LEDs/yellow", None, MetricDataType.Boolean, pibrella.light.yellow.read()) addMetric(payload, "button", None, MetricDataType.Boolean, pibrella.button.read()) addMetric(payload, "buzzer_fail", None, MetricDataType.Boolean, 0) addMetric(payload, "buzzer_success", None, MetricDataType.Boolean, 0) # Publish the initial data with the DBIRTH certificate totalByteArray = bytearray(payload.SerializeToString()) client.publish("spBv1.0/" + myGroupId + "/DBIRTH/" + myNodeName + "/" + mySubNodeName, totalByteArray, 0, False) ###################################################################### # Create the NDEATH payload deathPayload = sparkplug.getNodeDeathPayload() # Start of main program - Set up the MQTT client connection client = mqtt.Client(serverUrl, 1883, 60) client.on_connect = on_connect client.on_message = on_message client.username_pw_set(myUsername, myPassword) deathByteArray = bytearray(deathPayload.SerializeToString()) client.will_set("spBv1.0/" + myGroupId + "/NDEATH/" + myNodeName, deathByteArray, 0, False) client.connect(serverUrl, 1883, 60) # Short delay to allow connect callback to occur time.sleep(.1) client.loop() publishBirths()
def main(args): # Check input arguments if len(args) != 2: sys.stderr.write("usage: %s <v4l2-device-path>\n" % args[0]) sys.exit(1) # Standard GStreamer initialization GObject.threads_init() 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") # Source element for reading from the file print("Creating Source \n ") source = Gst.ElementFactory.make("v4l2src", "usb-cam-source") if not source: sys.stderr.write(" Unable to create Source \n") caps_v4l2src = Gst.ElementFactory.make("capsfilter", "v4l2src_caps") if not caps_v4l2src: sys.stderr.write(" Unable to create v4l2src capsfilter \n") print("Creating Video Converter \n") # Adding videoconvert -> nvvideoconvert as not all # raw formats are supported by nvvideoconvert; # Say YUYV is unsupported - which is the common # raw format for many logi usb cams # In case we have a camera with raw format supported in # nvvideoconvert, GStreamer plugins' capability negotiation # shall be intelligent enough to reduce compute by # videoconvert doing passthrough (TODO we need to confirm this) # videoconvert to make sure a superset of raw formats are supported vidconvsrc = Gst.ElementFactory.make("videoconvert", "convertor_src1") if not vidconvsrc: sys.stderr.write(" Unable to create videoconvert \n") # nvvideoconvert to convert incoming raw buffers to NVMM Mem (NvBufSurface API) nvvidconvsrc = Gst.ElementFactory.make("nvvideoconvert", "convertor_src2") if not nvvidconvsrc: sys.stderr.write(" Unable to create Nvvideoconvert \n") caps_vidconvsrc = Gst.ElementFactory.make("capsfilter", "nvmm_caps") if not caps_vidconvsrc: sys.stderr.write(" Unable to create capsfilter \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") # Use nvinfer to run inferencing on camera's output, # behaviour of inferencing is set through config file pgie = Gst.ElementFactory.make("nvinfer", "primary-inference") if not pgie: sys.stderr.write(" Unable to create pgie \n") # Use convertor to convert from NV12 to RGBA as required by nvosd nvvidconv = Gst.ElementFactory.make("nvvideoconvert", "convertor") if not nvvidconv: sys.stderr.write(" Unable to create nvvidconv \n") # Create OSD to draw on the converted RGBA buffer nvosd = Gst.ElementFactory.make("nvdsosd", "onscreendisplay") if not nvosd: sys.stderr.write(" Unable to create nvosd \n") # Finally render the osd output if is_aarch64(): transform = Gst.ElementFactory.make("nvegltransform", "nvegl-transform") print("Creating EGLSink \n") sink = Gst.ElementFactory.make("nveglglessink", "nvvideo-renderer") if not sink: sys.stderr.write(" Unable to create egl sink \n") print("Playing cam %s " % args[1]) caps_v4l2src.set_property( 'caps', Gst.Caps.from_string("video/x-raw, framerate=30/1")) caps_vidconvsrc.set_property( 'caps', Gst.Caps.from_string("video/x-raw(memory:NVMM)")) source.set_property('device', args[1]) streammux.set_property('width', 640) streammux.set_property('height', 480) streammux.set_property('batch-size', 1) streammux.set_property('batched-push-timeout', 4000000) pgie.set_property('config-file-path', "config_infer_primary_yoloV3.txt") # Set sync = false to avoid late frame drops at the display-sink sink.set_property('sync', False) print("Adding elements to Pipeline \n") pipeline.add(source) pipeline.add(caps_v4l2src) pipeline.add(vidconvsrc) pipeline.add(nvvidconvsrc) pipeline.add(caps_vidconvsrc) pipeline.add(streammux) pipeline.add(pgie) pipeline.add(nvvidconv) pipeline.add(nvosd) pipeline.add(sink) if is_aarch64(): pipeline.add(transform) # we link the elements together # v4l2src -> nvvideoconvert -> mux -> # nvinfer -> nvvideoconvert -> nvosd -> video-renderer print("Linking elements in the Pipeline \n") source.link(caps_v4l2src) caps_v4l2src.link(vidconvsrc) vidconvsrc.link(nvvidconvsrc) nvvidconvsrc.link(caps_vidconvsrc) sinkpad = streammux.get_request_pad("sink_0") if not sinkpad: sys.stderr.write(" Unable to get the sink pad of streammux \n") srcpad = caps_vidconvsrc.get_static_pad("src") if not srcpad: sys.stderr.write(" Unable to get source pad of caps_vidconvsrc \n") srcpad.link(sinkpad) streammux.link(pgie) pgie.link(nvvidconv) nvvidconv.link(nvosd) if is_aarch64(): nvosd.link(transform) transform.link(sink) else: nvosd.link(sink) # create an event loop and feed gstreamer bus mesages to it loop = GObject.MainLoop() bus = pipeline.get_bus() bus.add_signal_watch() bus.connect("message", bus_call, loop) # Lets add probe to get informed of the meta data generated, we add probe to # the sink pad of the osd element, since by that time, the buffer would have # had got all the metadata. 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) ###################################################################### # Create the node death payload deathPayload = sparkplug.getNodeDeathPayload() # Start of main program - Set up the MQTT client connection client.on_connect = on_connect client.on_message = on_message client.username_pw_set(myUsername, myPassword) deathByteArray = bytearray(deathPayload.SerializeToString()) client.will_set("spBv1.0/" + myGroupId + "/NDEATH/" + myNodeName, deathByteArray, 0, False) client.connect(serverUrl, 1883, 60) # Publish the birth certificates publishBirth() def foo(): # Periodically publish some new data payload = sparkplug.getDdataPayload() # Add some random data to the inputs addMetric(payload, "input/number of objects", AliasMap.Device_num_rectsx, MetricDataType.Int16, num_rectsx) addMetric(payload, "input/Frame Number", AliasMap.Device_frame_numberx, MetricDataType.Int16, frame_numberx) addMetric(payload, "input/Device Output1", AliasMap.Device_Output1, MetricDataType.Int16, Object1) addMetric(payload, "input/Device Output2", AliasMap.Device_Output2, MetricDataType.Int16, Object2) addMetric(payload, "input/Device Output3", AliasMap.Device_Output3, MetricDataType.Int16, Object3) addMetric(payload, "input/Device Output4", AliasMap.Device_Output4, MetricDataType.Int16, Object4) addMetric(payload, "input/Device Output5", AliasMap.Device_Output5, MetricDataType.Int16, Object5) addMetric(payload, "input/Device Output6", AliasMap.Device_Output6, MetricDataType.Int16, Object6) addMetric(payload, "input/Device Output7", AliasMap.Device_Output7, MetricDataType.Int16, Object7) addMetric(payload, "input/Device Output8", AliasMap.Device_Output8, MetricDataType.Int16, Object8) addMetric(payload, "input/Device Output9", AliasMap.Device_Output9, MetricDataType.Int16, Object9) addMetric(payload, "input/Device Output10", AliasMap.Device_Output10, MetricDataType.Int16, Object10) # Publish a message data byteArray = bytearray(payload.SerializeToString()) client.publish( "spBv1.0/" + myGroupId + "/DDATA/" + myNodeName + "/" + myDeviceName, byteArray, 0, False) # Sit and wait for inbound or outbound events for _ in range(1): time.sleep(1) client.loop() threading.Timer(WAIT_SECONDS, foo).start() foo() ###################################################################### print("Starting pipeline \n") pipeline.set_state(Gst.State.PLAYING) try: loop.run() except: pass #cleanup print("Exiting app\n") pipeline.set_state(Gst.State.NULL)
def main(args): # Check input arguments if len(args) < 2: sys.stderr.write("usage: %s <uri1> [uri2] ... [uriN]\n" % args[0]) sys.exit(1) for i in range(0, len(args) - 1): fps_streams["stream{0}".format(i)] = GETFPS(i) number_sources = len(args) - 1 # Standard GStreamer initialization GObject.threads_init() Gst.init(None) # Create gstreamer elements */ # Create Pipeline element that will form a connection of other elements print("Creating Pipeline \n ") pipeline = Gst.Pipeline() is_live = False if not pipeline: sys.stderr.write(" Unable to create Pipeline \n") print("Creating streamux \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") pipeline.add(streammux) for i in range(number_sources): print("Creating source_bin ", i, " \n ") uri_name = args[i + 1] if uri_name.find("rtsp://") == 0: is_live = True source_bin = create_source_bin(i, uri_name) if not source_bin: sys.stderr.write("Unable to create source bin \n") pipeline.add(source_bin) padname = "sink_%u" % i sinkpad = streammux.get_request_pad(padname) if not sinkpad: sys.stderr.write("Unable to create sink pad bin \n") srcpad = source_bin.get_static_pad("src") if not srcpad: sys.stderr.write("Unable to create src pad bin \n") srcpad.link(sinkpad) queue1 = Gst.ElementFactory.make("queue", "queue1") queue2 = Gst.ElementFactory.make("queue", "queue2") queue3 = Gst.ElementFactory.make("queue", "queue3") queue4 = Gst.ElementFactory.make("queue", "queue4") queue5 = Gst.ElementFactory.make("queue", "queue5") pipeline.add(queue1) pipeline.add(queue2) pipeline.add(queue3) pipeline.add(queue4) pipeline.add(queue5) print("Creating Pgie \n ") pgie = Gst.ElementFactory.make("nvinfer", "primary-inference") if not pgie: sys.stderr.write(" Unable to create pgie \n") print("Creating tiler \n ") tiler = Gst.ElementFactory.make("nvmultistreamtiler", "nvtiler") if not tiler: sys.stderr.write(" Unable to create tiler \n") print("Creating nvvidconv \n ") nvvidconv = Gst.ElementFactory.make("nvvideoconvert", "convertor") if not nvvidconv: sys.stderr.write(" Unable to create nvvidconv \n") print("Creating nvosd \n ") nvosd = Gst.ElementFactory.make("nvdsosd", "onscreendisplay") ########################################### if not nvosd: sys.stderr.write(" Unable to create nvosd \n") nvosd.set_property('process-mode', OSD_PROCESS_MODE) nvosd.set_property('display-text', OSD_DISPLAY_TEXT) if (is_aarch64()): print("Creating transform \n ") transform = Gst.ElementFactory.make("nvegltransform", "nvegl-transform") if not transform: sys.stderr.write(" Unable to create transform \n") print("Creating EGLSink \n") sink = Gst.ElementFactory.make("nveglglessink", "nvvideo-renderer") if not sink: sys.stderr.write(" Unable to create egl sink \n") if is_live: print("Atleast one of the sources is live") streammux.set_property('live-source', 1) streammux.set_property('gpu-id', 0) streammux.set_property('enable-padding', 0) streammux.set_property('nvbuf-memory-type', 0) streammux.set_property('width', 640) streammux.set_property('height', 480) streammux.set_property('batch-size', 4) streammux.set_property('batched-push-timeout', 40000) pgie.set_property('config-file-path', "config_infer_primary_yoloV3.txt") #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", 4) sink.set_property('sync', False) tiler_rows = int(math.sqrt(number_sources)) tiler_columns = int(math.ceil((1.0 * number_sources) / tiler_rows)) 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) sink.set_property("qos", 0) print("Adding elements to Pipeline \n") pipeline.add(pgie) pipeline.add(tiler) pipeline.add(nvvidconv) pipeline.add(nvosd) if is_aarch64(): pipeline.add(transform) pipeline.add(sink) print("Linking elements in the Pipeline \n") streammux.link(queue1) queue1.link(pgie) pgie.link(queue2) queue2.link(tiler) tiler.link(queue3) queue3.link(nvvidconv) nvvidconv.link(queue4) queue4.link(nvosd) if is_aarch64(): nvosd.link(queue5) queue5.link(transform) transform.link(sink) else: nvosd.link(queue5) queue5.link(sink) # create an event loop and feed gstreamer bus mesages to it loop = GObject.MainLoop() bus = pipeline.get_bus() bus.add_signal_watch() bus.connect("message", bus_call, loop) tiler_src_pad = pgie.get_static_pad("src") if not tiler_src_pad: sys.stderr.write(" Unable to get src pad \n") else: tiler_src_pad.add_probe(Gst.PadProbeType.BUFFER, tiler_src_pad_buffer_probe, 0) # List the sources print("Now playing...") for i, source in enumerate(args): if (i != 0): print(i, ": ", source) ###################################################################### # Create the node death payload deathPayload = sparkplug.getNodeDeathPayload() # Start of main program - Set up the MQTT client connection client.on_connect = on_connect client.on_message = on_message client.username_pw_set(myUsername, myPassword) deathByteArray = bytearray(deathPayload.SerializeToString()) client.will_set("spBv1.0/" + myGroupId + "/NDEATH/" + myNodeName, deathByteArray, 0, False) client.connect(serverUrl, 1883, 60) # Publish the birth certificates publishBirth() def foo(): # Periodically publish some new data payload = sparkplug.getDdataPayload() # Add some random data to the inputs addMetric(payload, "input/number of objects", AliasMap.Device_num_rectsx, MetricDataType.Int16, num_rectsx) addMetric(payload, "input/Frame Number", AliasMap.Device_frame_numberx, MetricDataType.Int16, frame_numberx) addMetric(payload, "input/Device Output1", AliasMap.Device_Output1, MetricDataType.Int16, Object1) addMetric(payload, "input/Device Output2", AliasMap.Device_Output2, MetricDataType.Int16, Object2) addMetric(payload, "input/Device Output3", AliasMap.Device_Output3, MetricDataType.Int16, Object3) addMetric(payload, "input/Device Output4", AliasMap.Device_Output4, MetricDataType.Int16, Object4) addMetric(payload, "input/Device Output5", AliasMap.Device_Output5, MetricDataType.Int16, Object5) addMetric(payload, "input/Device Output6", AliasMap.Device_Output6, MetricDataType.Int16, Object6) addMetric(payload, "input/Device Output7", AliasMap.Device_Output7, MetricDataType.Int16, Object7) addMetric(payload, "input/Device Output8", AliasMap.Device_Output8, MetricDataType.Int16, Object8) addMetric(payload, "input/Device Output9", AliasMap.Device_Output9, MetricDataType.Int16, Object9) addMetric(payload, "input/Device Output10", AliasMap.Device_Output10, MetricDataType.Int16, Object10) # Publish a message data byteArray = bytearray(payload.SerializeToString()) client.publish( "spBv1.0/" + myGroupId + "/DDATA/" + myNodeName + "/" + myDeviceName, byteArray, 0, False) # Sit and wait for inbound or outbound events for _ in range(1): time.sleep(1) client.loop() threading.Timer(WAIT_SECONDS, foo).start() foo() ###################################################################### print("Starting pipeline \n") # start play back and listed to events pipeline.set_state(Gst.State.PLAYING) try: loop.run() except: pass # cleanup print("Exiting app\n") pipeline.set_state(Gst.State.NULL)