NVIDIA DeepStream SDK API Reference

9.1 Release
sources/apps/sample_apps/deepstream-transfer-learning-app/image_meta_consumer.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-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 #pragma once
19 
20 #include <condition_variable>
21 #include <array>
22 #include <mutex>
23 #include <string>
24 #include <atomic>
25 #include <thread>
26 #include <iostream>
27 #include <fstream>
28 #include <sstream>
29 #include <iomanip>
30 #include <ios>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34 #include <deepstream_config.h>
35 #include "gstnvdsmeta.h"
36 #include "nvbufsurface.h"
37 #include "gst-nvmessage.h"
38 #include "nvds_obj_encode.h"
39 #include "concurrent_queue.h"
40 #include "capture_time_rules.h"
41 
43 public:
44  enum OutputType {
45  KITTI = 0,
46  JSON = 1,
47  CSV = 2
48  };
49 
53  };
54 
57 
60 
79  void init(unsigned gpu_id, const std::string &output_folder_path, const std::string &frame_to_skip_rules_path,
80  float min_box_confidence, float max_box_confidence,
81  unsigned min_box_width, unsigned min_box_height,
82  bool save_full_frame_enabled, bool save_cropped_obj_enabled,
83  unsigned seconds_to_skip_interval, unsigned source_nb);
84 
87  void add_meta_csv(const std::string &meta);
88 
91  void add_meta_json(const std::string &meta);
92 
96  void add_meta_kitti(const std::pair<std::string, std::string> &meta);
97 
99  void stop();
100 
101 
104  float get_min_confidence() const;
105 
108  float get_max_confidence() const;
109 
112  unsigned get_gpu_id() const;
113 
116  unsigned get_min_box_width() const;
117 
120  unsigned get_min_box_height() const;
121 
124  bool get_is_stopped() const;
125 
128  bool get_save_full_frame_enabled() const;
129 
132  bool get_save_cropped_images_enabled() const;
133 
137 
143  unsigned stream_source_id,
144  const std::string &datetime_iso8601);
145 
148 
154  bool should_save_data(unsigned source_id);
155 
159  void lock_source_nb(unsigned source_id);
160 
163  void data_was_saved_for_source(unsigned source_id);
164 
167  void unlock_source_nb(unsigned source_id);
168 
169 private:
170 
172  void run();
173 
175  void multi_metadata_maker(ConcurrentQueue<std::pair<std::string, std::string>> &queue,
176  std::mutex &mutex, std::condition_variable &cv);
177 
179  bool setup_files();
180 
182  bool setup_folders();
183 
185  void write_intro(std::ofstream &os, OutputType &ot);
186 
188  void write_mid_separator(std::ofstream &os, OutputType &ot);
189 
191  void write_end(std::ofstream &os, OutputType &ot, unsigned total_nb);
192 
194  unsigned int get_unique_id();
195 
198  void single_metadata_maker(const std::string &extension,
200  std::mutex &mutex, std::condition_variable &cv,
201  OutputType ot);
202 
204  ConcurrentQueue<std::string> queue_csv_;
205  ConcurrentQueue<std::string> queue_json_;
206  std::mutex m_kitti_;
207  std::mutex m_json_;
208  std::mutex m_csv_;
209  std::condition_variable cv_kitti_;
210  std::condition_variable cv_json_;
211  std::condition_variable cv_csv_;
212  std::atomic<bool> is_stopped_;
213  std::string output_folder_path_;
214  std::string images_cropped_obj_output_folder_;
215  std::string images_full_frame_output_folder_;
216  std::string labels_output_folder_;
217  std::thread th_kitti_;
218  std::thread th_json_;
219  std::thread th_csv_;
220  std::mutex mutex_unique_index_;
221  unsigned int unique_index_ = 0;
222  float min_confidence_;
223  float max_confidence_;
224  unsigned gpu_id_;
225  unsigned min_box_width_;
226  unsigned min_box_height_;
227  bool save_full_frame_enabled_;
228  bool save_cropped_obj_enabled_;
229  std::vector<std::chrono::_V2::system_clock::time_point> time_last_frame_saved_list_;
230  std::array<std::mutex, MAX_SOURCE_BINS> mutex_frame_saved_list_;
231  CaptureTimeRules ctr_;
232  NvDsObjEncCtxHandle obj_ctx_handle_;
233  bool image_saving_library_is_init_;
234  std::mutex mutex_image_save_init_;
235 };
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: sources/apps/sample_apps/deepstream-transfer-learning-app/concurrent_queue.h:27
ImageMetaConsumer::add_meta_json
void add_meta_json(const std::string &meta)
Add metadata to the stored concurrent queue.
concurrent_queue.h
ImageMetaConsumer::CSV
@ CSV
Definition: sources/apps/sample_apps/deepstream-transfer-learning-app/image_meta_consumer.h:47
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::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::KITTI
@ KITTI
Definition: sources/apps/sample_apps/deepstream-transfer-learning-app/image_meta_consumer.h:45
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: sources/apps/sample_apps/deepstream-transfer-learning-app/image_meta_consumer.h:42
ImageMetaConsumer::add_meta_kitti
void add_meta_kitti(const std::pair< std::string, std::string > &meta)
Add metadata to the stored concurrent queue.
ImageMetaConsumer::JSON
@ JSON
Definition: sources/apps/sample_apps/deepstream-transfer-learning-app/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::FULL_FRAME
@ FULL_FRAME
Definition: sources/apps/sample_apps/deepstream-transfer-learning-app/image_meta_consumer.h:51
ImageMetaConsumer::OutputType
OutputType
Definition: sources/apps/sample_apps/deepstream-transfer-learning-app/image_meta_consumer.h:44
ImageMetaConsumer::get_min_confidence
float get_min_confidence() const
Min confidence getter.
ImageMetaConsumer::CROPPED_TO_OBJECT
@ CROPPED_TO_OBJECT
Definition: sources/apps/sample_apps/deepstream-transfer-learning-app/image_meta_consumer.h:52
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.
NvDsObjEncCtxHandle
struct _NvDsObjEncCtx * NvDsObjEncCtxHandle
Opaque Handle to the Object Encoder Context.
Definition: sources/includes/nvds_obj_encode.h:45
ImageMetaConsumer::add_meta_csv
void add_meta_csv(const std::string &meta)
Add metadata to the stored concurrent queue.
CaptureTimeRules
Definition: sources/apps/sample_apps/deepstream-transfer-learning-app/capture_time_rules.h:25
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: sources/apps/sample_apps/deepstream-transfer-learning-app/image_meta_consumer.h:50
capture_time_rules.h
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.
ImageMetaConsumer::get_obj_ctx_handle
NvDsObjEncCtxHandle get_obj_ctx_handle()
Image Save Thread Handler.
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 ...