NVIDIA DeepStream SDK API Reference

6.4 Release
image_meta_consumer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2022, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #pragma once
24 
25 #include <condition_variable>
26 #include <mutex>
27 #include <string>
28 #include <atomic>
29 #include <thread>
30 #include <iostream>
31 #include <fstream>
32 #include <sstream>
33 #include <iomanip>
34 #include <ios>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38 #include <deepstream_config.h>
39 #include "gstnvdsmeta.h"
40 #include "nvbufsurface.h"
41 #include "gst-nvmessage.h"
42 #include "nvds_obj_encode.h"
43 #include "concurrent_queue.h"
44 #include "capture_time_rules.h"
45 
47 public:
48  enum OutputType {
49  KITTI = 0,
50  JSON = 1,
51  CSV = 2
52  };
53 
57  };
58 
61 
64 
83  void init(unsigned gpu_id, const std::string &output_folder_path, const std::string &frame_to_skip_rules_path,
84  float min_box_confidence, float max_box_confidence,
85  unsigned min_box_width, unsigned min_box_height,
86  bool save_full_frame_enabled, bool save_cropped_obj_enabled,
87  unsigned seconds_to_skip_interval, unsigned source_nb);
88 
91  void add_meta_csv(const std::string &meta);
92 
95  void add_meta_json(const std::string &meta);
96 
100  void add_meta_kitti(const std::pair<std::string, std::string> &meta);
101 
103  void stop();
104 
105 
108  float get_min_confidence() const;
109 
112  float get_max_confidence() const;
113 
116  unsigned get_gpu_id() const;
117 
120  unsigned get_min_box_width() const;
121 
124  unsigned get_min_box_height() const;
125 
128  bool get_is_stopped() const;
129 
132  bool get_save_full_frame_enabled() const;
133 
136  bool get_save_cropped_images_enabled() const;
137 
141 
147  unsigned stream_source_id,
148  const std::string &datetime_iso8601);
149 
152 
158  bool should_save_data(unsigned source_id);
159 
163  void lock_source_nb(unsigned source_id);
164 
167  void data_was_saved_for_source(unsigned source_id);
168 
171  void unlock_source_nb(unsigned source_id);
172 
173 private:
174 
176  void run();
177 
179  void multi_metadata_maker(ConcurrentQueue<std::pair<std::string, std::string>> &queue,
180  std::mutex &mutex, std::condition_variable &cv);
181 
183  bool setup_files();
184 
186  bool setup_folders();
187 
189  void write_intro(std::ofstream &os, OutputType &ot);
190 
192  void write_mid_separator(std::ofstream &os, OutputType &ot);
193 
195  void write_end(std::ofstream &os, OutputType &ot, unsigned total_nb);
196 
198  unsigned int get_unique_id();
199 
202  void single_metadata_maker(const std::string &extension,
204  std::mutex &mutex, std::condition_variable &cv,
205  OutputType ot);
206 
208  ConcurrentQueue<std::string> queue_csv_;
209  ConcurrentQueue<std::string> queue_json_;
210  std::mutex m_kitti_;
211  std::mutex m_json_;
212  std::mutex m_csv_;
213  std::condition_variable cv_kitti_;
214  std::condition_variable cv_json_;
215  std::condition_variable cv_csv_;
216  std::atomic<bool> is_stopped_;
217  std::string output_folder_path_;
218  std::string images_cropped_obj_output_folder_;
219  std::string images_full_frame_output_folder_;
220  std::string labels_output_folder_;
221  std::thread th_kitti_;
222  std::thread th_json_;
223  std::thread th_csv_;
224  std::mutex mutex_unique_index_;
225  unsigned int unique_index_ = 0;
226  float min_confidence_;
227  float max_confidence_;
228  unsigned gpu_id_;
229  unsigned min_box_width_;
230  unsigned min_box_height_;
231  bool save_full_frame_enabled_;
232  bool save_cropped_obj_enabled_;
233  std::vector<std::chrono::_V2::system_clock::time_point> time_last_frame_saved_list_;
234  std::array<std::mutex, MAX_SOURCE_BINS> mutex_frame_saved_list_;
235  CaptureTimeRules ctr_;
236  NvDsObjEncCtxHandle obj_ctx_handle_;
237  bool image_saving_library_is_init_;
238 };
ImageMetaConsumer::KITTI
@ KITTI
Definition: image_meta_consumer.h:49
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:32
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:50
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:46
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:56
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:48
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:38
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:55
CaptureTimeRules
Definition: capture_time_rules.h:30
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:54
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:51
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