NVIDIA DeepStream SDK API Reference

9.1 Release
9.1/service-maker/sources/modules/kitti_dump_probe/kitti_dump_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 <string>
19 #include <sys/stat.h>
20 #include <fstream>
21 
22 #include "buffer_probe.hpp"
23 
24 #define UNTRACKED_OBJECT_ID 0xFFFFFFFFFFFFFFFF
25 
26 #define NVDS_OBJ_VISIBILITY 20
27 #define NVDS_OBJ_IMAGE_FOOT_LOCATION 21
28 #define NVDS_TRACKER_PAST_FRAME_META 15
29 
30 namespace deepstream {
31  class NvDsKittiDump: public BufferProbe::IBatchMetadataOperator {
32  public:
33  void generateInferenceKittiDump(BatchMetadata& data, std::string output_dir) {
34  char bbox_file[1024] = {0};
35  FILE *bbox_params_dump_file = NULL;
36  FrameMetadata::Iterator frame_itr;
37  for (data.initiateIterator(frame_itr); !frame_itr->done(); frame_itr->next()) {
38  uint stream_id = (*frame_itr)->padIndex();
39  snprintf(bbox_file, sizeof(bbox_file) - 1, "%s/%03u_%06lu.txt",
40  output_dir.c_str(), stream_id,
41  (ulong)(*frame_itr)->frameNum());
42  bbox_params_dump_file = fopen(bbox_file, "w");
43  if (!bbox_params_dump_file) continue;
44 
45  FrameMetadata& frame_meta = frame_itr->get();
47 
48  for (frame_meta.initiateIterator(obj_itr); !obj_itr->done(); obj_itr->next()) {
49  ObjectMetadata& object_meta = obj_itr->get();
50  float confidence = object_meta.confidence();
51  float left = object_meta.rectParams().left;
52  float top = object_meta.rectParams().top;
53  float right = left + object_meta.rectParams().width;
54  float bottom = top + object_meta.rectParams().height;
55 
56  if (object_meta.objectId() == UNTRACKED_OBJECT_ID) {
57  fprintf(bbox_params_dump_file,
58  "%s 0.0 0 0.0 %f %f %f %f 0.0 0.0 0.0 0.0 0.0 0.0 0.0 %f\n",
59  object_meta.label().c_str(), left, top, right, bottom, confidence);
60  } else {
61  fprintf(bbox_params_dump_file,
62  "%s %lu 0.0 0 0.0 %f %f %f %f 0.0 0.0 0.0 0.0 0.0 0.0 0.0 %f\n",
63  object_meta.label().c_str(), object_meta.objectId(), left, top, right, bottom,
64  confidence);
65  }
66  }
67  fclose(bbox_params_dump_file);
68  }
69  }
70 
71  void generateTrackerKittiDump(BatchMetadata& data, std::string output_dir) {
72  char bbox_file[1024] = {0};
73  FILE *bbox_params_dump_file = NULL;
74  FrameMetadata::Iterator frame_itr;
75  for (data.initiateIterator(frame_itr); !frame_itr->done(); frame_itr->next()) {
76  uint stream_id = (*frame_itr)->padIndex();
77  snprintf(bbox_file, sizeof(bbox_file) - 1, "%s/%03u_%06lu.txt",
78  output_dir.c_str(), stream_id,
79  (ulong)(*frame_itr)->frameNum());
80  bbox_params_dump_file = fopen(bbox_file, "w");
81  if (!bbox_params_dump_file) continue;
82 
83  FrameMetadata& frame_meta = frame_itr->get();
85 
86  for (frame_meta.initiateIterator(obj_itr); !obj_itr->done(); obj_itr->next()) {
87  ObjectMetadata& object_meta = obj_itr->get();
88  float confidence = object_meta.trackerConfidence();
89  uint64_t id = object_meta.objectId();
90  bool write_proj_info = false;
91  float visibility = -1.0, x_img_foot = -1.0, y_img_foot = -1.0;
92 
93  float left = object_meta.nvBboxInfo().left;
94  float top = object_meta.nvBboxInfo().top;
95  float right = left + object_meta.nvBboxInfo().width;
96  float bottom = top + object_meta.nvBboxInfo().height;
97 
98  object_meta.iterate([&visibility, &write_proj_info](const UserMetadata& user_meta) {
99  ObjectVisibilityUserMetadata visibility_meta(user_meta);
100  visibility = visibility_meta.getVisibility();
101  write_proj_info = true;
103 
104  object_meta.iterate([&x_img_foot, &y_img_foot, &write_proj_info](const UserMetadata& user_meta) {
105  ObjectImageFootLocationUserMetadata foot_meta(user_meta);
106  x_img_foot = foot_meta.getImageFootLocation().first;
107  y_img_foot = foot_meta.getImageFootLocation().second;
108  write_proj_info = true;
110 
111 
112  if (write_proj_info)
113  {
114  fprintf (bbox_params_dump_file,
115  "%s %lu 0.0 0 0.0 %f %f %f %f 0.0 0.0 0.0 0.0 0.0 0.0 0.0 %f %f %f %f\n",
116  object_meta.label().c_str(), id, left, top, right, bottom, confidence, visibility, x_img_foot, y_img_foot);
117  }
118  else
119  {
120  fprintf (bbox_params_dump_file,
121  "%s %lu 0.0 0 0.0 %f %f %f %f 0.0 0.0 0.0 0.0 0.0 0.0 0.0 %f\n",
122  object_meta.label().c_str(), id, left, top, right, bottom, confidence);
123  }
124  }
125  fclose(bbox_params_dump_file);
126  }
127  }
128 
129  void generateTrackerPastKittiDump(BatchMetadata& data, std::string output_dir) {
130  data.iterate([&output_dir](const UserMetadata &user_meta) {
131  TrackerPastFrameUserMetadata past_frame_meta(user_meta);
132  NvDsTargetMiscDataBatch* pPastFrameObjBatch = past_frame_meta.getTrackerMiscDataBatch();
133 
134  if (!pPastFrameObjBatch) {
135  return; // Skip if batch is null
136  }
137 
138  for (uint si = 0; si < pPastFrameObjBatch->numFilled; si++) {
139  NvDsTargetMiscDataStream *objStream = (pPastFrameObjBatch->list) + si;
140  uint stream_id = (uint) (objStream->streamID);
141  for (uint li = 0; li < objStream->numFilled; li++) {
142  NvDsTargetMiscDataObject *objList = (objStream->list) + li;
143  for (uint oi = 0; oi < objList->numObj; oi++) {
144  NvDsTargetMiscDataFrame *obj = (objList->list) + oi;
145 
146  char bbox_file[1024] = { 0 };
147  snprintf(bbox_file, sizeof(bbox_file) - 1, "%s/%03u_%06lu.txt",
148  output_dir.c_str(), stream_id, (ulong)obj->frameNum);
149 
150  float left = obj->tBbox.left;
151  float right = left + obj->tBbox.width;
152  float top = obj->tBbox.top;
153  float bottom = top + obj->tBbox.height;
154  // Past frame object confidence given by tracker
155  float confidence = obj->confidence;
156 
157  FILE *bbox_params_dump_file = fopen(bbox_file, "a");
158  if (!bbox_params_dump_file) {
159  continue; // Skip this iteration if file open fails
160  }
161 
162  fprintf(bbox_params_dump_file,
163  "%s %lu 0.0 0 0.0 %f %f %f %f 0.0 0.0 0.0 0.0 0.0 0.0 0.0 %f\n",
164  objList->objLabel, objList->uniqueId, left, top, right, bottom,
165  confidence);
166  fclose(bbox_params_dump_file);
167  }
168  }
169  }
171  }
172 
175  std::string output_dir = "/tmp/kitti/";
176  probe.getProperty("kitti-dir", output_dir);
177 
178  bool tracker_kitti_output = false;
179  probe.getProperty("tracker-kitti-output", tracker_kitti_output);
180 
181  if (tracker_kitti_output) {
182  generateTrackerKittiDump(data, output_dir);
183  generateTrackerPastKittiDump(data, output_dir);
184  } else {
185  generateInferenceKittiDump(data, output_dir);
186  }
187 
188  return probeReturn::Probe_Ok;
189  }
190  };
191 
192 }
193 
deepstream::probeReturn::Probe_Ok
@ Probe_Ok
Nothing abnormal, the buffer will be treated as usual.
deepstream::NvDsKittiDump::generateTrackerPastKittiDump
void generateTrackerPastKittiDump(BatchMetadata &data, std::string output_dir)
Definition: 9.1/service-maker/sources/modules/kitti_dump_probe/kitti_dump_probe.hpp:129
_NvDsTargetMiscDataObject::numObj
uint32_t numObj
Number of frames this target appreared in the past.
Definition: sources/includes/nvds_tracker_meta.h:81
deepstream::ObjectMetadata::iterate
unsigned int iterate(const std::function< void(const ClassifierMetadata &)> &func) const
Iterate the classifier metadata within it.
UNTRACKED_OBJECT_ID
#define UNTRACKED_OBJECT_ID
Definition: 9.1/service-maker/sources/modules/kitti_dump_probe/kitti_dump_probe.hpp:24
deepstream::ObjectMetadata::Iterator
std::unique_ptr< AbstractIterator< ObjectMetadata > > Iterator
Definition: service-maker/includes/metadata.hpp:184
_NvOSD_RectParams::width
float width
Holds the box's width in pixels.
Definition: sources/includes/nvll_osd_struct.h:152
_NvDsTargetMiscDataStream
All misc targets data for a given stream.
Definition: sources/includes/nvds_tracker_meta.h:96
deepstream::ObjectVisibilityUserMetadata::getVisibility
float getVisibility() const
Get the visibility of the object.
_NvDsTargetMiscDataBatch::list
NvDsTargetMiscDataStream * list
Pointer to array of stream lists.
Definition: sources/includes/nvds_tracker_meta.h:116
deepstream::ObjectMetadata::objectId
unsigned long int objectId() const
Number to identify the object which is tracked in the scene.
_NvBbox_Coords::height
float height
Holds the box's height in pixels.
Definition: sources/includes/nvll_osd_struct.h:80
_NvDsTargetMiscDataStream::streamID
uint32_t streamID
Stream id the same as frame_meta->pad_index.
Definition: sources/includes/nvds_tracker_meta.h:101
deepstream::BufferProbe
Represent a custom object for the purpose of probing output buffers.
Definition: service-maker/includes/buffer_probe.hpp:63
_NvDsTargetMiscDataFrame::tBbox
NvOSD_RectParams tBbox
Bounding box.
Definition: sources/includes/nvds_tracker_meta.h:58
_NvBbox_Coords::left
float left
Holds the box's left coordinate in pixels.
Definition: sources/includes/nvll_osd_struct.h:72
_NvDsTargetMiscDataStream::list
NvDsTargetMiscDataObject * list
Pointer to targets inside this stream.
Definition: sources/includes/nvds_tracker_meta.h:99
deepstream::NvDsKittiDump::generateTrackerKittiDump
void generateTrackerKittiDump(BatchMetadata &data, std::string output_dir)
Definition: 9.1/service-maker/sources/modules/kitti_dump_probe/kitti_dump_probe.hpp:71
_NvDsTargetMiscDataObject::objLabel
char objLabel[MAX_LABEL_SIZE]
An array of the string describing the target class.
Definition: sources/includes/nvds_tracker_meta.h:89
_NvDsTargetMiscDataFrame::confidence
float confidence
Tracking confidence.
Definition: sources/includes/nvds_tracker_meta.h:60
deepstream::BatchMetadata::iterate
unsigned int iterate(const std::function< void(const FrameMetadata &)> &func) const
Iterate the frame metadata within it.
deepstream::ObjectMetadata::nvBboxInfo
NvBbox_Coords & nvBboxInfo() const
Bounding box of the object.
deepstream::NvDsKittiDump::generateInferenceKittiDump
void generateInferenceKittiDump(BatchMetadata &data, std::string output_dir)
Definition: 9.1/service-maker/sources/modules/kitti_dump_probe/kitti_dump_probe.hpp:33
deepstream::FrameMetadata::initiateIterator
void initiateIterator(ObjectMetadata::Iterator &) const
Get the iterator for object metadata within it.
deepstream::ObjectImageFootLocationUserMetadata
Definition: service-maker/includes/metadata.hpp:645
_NvDsTargetMiscDataObject::uniqueId
uint64_t uniqueId
Target tracking id.
Definition: sources/includes/nvds_tracker_meta.h:85
deepstream::ObjectMetadata
Object metadata.
Definition: service-maker/includes/metadata.hpp:182
deepstream::probeReturn
probeReturn
Return values from user implemented probe interfaces.
Definition: service-maker/includes/buffer_probe.hpp:47
deepstream
Definition: service-maker/includes/buffer.hpp:38
_NvDsTargetMiscDataStream::numFilled
uint32_t numFilled
Number of objects in this frame.
Definition: sources/includes/nvds_tracker_meta.h:107
deepstream::TrackerPastFrameUserMetadata
Definition: service-maker/includes/metadata.hpp:663
deepstream::BatchMetadata::initiateIterator
void initiateIterator(FrameMetadata::Iterator &) const
Get the iterator for frame metadata within it.
_NvBbox_Coords::width
float width
Holds the box's width in pixels.
Definition: sources/includes/nvll_osd_struct.h:78
_NvDsTargetMiscDataFrame::frameNum
uint32_t frameNum
Frame number.
Definition: sources/includes/nvds_tracker_meta.h:56
deepstream::BatchMetadata
Holds information about a formed batch containingframes from different sources.
Definition: service-maker/includes/metadata.hpp:797
_NvOSD_RectParams::left
float left
Holds the box's left coordinate in pixels.
Definition: sources/includes/nvll_osd_struct.h:146
_NvDsTargetMiscDataFrame
A single frame of misc data for a given Target.
Definition: sources/includes/nvds_tracker_meta.h:53
deepstream::NvDsKittiDump::NvDsKittiDump
NvDsKittiDump()
Definition: 9.1/service-maker/sources/modules/kitti_dump_probe/kitti_dump_probe.hpp:173
deepstream::FrameMetadata::Iterator
std::unique_ptr< AbstractIterator< FrameMetadata > > Iterator
Definition: service-maker/includes/metadata.hpp:456
deepstream::UserMetadata
Base class of user defined metadata.
Definition: service-maker/includes/metadata.hpp:110
NVDS_OBJ_VISIBILITY
#define NVDS_OBJ_VISIBILITY
Definition: 9.1/service-maker/sources/modules/kitti_dump_probe/kitti_dump_probe.hpp:26
deepstream::ObjectMetadata::label
std::string label() const
A string to describe the object class.
deepstream::ObjectImageFootLocationUserMetadata::getImageFootLocation
std::pair< float, float > getImageFootLocation() const
Get the image foot location of the object.
_NvDsTargetMiscDataObject::list
NvDsTargetMiscDataFrame * list
Pointer to a list per-frame information of the target.
Definition: sources/includes/nvds_tracker_meta.h:79
NVDS_TRACKER_PAST_FRAME_META
#define NVDS_TRACKER_PAST_FRAME_META
Definition: 9.1/service-maker/sources/modules/kitti_dump_probe/kitti_dump_probe.hpp:28
deepstream::NvDsKittiDump::handleData
virtual probeReturn handleData(BufferProbe &probe, BatchMetadata &data)
Definition: 9.1/service-maker/sources/modules/kitti_dump_probe/kitti_dump_probe.hpp:174
_NvOSD_RectParams::height
float height
Holds the box's height in pixels.
Definition: sources/includes/nvll_osd_struct.h:154
deepstream::ObjectVisibilityUserMetadata
Definition: service-maker/includes/metadata.hpp:635
_NvDsTargetMiscDataBatch
Batch of all streams of a given target misc output.
Definition: sources/includes/nvds_tracker_meta.h:113
deepstream::TrackerPastFrameUserMetadata::getTrackerMiscDataBatch
NvDsTargetMiscDataBatch * getTrackerMiscDataBatch() const
NVDS_OBJ_IMAGE_FOOT_LOCATION
#define NVDS_OBJ_IMAGE_FOOT_LOCATION
Definition: 9.1/service-maker/sources/modules/kitti_dump_probe/kitti_dump_probe.hpp:27
deepstream::ObjectMetadata::trackerConfidence
float trackerConfidence() const
Confidence level from tracker.
_NvBbox_Coords::top
float top
Holds the box's top coordinate in pixels.
Definition: sources/includes/nvll_osd_struct.h:75
_NvOSD_RectParams::top
float top
Holds the box's top coordinate in pixels.
Definition: sources/includes/nvll_osd_struct.h:149
deepstream::ObjectMetadata::confidence
float confidence() const
Confidence level.
deepstream::ObjectMetadata::rectParams
NvOSD_RectParams & rectParams() const
Bounding box of the object.
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
_NvDsTargetMiscDataBatch::numFilled
uint32_t numFilled
Number of filled blocks in the list.
Definition: sources/includes/nvds_tracker_meta.h:120
deepstream::FrameMetadata
Holds information for a single frame.
Definition: service-maker/includes/metadata.hpp:454
_NvDsTargetMiscDataObject
All misc data output for a single target.
Definition: sources/includes/nvds_tracker_meta.h:76