NVIDIA DeepStream SDK API Reference

7.1 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 <mutex>
17 #include <string>
18 #include <atomic>
19 #include <thread>
20 #include <iostream>
21 #include <fstream>
22 #include <sstream>
23 #include <iomanip>
24 #include <ios>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <deepstream_config.h>
29 #include "gstnvdsmeta.h"
30 #include "nvbufsurface.h"
31 #include "gst-nvmessage.h"
32 #include "nvds_obj_encode.h"
33 #include "concurrent_queue.h"
34 #include "capture_time_rules.h"
35 
37 public:
38  enum OutputType {
39  KITTI = 0,
40  JSON = 1,
41  CSV = 2
42  };
43 
47  };
48 
51 
54 
73  void init(unsigned gpu_id, const std::string &output_folder_path, const std::string &frame_to_skip_rules_path,
74  float min_box_confidence, float max_box_confidence,
75  unsigned min_box_width, unsigned min_box_height,
76  bool save_full_frame_enabled, bool save_cropped_obj_enabled,
77  unsigned seconds_to_skip_interval, unsigned source_nb);
78 
81  void add_meta_csv(const std::string &meta);
82 
85  void add_meta_json(const std::string &meta);
86 
90  void add_meta_kitti(const std::pair<std::string, std::string> &meta);
91 
93  void stop();
94 
95 
98  float get_min_confidence() const;
99 
102  float get_max_confidence() const;
103 
106  unsigned get_gpu_id() const;
107 
110  unsigned get_min_box_width() const;
111 
114  unsigned get_min_box_height() const;
115 
118  bool get_is_stopped() const;
119 
122  bool get_save_full_frame_enabled() const;
123 
126  bool get_save_cropped_images_enabled() const;
127 
131 
137  unsigned stream_source_id,
138  const std::string &datetime_iso8601);
139 
142 
148  bool should_save_data(unsigned source_id);
149 
153  void lock_source_nb(unsigned source_id);
154 
157  void data_was_saved_for_source(unsigned source_id);
158 
161  void unlock_source_nb(unsigned source_id);
162 
163 private:
164 
166  void run();
167 
169  void multi_metadata_maker(ConcurrentQueue<std::pair<std::string, std::string>> &queue,
170  std::mutex &mutex, std::condition_variable &cv);
171 
173  bool setup_files();
174 
176  bool setup_folders();
177 
179  void write_intro(std::ofstream &os, OutputType &ot);
180 
182  void write_mid_separator(std::ofstream &os, OutputType &ot);
183 
185  void write_end(std::ofstream &os, OutputType &ot, unsigned total_nb);
186 
188  unsigned int get_unique_id();
189 
192  void single_metadata_maker(const std::string &extension,
194  std::mutex &mutex, std::condition_variable &cv,
195  OutputType ot);
196 
198  ConcurrentQueue<std::string> queue_csv_;
199  ConcurrentQueue<std::string> queue_json_;
200  std::mutex m_kitti_;
201  std::mutex m_json_;
202  std::mutex m_csv_;
203  std::condition_variable cv_kitti_;
204  std::condition_variable cv_json_;
205  std::condition_variable cv_csv_;
206  std::atomic<bool> is_stopped_;
207  std::string output_folder_path_;
208  std::string images_cropped_obj_output_folder_;
209  std::string images_full_frame_output_folder_;
210  std::string labels_output_folder_;
211  std::thread th_kitti_;
212  std::thread th_json_;
213  std::thread th_csv_;
214  std::mutex mutex_unique_index_;
215  unsigned int unique_index_ = 0;
216  float min_confidence_;
217  float max_confidence_;
218  unsigned gpu_id_;
219  unsigned min_box_width_;
220  unsigned min_box_height_;
221  bool save_full_frame_enabled_;
222  bool save_cropped_obj_enabled_;
223  std::vector<std::chrono::_V2::system_clock::time_point> time_last_frame_saved_list_;
224  std::array<std::mutex, MAX_SOURCE_BINS> mutex_frame_saved_list_;
225  CaptureTimeRules ctr_;
226  NvDsObjEncCtxHandle obj_ctx_handle_;
227  bool image_saving_library_is_init_;
228  std::mutex mutex_image_save_init_;
229 };
ImageMetaConsumer::KITTI
@ KITTI
Definition: image_meta_consumer.h:39
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:40
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:36
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:46
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:38
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:45
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:44
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:41
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