JeVois  1.17
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Utils.H
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2020 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 #pragma once
19 
20 #include <map>
21 #include <string>
22 #include <opencv2/core/core.hpp>
23 #include <tensorflow/lite/c/common.h> // for TfLiteType
24 #include <ovxlib/vsi_nn_pub.h> // for data types and quantization types
25 
26 namespace jevois
27 {
28  namespace dnn
29  {
30  /*! \defgroup dnn Tensor/Neural Processing networks
31 
32  Classes and utilities to provide abstraction to deep neural networks. Provides interfacing to OpenCV backends
33  (CPU, OpenCL), tensor processing units (TPU) such as Coral Edge TPU and neural processing units (NPU) such as
34  Amlogic A311D NPU. Supported on JeVois-Pro only.
35 
36  \ingroup core */
37 
38  /*! @{ */ // **********************************************************************
39 
40  //! Read a label file
41  /*! Two formats are allowed: one class name per line, or one class number followed by one class name per file.*/
42  std::map<int, std::string> readLabelsFile(std::string const & fname);
43 
44  //! Get a label from an id
45  /*! If no entry is found in the map, return the id as a string. */
46  std::string getLabel(std::map<int, std::string> const & labels, int id);
47 
48  //! Compute a color from a label name
49  int stringToRGBA(std::string const & label, unsigned char alpha = 128);
50 
51  //! Get top-k entries and their indices
52  void topK(float const * pfProb, float * pfMaxProb, uint32_t * pMaxClass, uint32_t outputCount, uint32_t topNum);
53 
54  //! Get a string of the form: "nD AxBxC... TYPE" from an n-dimensional cv::Mat with data type TYPE
55  std::string shapestr(cv::Mat const & m);
56 
57  //! Get a string of the form: "nD AxBxC... TYPE" from an n-dimensional TfLiteTensor with data type TYPE
58  std::string shapestr(TfLiteTensor const * t);
59 
60  //! Get a string of the form: "nD AxBxC... TYPE" from an n-dimensional NPU tensor with data type TYPE
61  std::string shapestr(vsi_nn_tensor_attr_t const & attr);
62 
63  //! Get a vector of size_t from a string containing AxBxC...
64  std::vector<size_t> strshape(std::string const & str);
65 
66  //! Convert from TensorFlow data type to OpenCV
67  int tf2cv(TfLiteType t);
68 
69  //! Convert from TensorFlow data type to vsi_nn
70  vsi_nn_type_e tf2vsi(TfLiteType t);
71 
72  //! Convert from NPU data type to OpenCV
73  int vsi2cv(vsi_nn_type_e t);
74 
75  //! Clamp a rectangle to within given image width and height
76  void clamp(cv::Rect & r, int width, int height);
77 
78  //! Parse tensor specification
79  /*! If the specification is empty, an empty vector is returned. Throws std::range_error on any parsing error. */
80  std::vector<vsi_nn_tensor_attr_t> parseTensorSpecs(std::string const & specs);
81 
82  //! Get a tensor's size in cv::Size format
83  cv::Size attrsize(vsi_nn_tensor_attr_t const & attr);
84 
85  //! Get a string describing the specs of a tensor
86  std::string attrstr(vsi_nn_tensor_attr_t const & attr);
87 
88  //! Get tensor shape and type attributes for a TensorFlow Lite tensor
89  vsi_nn_tensor_attr_t tensorattr(TfLiteTensor const * t);
90 
91  //! Apply softmax to a float vector
92  void softmax(float const * input, size_t n, float fac, float * output);
93 
94  /*! @} */ // **********************************************************************
95 
96  } // namespace dnn
97 } // namespace jevois
jevois::dnn::clamp
void clamp(cv::Rect &r, int width, int height)
Clamp a rectangle to within given image width and height.
Definition: Utils.C:250
jevois::dnn::tf2cv
int tf2cv(TfLiteType t)
Convert from TensorFlow data type to OpenCV.
Definition: Utils.C:182
jevois::dnn::vsi2cv
int vsi2cv(vsi_nn_type_e t)
Convert from NPU data type to OpenCV.
Definition: Utils.C:205
jevois::dnn::tensorattr
vsi_nn_tensor_attr_t tensorattr(TfLiteTensor const *t)
Get tensor shape and type attributes for a TensorFlow Lite tensor.
Definition: Utils.C:410
jevois::dnn::stringToRGBA
int stringToRGBA(std::string const &label, unsigned char alpha=128)
Compute a color from a label name.
Definition: Utils.C:76
jevois::dnn::readLabelsFile
std::map< int, std::string > readLabelsFile(std::string const &fname)
Read a label file.
Definition: Utils.C:25
jevois::dnn::parseTensorSpecs
std::vector< vsi_nn_tensor_attr_t > parseTensorSpecs(std::string const &specs)
Parse tensor specification.
Definition: Utils.C:260
jevois::dnn::shapestr
std::string shapestr(cv::Mat const &m)
Get a string of the form: "nD AxBxC... TYPE" from an n-dimensional cv::Mat with data type TYPE.
Definition: Utils.C:105
jevois
Definition: Concepts.dox:1
jevois::dnn::getLabel
std::string getLabel(std::map< int, std::string > const &labels, int id)
Get a label from an id.
Definition: Utils.C:68
jevois::dnn::strshape
std::vector< size_t > strshape(std::string const &str)
Get a vector of size_t from a string containing AxBxC...
Definition: Utils.C:173
jevois::dnn::attrsize
cv::Size attrsize(vsi_nn_tensor_attr_t const &attr)
Get a tensor's size in cv::Size format.
Definition: Utils.C:344
jevois::dnn::softmax
void softmax(float const *input, size_t n, float fac, float *output)
Apply softmax to a float vector.
Definition: Utils.C:443
jevois::dnn::attrstr
std::string attrstr(vsi_nn_tensor_attr_t const &attr)
Get a string describing the specs of a tensor.
Definition: Utils.C:360
jevois::dnn::tf2vsi
vsi_nn_type_e tf2vsi(TfLiteType t)
Convert from TensorFlow data type to vsi_nn.
Definition: Utils.C:227
jevois::dnn::topK
void topK(float const *pfProb, float *pfMaxProb, uint32_t *pMaxClass, uint32_t outputCount, uint32_t topNum)
Get top-k entries and their indices.
Definition: Utils.C:85