NVIDIA DeepStream SDK API Reference

8.0 Release
image_meta_consumer.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: LicenseRef-NvidiaProprietary
4  *
5  * NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
6  * property and proprietary rights in and to this material, related
7  * documentation and any modifications thereto. Any use, reproduction,
8  * disclosure or distribution of this material and related documentation
9  * without an express license agreement from NVIDIA CORPORATION or
10  * its affiliates is strictly prohibited.
11  */
12 
13 #pragma once
14 
15 #include <condition_variable>
16 #include <array>
17 #include <mutex>
18 #include <string>
19 #include <atomic>
20 #include <thread>
21 #include <iostream>
22 #include <fstream>
23 #include <sstream>
24 #include <iomanip>
25 #include <ios>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <deepstream_config.h>
30 #include "gstnvdsmeta.h"
31 #include "nvbufsurface.h"
32 #include "gst-nvmessage.h"
33 #include "nvds_obj_encode.h"
34 #include "concurrent_queue.h"
35 #include "capture_time_rules.h"
36 
38 public:
39  enum OutputType {
40  KITTI = 0,
41  JSON = 1,
42  CSV = 2
43  };
44 
48  };
49 
52 
55 
74  void init(unsigned gpu_id, const std::string &output_folder_path, const std::string &frame_to_skip_rules_path,
75  float min_box_confidence, float max_box_confidence,
76  unsigned min_box_width, unsigned min_box_height,
77  bool save_full_frame_enabled, bool save_cropped_obj_enabled,
78  unsigned seconds_to_skip_interval, unsigned source_nb);
79 
82  void add_meta_csv(const std::string &meta);
83 
86  void add_meta_json(const std::string &meta);
87 
91  void add_meta_kitti(const std::pair<std::string, std::string> &meta);
92 
94  void stop();
95 
96 
99  float get_min_confidence() const;
100 
103  float get_max_confidence() const;
104 
107  unsigned get_gpu_id() const;
108 
111  unsigned get_min_box_width() const;
112 
115  unsigned get_min_box_height() const;
116 
119  bool get_is_stopped() const;
120 
123  bool get_save_full_frame_enabled() const;
124 
127  bool get_save_cropped_images_enabled() const;
128 
132 
138  unsigned stream_source_id,
139  const std::string &datetime_iso8601);
140 
143 
149  bool should_save_data(unsigned source_id);
150 
154  void lock_source_nb(unsigned source_id);
155 
158  void data_was_saved_for_source(unsigned source_id);
159 
162  void unlock_source_nb(unsigned source_id);
163 
164 private:
165 
167  void run();
168 
170  void multi_metadata_maker(ConcurrentQueue<std::pair<std::string, std::string>> &queue,
171  std::mutex &mutex, std::condition_variable &cv);
172 
174  bool setup_files();
175 
177  bool setup_folders();
178 
180  void write_intro(std::ofstream &os, OutputType &ot);
181 
183  void write_mid_separator(std::ofstream &os, OutputType &ot);
184 
186  void write_end(std::ofstream &os, OutputType &ot, unsigned total_nb);
187 
189  unsigned int get_unique_id();
190 
193  void single_metadata_maker(const std::string &extension,
195  std::mutex &mutex, std::condition_variable &cv,
196  OutputType ot);
197 
199  ConcurrentQueue<std::string> queue_csv_;
200  ConcurrentQueue<std::string> queue_json_;
201  std::mutex m_kitti_;
202  std::mutex m_json_;
203  std::mutex m_csv_;
204  std::condition_variable cv_kitti_;
205  std::condition_variable cv_json_;
206  std::condition_variable cv_csv_;
207  std::atomic<bool> is_stopped_;
208  std::string output_folder_path_;
209  std::string images_cropped_obj_output_folder_;
210  std::string images_full_frame_output_folder_;
211  std::string labels_output_folder_;
212  std::thread th_kitti_;
213  std::thread th_json_;
214  std::thread th_csv_;
215  std::mutex mutex_unique_index_;
216  unsigned int unique_index_ = 0;
217  float min_confidence_;
218  float max_confidence_;
219  unsigned gpu_id_;
220  unsigned min_box_width_;
221  unsigned min_box_height_;
222  bool save_full_frame_enabled_;
223  bool save_cropped_obj_enabled_;
224  std::vector<std::chrono::_V2::system_clock::time_point> time_last_frame_saved_list_;
225  std::array<std::mutex, MAX_SOURCE_BINS> mutex_frame_saved_list_;
226  CaptureTimeRules ctr_;
227  NvDsObjEncCtxHandle obj_ctx_handle_;
228  bool image_saving_library_is_init_;
229  std::mutex mutex_image_save_init_;
230 };
ImageMetaConsumer::KITTI
@ KITTI
Definition: image_meta_consumer.h:40
ImageMetaConsumer::stop
void stop()
End the job of the current thread reading from the queue.
ConcurrentQueue
Simple concurrent Queue class using an stl queue.
Definition: concurrent_queue.h:22
nvds_obj_encode.h
ImageMetaConsumer::add_meta_json
void add_meta_json(const std::string &meta)
Add metadata to the stored concurrent queue.
ImageMetaConsumer::ImageMetaConsumer
ImageMetaConsumer()
Init an object and set that the queue is stopped.
ImageMetaConsumer::init
void init(unsigned gpu_id, const std::string &output_folder_path, const std::string &frame_to_skip_rules_path, float min_box_confidence, float max_box_confidence, unsigned min_box_width, unsigned min_box_height, bool save_full_frame_enabled, bool save_cropped_obj_enabled, unsigned seconds_to_skip_interval, unsigned source_nb)
Fill information in the Consumer.
ImageMetaConsumer::~ImageMetaConsumer
~ImageMetaConsumer()
End the job of the thread dequeing by calling stop()
ImageMetaConsumer::JSON
@ JSON
Definition: image_meta_consumer.h:41
deepstream_config.h
ImageMetaConsumer::make_img_path
std::string make_img_path(ImageMetaConsumer::ImageSizeType ist, unsigned stream_source_id, const std::string &datetime_iso8601)
Metadata writer for a unique file (Json or Csv)
ImageMetaConsumer::get_max_confidence
float get_max_confidence() const
Max confidence getter.
ImageMetaConsumer::get_gpu_id
unsigned get_gpu_id() const
GPU ID getter.
ImageMetaConsumer
Definition: image_meta_consumer.h:37
ImageMetaConsumer::add_meta_kitti
void add_meta_kitti(const std::pair< std::string, std::string > &meta)
Add metadata to the stored concurrent queue.
gstnvdsmeta.h
ImageMetaConsumer::CROPPED_TO_OBJECT
@ CROPPED_TO_OBJECT
Definition: image_meta_consumer.h:47
ImageMetaConsumer::data_was_saved_for_source
void data_was_saved_for_source(unsigned source_id)
If an image was saved, then the timestamp of the last image saved is stored.
ImageMetaConsumer::OutputType
OutputType
Definition: image_meta_consumer.h:39
capture_time_rules.h
ImageMetaConsumer::get_min_confidence
float get_min_confidence() const
Min confidence getter.
ImageMetaConsumer::unlock_source_nb
void unlock_source_nb(unsigned source_id)
Unlock a stream when a stream was locked for image saving.
ImageMetaConsumer::get_is_stopped
bool get_is_stopped() const
Dequeuing thread status getter.
ImageMetaConsumer::get_min_box_width
unsigned get_min_box_width() const
Minimum box width getter.
ImageMetaConsumer::get_save_cropped_images_enabled
bool get_save_cropped_images_enabled() const
Dequeuing cropped image enable getter.
ImageMetaConsumer::init_image_save_library_on_first_time
void init_image_save_library_on_first_time()
Create image saving context if needed.
gst-nvmessage.h
NvDsObjEncCtxHandle
struct _NvDsObjEncCtx * NvDsObjEncCtxHandle
Opaque Handle to the Object Encoder Context.
Definition: nvds_obj_encode.h:40
ImageMetaConsumer::add_meta_csv
void add_meta_csv(const std::string &meta)
Add metadata to the stored concurrent queue.
ImageMetaConsumer::FULL_FRAME
@ FULL_FRAME
Definition: image_meta_consumer.h:46
CaptureTimeRules
Definition: capture_time_rules.h:20
ImageMetaConsumer::get_save_full_frame_enabled
bool get_save_full_frame_enabled() const
Dequeuing full frame enabled getter.
ImageMetaConsumer::get_min_box_height
unsigned get_min_box_height() const
Minimum box height getter.
ImageMetaConsumer::ImageSizeType
ImageSizeType
Definition: image_meta_consumer.h:45
ImageMetaConsumer::should_save_data
bool should_save_data(unsigned source_id)
This function checks if a certain amount of time passed before the last image was saved.
nvbufsurface.h
ImageMetaConsumer::get_obj_ctx_handle
NvDsObjEncCtxHandle get_obj_ctx_handle()
Image Save Thread Handler.
ImageMetaConsumer::CSV
@ CSV
Definition: image_meta_consumer.h:42
ImageMetaConsumer::lock_source_nb
void lock_source_nb(unsigned source_id)
Lock a stream when an image save is needed in order not to save multiple images from the same stream ...
concurrent_queue.h