NVIDIA DeepStream SDK API Reference

9.2 Release
sources/libs/nvmultiobjecttracker/include/modules/ReID/ReIDCropScaler.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2023-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 #ifndef ReIDCropScaler_HPP_
19 #define ReIDCropScaler_HPP_
20 
21 #include <map>
22 #include <vector>
23 #include <unordered_map>
24 #include "nvbufsurface.h"
25 #include "vpi/Stream.h"
26 #include "vpi/Image.h"
27 #include "vpi/Array.h"
28 #include "vpi/algo/CropScaler.h"
29 #include "modules/utility/dataTypes.hpp"
30 #include "modules/NvMultiObjectTracker/nvdstracker.h"
31 
32 #if defined(__aarch64__)
33 #include <EGL/egl.h>
34 #include <EGL/eglext.h>
35 #include "cudaEGL.h"
36 #endif
37 
40 {
41 public:
44 
47 
61  void initialize(
62  const int debugVerboseLevel, const int maxStreamNum, const int maxBufSurfAddrSize, const int maxTargetNum,
63  const NvMotDataTypes::Size srcImgSize, const uint srcImgPitch, const NvBufSurfaceColorFormat colorFormatType,
64  const NvMotDataTypes::Size dstImgSize, const NvBufSurfaceColorFormat dstColorFormatType, const int dimOrder
65  );
66 
77  const std::map<NvMOTStreamId, std::vector<std::pair<uint, NvMotDataTypes::Rect2f> > > &roisInBatch,
78  const int lastBboxInd, const float outputScaleFactor, const std::vector<float> outputOffsets,
79  const std::map<NvMOTStreamId, NvMOTFrame*> &inputMeta
80  );
81 
83  float* getOutputImagePatchPtrs() { return m_odata_d; }
84 
85  #if defined VIZ_DEBUG_REID || defined VIZ_DEBUG_SEG || defined VIZ_DEBUG_POSE
86 
92  std::map<NvMOTStreamId, std::map<uint, cv::Mat > > getImagePatches2Host_OCV_batch(
93  const float outputScaleFactor, const std::vector<float> outputOffsets
94  );
95  #endif
96 
97 private:
104  void initialize4Output(
105  const NvMotDataTypes::Size dstSize, const NvBufSurfaceColorFormat dstColorFormat, const int dimOrder
106  );
107 
109  void allocateOutputMemories();
110 
112  void deAllocateOutputMemories();
113 
121  void allocateInputMemoriesBatch(const uint numFramesInBatch, const int nx, const int ny, const uint pitch_in);
122 
124  void deAllocateInputMemoriesBatch();
125 
133  void initialize4Input(
134  uint numFramesInBatch, const NvMotDataTypes::Size frameSize, const uint srcImgPitch, const uint frameChannels
135  );
136 
141  void checkInput(const std::map<NvMOTStreamId, NvMOTFrame*> &inputMeta);
142 
147  inline void getCudaMemPointers(NvBufSurfaceParams *baseSurface);
148 
149  bool m_isInit;
150 
151  // [Output configuration specific to ReID]
152  int m_width_out, m_height_out, m_channels_out;
153  int m_dimOrder;
154  NvBufSurfaceColorFormat m_dstColorFormatType;
155  VPIImageFormat m_vpiImageFormat;
156  cudaStream_t m_cuStream;
157  const int m_cudaBlockSize_2D = 32;
158  int m_debugVerboseLevel;
159  int m_maxTargetsToTrack;
160  uint m_lastInd;
161  uint m_maxBufSurfAddrSize;
162  uint m_numFramesInBatchMax;
163 
164  // [Input image format]
165  int m_width_in, m_height_in, m_pitch_in, m_channels_in;
166  NvBufSurfaceColorFormat m_colorFormatType;
167 
168  // [Output memory specific to ReID]
169  float *m_odata_d;
170  float *m_odata_host;
171  #if defined VIZ_DEBUG_REID || defined VIZ_DEBUG_SEG || defined VIZ_DEBUG_POSE
172  float *m_odata_host_nchw;
173  #endif
174  uint8_t *m_odata_unit8_d;
175  VPIImageData m_vpiObjPatchesData;
176  VPIImage m_vpiObjPatches;
177 
178  // [Getting CUDA pointers from NvBufSurfaces]
179  int m_integrated_gpu;
180  int m_deviceId;
181  uint8_t *m_surfCudaMemPtr[NVBUF_MAX_PLANES];
182  #if defined(__aarch64__)
183  std::unordered_map<const NvBufSurfaceParams *, CUgraphicsResource> m_graphicsResourceAddrMap;
184  std::unordered_map<const NvBufSurfaceParams *, CUeglFrame> m_eglFrameAddrMap;
186  #endif
187 
188  // [VPI]
189  VPIStream m_vpiStream;
190  VPIBackend m_vpiBackend4CropnScale = VPI_BACKEND_CUDA;
191  VPIPayload m_vpiCropPayload;
192  int m_vpiMemFlags = VPI_BACKEND_CUDA | VPI_RESTRICT_MEM_USAGE;
193 
194  // [Sequence of input frames]
195  VPIImage *m_vpiInputFrameSequence;
196  VPIImageData *m_vpiInputFrameDataList;
197  std::vector<bool> m_isFrameCreated;
198 
200  // [List of tracked objects]
201  std::vector<VPIDCFTrackedBoundingBox> m_vpiDCFBboxes;
202  VPIArrayData m_vpiDCFBboxesData;
204  VPIArray m_vpiDCFBboxesArray;
205  int32_t m_vpiBboxesArraySize;
206 
208  #if defined VIZ_DEBUG_REID || defined VIZ_DEBUG_SEG || defined VIZ_DEBUG_POSE
209  std::map<uint, std::pair<NvMOTStreamId, uint>> m_targetNumInd2RoisInBatchMapper;
210  #endif
211 };
212 
213 #endif // ReIDCropScaler_HPP_
ReIDCropScaler::getOutputImagePatchPtrs
float * getOutputImagePatchPtrs()
Get the pointer to the output image patches on device memory with metadata.
Definition: sources/libs/nvmultiobjecttracker/include/modules/ReID/ReIDCropScaler.hpp:83
ReIDCropScaler::ReIDCropScaler
ReIDCropScaler()
Construct a ReIDCropScaler instance.
cudaStream_t
struct CUstream_st * cudaStream_t
Forward declaration of cudaStream_t.
Definition: includes/nvbufsurftransform.h:40
NVBUF_MAX_PLANES
#define NVBUF_MAX_PLANES
Defines the maximum number of planes.
Definition: includes/nvbufsurface.h:56
uint
unsigned int uint
Definition: sources/libs/nvmultiobjecttracker/include/modules/cuDCFv2/helper_math.h:25
ReIDCropScaler::~ReIDCropScaler
~ReIDCropScaler()
Release resources owned by the ReIDCropScaler instance.
NvBufSurfaceParams
Hold the information of single buffer in the batch.
Definition: includes/nvbufsurface.h:567
ReIDCropScaler
Crops and rescales target regions with VPI for network input.
Definition: sources/libs/nvmultiobjecttracker/include/modules/ReID/ReIDCropScaler.hpp:39
NvMOTStreamId
uint64_t NvMOTStreamId
Definition: includes/nvdstracker.h:53
ReIDCropScaler::frameTransformInBatch
bool frameTransformInBatch(const std::map< NvMOTStreamId, std::vector< std::pair< uint, NvMotDataTypes::Rect2f > > > &roisInBatch, const int lastBboxInd, const float outputScaleFactor, const std::vector< float > outputOffsets, const std::map< NvMOTStreamId, NvMOTFrame * > &inputMeta)
Transform (crop, scale and color conversion) a set of bboxes for the given single frame,...
NvMotDataTypes::Size
Represents a two-dimensional integer size.
Definition: sources/libs/nvmultiobjecttracker/include/modules/utility/dataTypes.hpp:52
NvBufSurfaceColorFormat
NvBufSurfaceColorFormat
Defines color formats for NvBufSurface.
Definition: includes/nvbufsurface.h:110
ReIDCropScaler::initialize
void initialize(const int debugVerboseLevel, const int maxStreamNum, const int maxBufSurfAddrSize, const int maxTargetNum, const NvMotDataTypes::Size srcImgSize, const uint srcImgPitch, const NvBufSurfaceColorFormat colorFormatType, const NvMotDataTypes::Size dstImgSize, const NvBufSurfaceColorFormat dstColorFormatType, const int dimOrder)
Initialize for image cropping and scaling.