Jetson Inference
DNN Vision Library
depthNet.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021, 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 #ifndef __DEPTH_NET_H__
24 #define __DEPTH_NET_H__
25 
26 #include "tensorNet.h"
28 
29 
34 #define DEPTHNET_DEFAULT_INPUT "input_0"
35 
40 #define DEPTHNET_DEFAULT_OUTPUT "output_0"
41 
46 #define DEPTHNET_MODEL_TYPE "monodepth"
47 
52 #define DEPTHNET_USAGE_STRING "depthNet arguments: \n" \
53  " --network NETWORK pre-trained model to load, one of the following:\n" \
54  " * fcn-mobilenet\n" \
55  " * fcn-resnet18\n" \
56  " * fcn-resnet50\n" \
57  " --model MODEL path to custom model to load (onnx)\n" \
58  " --input_blob INPUT name of the input layer (default is '" DEPTHNET_DEFAULT_INPUT "')\n" \
59  " --output_blob OUTPUT name of the output layer (default is '" DEPTHNET_DEFAULT_OUTPUT "')\n" \
60  " --profile enable layer profiling in TensorRT\n\n"
61 
62 
67 class depthNet : public tensorNet
68 {
69 public:
74  {
75  VISUALIZE_INPUT = (1 << 0),
76  VISUALIZE_DEPTH = (1 << 1),
77  };
78 
83  static uint32_t VisualizationFlagsFromStr( const char* str, uint32_t default_value=VISUALIZE_INPUT|VISUALIZE_DEPTH );
84 
89  static depthNet* Create( const char* network="fcn-mobilenet",
90  uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE,
91  precisionType precision=TYPE_FASTEST,
92  deviceType device=DEVICE_GPU, bool allowGPUFallback=true );
93 
103  static depthNet* Create( const char* model_path,
104  const char* input=DEPTHNET_DEFAULT_INPUT,
105  const char* output=DEPTHNET_DEFAULT_OUTPUT,
106  uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE,
107  precisionType precision=TYPE_FASTEST,
108  deviceType device=DEVICE_GPU, bool allowGPUFallback=true );
109 
118  static depthNet* Create( const char* model_path, const char* input,
119  const Dims3& inputDims, const char* output,
120  uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE,
121  precisionType precision=TYPE_FASTEST,
122  deviceType device=DEVICE_GPU, bool allowGPUFallback=true );
123 
127  static depthNet* Create( int argc, char** argv );
128 
132  static depthNet* Create( const commandLine& cmdLine );
133 
137  static inline const char* Usage() { return DEPTHNET_USAGE_STRING; }
138 
142  virtual ~depthNet();
143 
148  template<typename T> bool Process( T* image, uint32_t width, uint32_t height ) { return Process((void*)image, width, height, imageFormatFromType<T>()); }
149 
154  bool Process( void* input, uint32_t width, uint32_t height, imageFormat format );
155 
160  template<typename T1, typename T2>
161  bool Process( T1* input, T2* output, uint32_t width, uint32_t height,
163  cudaFilterMode filter=FILTER_LINEAR ) { return Process((void*)input, imageFormatFromType<T1>(), (void*)output, imageFormatFromType<T2>(), width, height, colormap, filter); }
164 
169  bool Process( void* input, imageFormat input_format,
170  void* output, imageFormat output_format,
171  uint32_t width, uint32_t height,
173  cudaFilterMode filter=FILTER_LINEAR );
174 
179  template<typename T1, typename T2>
180  bool Process( T1* input, uint32_t input_width, uint32_t input_height,
181  T2* output, uint32_t output_width, uint32_t output_height,
183  cudaFilterMode filter=FILTER_LINEAR ) { return Process((void*)input, input_width, input_height, imageFormatFromType<T1>(), (void*)output, output_width, output_height, imageFormatFromType<T2>(), colormap, filter); }
184 
189  bool Process( void* input, uint32_t input_width, uint32_t input_height, imageFormat input_format,
190  void* output, uint32_t output_width, uint32_t output_height, imageFormat output_format,
192  cudaFilterMode filter=FILTER_LINEAR );
193 
198  template<typename T>
199  bool Visualize( T* output, uint32_t width, uint32_t height,
201  cudaFilterMode filter=FILTER_LINEAR ) { return Visualize((void*)output, width, height, imageFormatFromType<T>(), colormap, filter); }
202 
207  bool Visualize( void* output, uint32_t width, uint32_t height, imageFormat format,
209  cudaFilterMode filter=FILTER_LINEAR );
210 
214  inline float* GetDepthField() const { return mOutputs[0].CUDA; }
215 
219  inline uint32_t GetDepthFieldWidth() const { return DIMS_W(mOutputs[0].dims); }
220 
224  inline uint32_t GetDepthFieldHeight() const { return DIMS_H(mOutputs[0].dims); }
225 
230  bool SavePointCloud( const char* filename );
231 
236  bool SavePointCloud( const char* filename, float* rgba, uint32_t width, uint32_t height );
237 
242  bool SavePointCloud( const char* filename, float* rgba, uint32_t width, uint32_t height,
243  const float2& focalLength, const float2& principalPoint );
244 
249  bool SavePointCloud( const char* filename, float* rgba, uint32_t width, uint32_t height,
250  const float intrinsicCalibration[3][3] );
251 
256  bool SavePointCloud( const char* filename, float* rgba, uint32_t width, uint32_t height,
257  const char* intrinsicCalibrationPath );
258 
259 protected:
260  depthNet();
261 
262  bool allocHistogramBuffers();
263  bool histogramEqualization();
265 
266  int2* mDepthRange;
268  uint32_t* mHistogram;
271  uint32_t* mHistogramEDU;
272 
274  #define DEPTH_FLOAT_TO_INT 1000000
275 
277  #define DEPTH_HISTOGRAM_BINS 256
278 };
279 
280 
282 
283 #endif
284 
DEPTHNET_DEFAULT_OUTPUT
#define DEPTHNET_DEFAULT_OUTPUT
Name of default output blob for depthNet model.
Definition: depthNet.h:40
depthNet::~depthNet
virtual ~depthNet()
Destroy.
depthNet::histogramEqualization
bool histogramEqualization()
depthNet::Process
bool Process(T1 *input, T2 *output, uint32_t width, uint32_t height, cudaColormapType colormap=COLORMAP_VIRIDIS_INVERTED, cudaFilterMode filter=FILTER_LINEAR)
Process an RGB/RGBA image and map the depth image with the specified colormap.
Definition: depthNet.h:161
DEPTHNET_USAGE_STRING
#define DEPTHNET_USAGE_STRING
Command-line options able to be passed to depthNet::Create()
Definition: depthNet.h:52
depthNet::mDepthEqualized
float * mDepthEqualized
Definition: depthNet.h:267
FILTER_LINEAR
@ FILTER_LINEAR
Bilinear filtering.
Definition: cudaFilterMode.h:38
depthNet::SavePointCloud
bool SavePointCloud(const char *filename)
Extract and save the point cloud to a PCD file (depth only).
depthNet::mHistogramPDF
float * mHistogramPDF
Definition: depthNet.h:269
COLORMAP_VIRIDIS_INVERTED
@ COLORMAP_VIRIDIS_INVERTED
Viridis colormap (inverted), see http://bids.github.io/colormap/.
Definition: cudaColormap.h:52
depthNet::GetDepthFieldHeight
uint32_t GetDepthFieldHeight() const
Return the height of the depth field.
Definition: depthNet.h:224
depthNet::VisualizationFlagsFromStr
static uint32_t VisualizationFlagsFromStr(const char *str, uint32_t default_value=VISUALIZE_INPUT|VISUALIZE_DEPTH)
Parse a string of one of more VisualizationMode values.
depthNet::mHistogramEDU
uint32_t * mHistogramEDU
Definition: depthNet.h:271
depthNet::mHistogramCDF
float * mHistogramCDF
Definition: depthNet.h:270
depthNet::Process
bool Process(T *image, uint32_t width, uint32_t height)
Compute the depth field from a monocular RGB/RGBA image.
Definition: depthNet.h:148
DEVICE_GPU
@ DEVICE_GPU
GPU (if multiple GPUs are present, a specific GPU can be selected with cudaSetDevice()
Definition: tensorNet.h:131
Dims3
nvinfer1::Dims3 Dims3
Definition: tensorNet.h:58
depthNet::allocHistogramBuffers
bool allocHistogramBuffers()
COLORMAP_DEFAULT
@ COLORMAP_DEFAULT
Definition: cudaColormap.h:60
depthNet::Visualize
bool Visualize(T *output, uint32_t width, uint32_t height, cudaColormapType colormap=COLORMAP_DEFAULT, cudaFilterMode filter=FILTER_LINEAR)
Visualize the raw depth field into a colorized RGB/RGBA depth map.
Definition: depthNet.h:199
deviceType
deviceType
Enumeration for indicating the desired device that the network should run on, if available in hardwar...
Definition: tensorNet.h:129
depthNet::GetDepthField
float * GetDepthField() const
Return the raw depth field.
Definition: depthNet.h:214
depthNet::VISUALIZE_DEPTH
@ VISUALIZE_DEPTH
Display the colorized depth field.
Definition: depthNet.h:76
DEPTHNET_DEFAULT_INPUT
#define DEPTHNET_DEFAULT_INPUT
Name of default input blob for depthNet model.
Definition: depthNet.h:34
tensorNet.h
DIMS_H
#define DIMS_H(x)
Definition: tensorNet.h:61
TYPE_FASTEST
@ TYPE_FASTEST
The fastest detected precision should be use (i.e.
Definition: tensorNet.h:105
depthNet::Process
bool Process(T1 *input, uint32_t input_width, uint32_t input_height, T2 *output, uint32_t output_width, uint32_t output_height, cudaColormapType colormap=COLORMAP_DEFAULT, cudaFilterMode filter=FILTER_LINEAR)
Process an RGB/RGBA image and map the depth image with the specified colormap.
Definition: depthNet.h:180
depthNet::depthNet
depthNet()
cudaFilterMode
cudaFilterMode
Enumeration of interpolation filtering modes.
Definition: cudaFilterMode.h:35
depthNet::VISUALIZE_INPUT
@ VISUALIZE_INPUT
Display the original input image.
Definition: depthNet.h:75
depthNet::Create
static depthNet * Create(const char *network="fcn-mobilenet", uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE, precisionType precision=TYPE_FASTEST, deviceType device=DEVICE_GPU, bool allowGPUFallback=true)
Load a pre-trained model.
depthNet::Usage
static const char * Usage()
Usage string for command line arguments to Create()
Definition: depthNet.h:137
precisionType
precisionType
Enumeration for indicating the desired precision that the network should run in, if available in hard...
Definition: tensorNet.h:102
depthNet
Mono depth estimation from monocular images, using TensorRT.
Definition: depthNet.h:67
tensorNet
Abstract class for loading a tensor network with TensorRT.
Definition: tensorNet.h:218
cudaColormap.h
DIMS_W
#define DIMS_W(x)
Definition: tensorNet.h:62
depthNet::mDepthRange
int2 * mDepthRange
Definition: depthNet.h:266
cudaColormapType
cudaColormapType
Enumeration of built-in colormaps.
Definition: cudaColormap.h:36
DEFAULT_MAX_BATCH_SIZE
#define DEFAULT_MAX_BATCH_SIZE
Default maximum batch size.
Definition: tensorNet.h:88
commandLine
Command line parser for extracting flags, values, and strings.
Definition: commandLine.h:35
depthNet::mHistogram
uint32_t * mHistogram
Definition: depthNet.h:268
depthNet::histogramEqualizationCUDA
bool histogramEqualizationCUDA()
imageFormat
imageFormat
The imageFormat enum is used to identify the pixel format and colorspace of an image.
Definition: imageFormat.h:49
depthNet::VisualizationFlags
VisualizationFlags
Visualization flags.
Definition: depthNet.h:73
tensorNet::mOutputs
std::vector< layerInfo > mOutputs
Definition: tensorNet.h:819
depthNet::GetDepthFieldWidth
uint32_t GetDepthFieldWidth() const
Return the width of the depth field.
Definition: depthNet.h:219