NVIDIA DeepStream SDK API Reference

9.1 Release
service-maker/sources/modules/add_message_meta_probe/add_message_meta_probe.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <iostream>
19 #include <string>
20 #include <memory>
21 #include <vector>
22 #include <fstream>
23 #include <algorithm> // for std::transform
24 #include <cctype> // for std::tolower
25 
26 #include "buffer_probe.hpp"
27 #include "source_config.hpp"
28 
29 namespace deepstream {
30 
31  static std::vector<std::string> parse_labels(const std::string& filename) {
32  std::ifstream file(filename); // Open the file
33  std::vector<std::string> lines; // Vector to store lines
34 
35  if (file.is_open()) { // Check if the file is open
36  std::string line;
37  while (std::getline(file, line)) { // Read each line
38  std::string result=line;
39  std::transform(line.begin(), line.end(), result.begin(), [](unsigned char c){ return std::tolower(c); });
40  lines.push_back(result); // Store the line in the vector
41  }
42  file.close(); // Close the file
43  } else {
44  std::cerr << "Unable to open file: " << filename << std::endl;
45  }
46 
47  return lines;
48  }
49 
51 public:
53  virtual ~MsgMetaGenerator() {}
54 
56  // Initialize only once
57  if (!initialized_) {
58  probe.getProperty("frame-interval", frame_interval);
59  std::string source_config;
60  probe.getProperty("source-config", source_config);
61  std::string label_file;
62  probe.getProperty("label-file", label_file);
63 
64  if (!source_config.empty()) {
65  SourceConfig source_config_obj(source_config);
66  for (size_t i = 0; i < source_config_obj.nSources(); i++) {
67  // multiple sources added
68  std::string src_name = "src_";
69  src_name += std::to_string(i);
70  std::string uri = source_config_obj.getSensorInfo(i).uri;
71  std::string sensor_id = source_config_obj.getSensorInfo(i).sensor_id;
72  sensor_map_[i] = SensorInfo{uri, sensor_id, src_name};
73  }
74  }
75 
76  if (!label_file.empty()) {
77  labels_ = parse_labels(label_file);
78  }
79 
80  initialized_ = true;
81  }
82 
83  FrameMetadata::Iterator frame_itr;
84  for (data.initiateIterator(frame_itr); !frame_itr->done(); frame_itr->next())
85  {
87  for ((*frame_itr)->initiateIterator(obj_itr); !obj_itr->done(); obj_itr->next()) {
88  if (frames_ % frame_interval == 0)
89  {
90  EventMessageUserMetadata event_user_meta;
91  if (data.acquire(event_user_meta)) {
92  if(sensor_map_.empty()) {
93  event_user_meta.generate(**obj_itr, **frame_itr, "N/A", "N/A", labels_);
94  (*frame_itr)->append(event_user_meta);
95  } else {
96  auto source_id = (*frame_itr)->sourceId();
97  auto itr = sensor_map_.find(source_id);
98  if (itr != sensor_map_.end()) {
99  const std::string sensor = itr->second.sensor_id;
100  const std::string uri = itr->second.uri;
101  event_user_meta.generate(**obj_itr, **frame_itr, sensor, uri, labels_);
102  (*frame_itr)->append(event_user_meta);
103  }
104  }
105  }
106  }
107  }
108  frames_++;
109  }
110 
111  return probeReturn::Probe_Ok;
112  }
113 
114  protected:
115  int frames_ = 0;
116  int frame_interval = 1;
117  std::map<uint32_t, SensorInfo> sensor_map_;
118  std::vector<std::string> labels_;
120 };
121 
122 }
deepstream::probeReturn::Probe_Ok
@ Probe_Ok
Nothing abnormal, the buffer will be treated as usual.
deepstream::MsgMetaGenerator
Definition: service-maker/sources/modules/add_message_meta_probe/add_message_meta_probe.hpp:50
deepstream::ObjectMetadata::Iterator
std::unique_ptr< AbstractIterator< ObjectMetadata > > Iterator
Definition: service-maker/includes/metadata.hpp:184
deepstream::SensorInfo::uri
std::string uri
Definition: service-maker/includes/source_config.hpp:36
deepstream::SourceConfig::getSensorInfo
SensorInfo getSensorInfo(uint32_t index) const
Get sensor information for a specific source.
deepstream::BufferProbe
Represent a custom object for the purpose of probing output buffers.
Definition: service-maker/includes/buffer_probe.hpp:63
deepstream::BufferProbe::IBatchMetadataOperator
Read/write interface for handling batch metadata.
Definition: service-maker/includes/buffer_probe.hpp:116
deepstream::MsgMetaGenerator::~MsgMetaGenerator
virtual ~MsgMetaGenerator()
Definition: service-maker/sources/modules/add_message_meta_probe/add_message_meta_probe.hpp:53
deepstream::MsgMetaGenerator::frames_
int frames_
Definition: service-maker/sources/modules/add_message_meta_probe/add_message_meta_probe.hpp:115
deepstream::MsgMetaGenerator::frame_interval
int frame_interval
Definition: service-maker/sources/modules/add_message_meta_probe/add_message_meta_probe.hpp:116
deepstream::parse_labels
static std::vector< std::string > parse_labels(const std::string &filename)
Definition: service-maker/sources/modules/add_message_meta_probe/add_message_meta_probe.hpp:31
deepstream::MsgMetaGenerator::labels_
std::vector< std::string > labels_
Definition: service-maker/sources/modules/add_message_meta_probe/add_message_meta_probe.hpp:118
deepstream::EventMessageUserMetadata
User metadata for event message.
Definition: service-maker/includes/metadata.hpp:253
deepstream::probeReturn
probeReturn
Return values from user implemented probe interfaces.
Definition: service-maker/includes/buffer_probe.hpp:47
deepstream::MsgMetaGenerator::MsgMetaGenerator
MsgMetaGenerator()
Definition: service-maker/sources/modules/add_message_meta_probe/add_message_meta_probe.hpp:52
deepstream::MsgMetaGenerator::handleData
virtual probeReturn handleData(BufferProbe &probe, BatchMetadata &data)
Definition: service-maker/sources/modules/add_message_meta_probe/add_message_meta_probe.hpp:55
deepstream
Definition: service-maker/includes/buffer.hpp:38
deepstream::BatchMetadata::initiateIterator
void initiateIterator(FrameMetadata::Iterator &) const
Get the iterator for frame metadata within it.
deepstream::BatchMetadata
Holds information about a formed batch containingframes from different sources.
Definition: service-maker/includes/metadata.hpp:797
deepstream::MsgMetaGenerator::initialized_
bool initialized_
Definition: service-maker/sources/modules/add_message_meta_probe/add_message_meta_probe.hpp:119
deepstream::SourceConfig::nSources
uint32_t nSources() const
Get the number of the sources.
deepstream::FrameMetadata::Iterator
std::unique_ptr< AbstractIterator< FrameMetadata > > Iterator
Definition: service-maker/includes/metadata.hpp:456
deepstream::SensorInfo::sensor_id
std::string sensor_id
Definition: service-maker/includes/source_config.hpp:37
deepstream::EventMessageUserMetadata::generate
void generate(const ObjectMetadata &, const FrameMetadata &, const std::string sensor="N/A", const std::string uri="N/A", const std::vector< std::string >labels=std::vector< std::string >())
generate an event by attaching an object
deepstream::SensorInfo
Definition: service-maker/includes/source_config.hpp:34
deepstream::SourceConfig
Definition: service-maker/includes/source_config.hpp:61
deepstream::BatchMetadata::acquire
bool acquire(DisplayMetadata &)
Initialize an empty display metadata.
deepstream::Object::getProperty
Object & getProperty(const std::string &name, T &value, Args &... args)
Template for getting multiple properties.
Definition: service-maker/includes/object.hpp:176
deepstream::MsgMetaGenerator::sensor_map_
std::map< uint32_t, SensorInfo > sensor_map_
Definition: service-maker/sources/modules/add_message_meta_probe/add_message_meta_probe.hpp:117