openvino-helpers.h
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2019 Intel Corporation. All Rights Reserved.
3 
4 #pragma once
5 
6 // The following warnings are given when including the OpenVINO headers:
7 // ...\openvino\inference_engine\include\ie_layouts.h(127): warning C4251: 'InferenceEngine::BlockingDesc::blockedDims': class 'std::vector<size_t,std::allocator<_Ty>>' needs to have dll-interface to be used by clients of class 'InferenceEngine::BlockingDesc'
8 // And:
9 // ...\openvino\inference_engine\src\extension\ext_list.hpp(25): warning C4275: non dll-interface class 'InferenceEngine::IExtension' used as base for dll-interface class 'InferenceEngine::Extensions::Cpu::CpuExtensions' (compiling source file ...)
10 // These should be harmless and not affect us. We disable them:
11 // (They even disable these warnings in their own CMake... see inference_engine/src/extension/CMakeList.txt)
12 #pragma warning(push)
13 #pragma warning(disable:4251)
14 #pragma warning(disable:4275)
15 # include <inference_engine.hpp>
16 # include <ie_iextension.h>
17 #ifdef OPENVINO2019
18 # include <ext_list.hpp> // Required for CPU extension usage
19 #endif
20 #pragma warning(pop)
21 
22 #include <opencv2/opencv.hpp>
23 #include <easylogging++.h>
24 
25 
26 namespace openvino_helpers
27 {
28  /*
29  Sets image data stored in cv::Mat object to a given Blob object.
30  Copies the mat data into the blob.
31  */
32  template <typename T>
33  void matU8ToBlob( const cv::Mat& orig_image, InferenceEngine::Blob::Ptr& blob, int batchIndex = 0 )
34  {
35  InferenceEngine::SizeVector blobSize = blob->getTensorDesc().getDims();
36  const int width = int( blobSize[3] );
37  const int height = int( blobSize[2] );
38  const size_t channels = blobSize[1];
39  T* blob_data = blob->buffer().as<T*>();
40 
41  cv::Mat resized_image( orig_image );
42  if( static_cast<int>(width) != orig_image.size().width ||
43  static_cast<int>(height) != orig_image.size().height )
44  {
45  cv::resize( orig_image, resized_image, cv::Size( width, height ) );
46  }
47 
48  size_t batchOffset = batchIndex * width * height * channels;
49 
50  if( channels == 1 )
51  {
52  for( int h = 0; h < height; h++ )
53  {
54  for( int w = 0; w < width; w++ )
55  {
56  blob_data[batchOffset + h * width + w] = resized_image.at<uchar>( h, w );
57  }
58  }
59  }
60  else if( channels == 3 )
61  {
62  for( int c = 0; c < channels; c++ )
63  {
64  for( int h = 0; h < height; h++ )
65  {
66  for( int w = 0; w < width; w++ )
67  {
68  blob_data[batchOffset + c * width * height + h * width + w] =
69  resized_image.at<cv::Vec3b>( h, w )[c];
70  }
71  }
72  }
73  }
74  else
75  {
76  THROW_IE_EXCEPTION << "Unsupported number of channels";
77  }
78  }
79 
80 
81  /*
82  Wraps data stored inside of a passed cv::Mat object by new Blob pointer.
83  No memory allocation occurs. The blob just points to existing cv::Mat data.
84  */
85  static InferenceEngine::Blob::Ptr wrapMat2Blob( const cv::Mat &mat )
86  {
87  size_t channels = mat.channels();
88  size_t height = mat.size().height;
89  size_t width = mat.size().width;
90 
91  size_t strideH = mat.step.buf[0];
92  size_t strideW = mat.step.buf[1];
93 
94  bool is_dense =
95  strideW == channels &&
96  strideH == channels * width;
97 
98  if( !is_dense ) THROW_IE_EXCEPTION
99  << "Doesn't support conversion from not dense cv::Mat";
100 
101  InferenceEngine::TensorDesc tDesc( InferenceEngine::Precision::U8,
102  { 1, channels, height, width },
103  InferenceEngine::Layout::NHWC );
104 
105  return InferenceEngine::make_shared_blob<uint8_t>( tDesc, mat.data );
106  }
107 
108 
109  /*
110  Remove extension from a file name.
111  */
112  inline std::string remove_ext( const std::string & filepath )
113  {
114  auto pos = filepath.rfind( '.' );
115  if( pos == std::string::npos )
116  return filepath;
117  return filepath.substr( 0, pos );
118  }
119 
120 
121  /*
122  Calculate the mean intensity of the given image
123  */
124  inline float calc_intensity( const cv::Mat & src )
125  {
126  cv::Mat tmp;
127  cv::cvtColor( src, tmp, cv::COLOR_RGB2GRAY );
128  cv::Scalar mean = cv::mean( tmp );
129 
130  return static_cast<float>(mean[0]);
131  }
132 
133 
134  /*
135  */
136  inline std::vector< std::string > read_labels( std::string const & filename )
137  {
138  std::vector< std::string > labels;
139  std::ifstream inputFile( filename );
140  std::copy( std::istream_iterator< std::string >( inputFile ),
141  std::istream_iterator< std::string >(),
142  std::back_inserter( labels ) );
143  return labels;
144  }
145 
146 
147  /*
148  Allow manipulation of a face bounding box so as to make additional face analytic networks more
149  effective.
150 
151  For example, a face may not include the hair. Gender detection, though, may find the hair very
152  important for proper classification!
153  */
154  inline cv::Rect adjust_face_bbox(
155  cv::Rect const & r,
156  float enlarge_coefficient = 1,
157  float dx_coefficient = 1,
158  float dy_coefficient = 1
159  )
160  {
161  int w = r.width;
162  int h = r.height;
163  int center_x = r.x + w / 2;
164  int center_y = r.y + h / 2;
165 
166  // Make square and enlarge
167  int max_of_sizes = std::max( w, h );
168  int new_width = static_cast<int>(enlarge_coefficient * max_of_sizes);
169  int new_height = static_cast<int>(enlarge_coefficient * max_of_sizes);
170 
171  // Offset, if requested
172  int new_x = center_x - static_cast<int>(std::floor( dx_coefficient * new_width / 2 ));
173  int new_y = center_y - static_cast<int>(std::floor( dy_coefficient * new_height / 2 ));
174 
175  return cv::Rect( new_x, new_y, new_width, new_height );
176  }
177 
178 
179  /*
180  Implementation of OpenVINO interface, allowing us to listen to any errors that occur
181  and output them for debugging using LOG(DEBUG).
182 
183  Example usage:
184  InferenceEngine::Core engine;
185  openvino_helpers::error_listener error_listener;
186  engine.SetLogCallback( error_listener );
187  */
188  class error_listener : public InferenceEngine::IErrorListener
189  {
190  void onError( char const * msg ) noexcept override
191  {
192  LOG(DEBUG) << "[InferenceEngine] " << msg;
193  }
194  };
195 }
float calc_intensity(const cv::Mat &src)
std::string remove_ext(const std::string &filepath)
static InferenceEngine::Blob::Ptr wrapMat2Blob(const cv::Mat &mat)
LOG(INFO)<< "Log message to default logger"
GLdouble GLdouble GLdouble w
GLsizei const GLchar *const * string
GLenum src
Definition: glext.h:1751
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:1960
void onError(char const *msg) noexceptoverride
cv::Rect adjust_face_bbox(cv::Rect const &r, float enlarge_coefficient=1, float dx_coefficient=1, float dy_coefficient=1)
const GLubyte * c
Definition: glext.h:12690
GLdouble GLdouble r
void matU8ToBlob(const cv::Mat &orig_image, InferenceEngine::Blob::Ptr &blob, int batchIndex=0)
GLint GLsizei GLsizei height
std::vector< std::string > read_labels(std::string const &filename)
float rs2_vector::* pos
GLint GLsizei width
void copy(void *dst, void const *src, size_t size)
Definition: types.cpp:836


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:38