JeVoisBase  1.20
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
DNN.C
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 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 #include <jevois/Core/Module.H>
19 #include <jevois/Debug/Timer.H>
21 #include <jevois/DNN/Pipeline.H>
22 
23 // icon from opencv
24 
25 //! Detect and recognize multiple objects in scenes using OpenCV, NPU, TPU, or VPU Deep Neural Nets
26 /*! This module runs a deep neural network using the OpenCV #DNN library. Classification networks try to identify the
27  whole object or scene in the field of view, and return the top scoring object classes. Detection networks analyze a
28  scene and produce a number of bounding boxes around detected objects, together with identity labels and confidence
29  scores for each detected box. Semantic segmentation networks create a pixel-by-pixel mask which assigns a class
30  label to every location in the camera view.
31 
32  To select a network, see parameter \p pipe of component Pipeline.
33 
34  The following keys are used in the JeVois-Pro GUI (\p pipe parameter of Pipeline component):
35 
36  - **OpenCV:** network loaded by OpenCV #DNN framework and running on CPU.
37  - **ORT:** network loaded by ONNX-Runtime framework and running on CPU.
38  - **NPU:** network running native on the JeVois-Pro integrated 5-TOPS NPU (neural processing unit).
39  - **SPU:** network running on the optional 26-TOPS Hailo8 SPU accelerator (stream processing unit).
40  - **TPU:** network running on the optional 4-TOPS Google Coral TPU accelerator (tensor processing unit).
41  - **VPU:** network running on the optional 1-TOPS MyriadX VPU accelerator (vector processing unit).
42  - **NPUX:** network loaded by OpenCV and running on NPU via the TIM-VX OpenCV extension. To run efficiently, network
43  should have been quantized to int8, otherwise some slow CPU-based emulation will occur.
44  - **VPUX:** network optimized for VPU but running on CPU if VPU is not available. Note that VPUX entries are
45  automatically created by scanning all VPU entries and changing their target from Myriad to CPU, if a VPU
46  accelerator is not detected. If a VPU is detected, then VPU models are listed and VPUX ones are not.
47  VPUX emulation runs on the JeVois-Pro CPU using the Arm Compute Library to provide efficient implementation
48  of various network layers and operations.
49 
50  For expected network speed, see \subpage JeVoisProBenchmarks
51 
52  Serial messages
53  ---------------
54 
55  For classification networks, when object classes are found with confidence scores above \p thresh, a message
56  containing up to \p top category:score pairs will be sent per video frame. Exact message format depends on the
57  current \p serstyle setting and is described in \ref UserSerialStyle. For example, when \p serstyle is \b Detail,
58  this module sends:
59 
60  \verbatim
61  DO category:score category:score ... category:score
62  \endverbatim
63 
64  where \a category is a category name (from \p namefile) and \a score is the confidence score from 0.0 to 100.0 that
65  this category was recognized. The pairs are in order of decreasing score.
66 
67  See \ref UserSerialStyle for more on standardized serial messages, and \ref coordhelpers for more info on
68  standardized coordinates.
69 
70  For object detection networks, when detections are found which are above threshold, one message will be sent for
71  each detected object (i.e., for each box that gets drawn when USB outputs are used), using a standardized 2D
72  message:
73  + Serial message type: \b 2D
74  + `id`: the category of the recognized object, followed by ':' and the confidence score in percent
75  + `x`, `y`, or vertices: standardized 2D coordinates of object center or corners
76  + `w`, `h`: standardized object size
77  + `extra`: any number of additional category:score pairs which had an above-threshold score for that box
78 
79  See \ref UserSerialStyle for more on standardized serial messages, and \ref coordhelpers for more info on
80  standardized coordinates.
81 
82  @author Laurent Itti
83 
84  @displayname DNN
85  @videomapping NONE 0 0 0.0 YUYV 640 480 15.0 JeVois DNN
86  @videomapping YUYV 640 498 15.0 YUYV 640 480 15.0 JeVois DNN
87  @email itti\@usc.edu
88  @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
89  @copyright Copyright (C) 2018 by Laurent Itti, iLab and the University of Southern California
90  @mainurl http://jevois.org
91  @supporturl http://jevois.org/doc
92  @otherurl http://iLab.usc.edu
93  @license GPL v3
94  @distribution Unrestricted
95  @restrictions None
96  \ingroup modules */
97 class DNN : public jevois::StdModule
98 {
99  public:
100  // ####################################################################################################
101  //! Constructor
102  // ####################################################################################################
103  DNN(std::string const & instance) : jevois::StdModule(instance)
104  {
105  itsPipeline = addSubComponent<jevois::dnn::Pipeline>("pipeline");
106  }
107 
108  // ####################################################################################################
109  //! Virtual destructor for safe inheritance
110  // ####################################################################################################
111  virtual ~DNN()
112  { }
113 
114  // ####################################################################################################
115  //! Processing function implementation
116  // ####################################################################################################
117  void doprocess(jevois::InputFrame const & inframe, jevois::RawImage * outimg,
118  jevois::OptGUIhelper * helper, bool idle)
119  {
120  // If we have a second (scaled) image, assume this is the one we want to process:
121  jevois::RawImage const inimg = inframe.getp();
122 
123  // Ok, process it:
124  itsPipeline->process(inimg, this, outimg, helper, idle);
125  }
126 
127  // ####################################################################################################
128  //! Processing function, no video output
129  // ####################################################################################################
130  virtual void process(jevois::InputFrame && inframe) override
131  {
132  doprocess(inframe, nullptr, nullptr, false);
133  }
134 
135  // ####################################################################################################
136  //! Processing function with video output to USB on JeVois-A33
137  // ####################################################################################################
138  virtual void process(jevois::InputFrame && inframe, jevois::OutputFrame && outframe) override
139  {
140  // Get the input frame:
141  jevois::RawImage const & inimg = inframe.get();
142  unsigned int const w = inimg.width, h = inimg.height;
143 
144  // Get the output image:
145  jevois::RawImage outimg = outframe.get();
146 
147  // Input and output sizes and format must match:
148  outimg.require("output", w, h, inimg.fmt);
149 
150  // Copy input to output:
151  jevois::rawimage::paste(inimg, outimg, 0, 0);
152 
153  // Process and draw any results (e.g., detected boxes) into outimg:
154  doprocess(inframe, &outimg, nullptr, false);
155 
156  // Send the output image with our processing results to the host over USB:
157  outframe.send();
158  }
159 
160 #ifdef JEVOIS_PRO
161  // ####################################################################################################
162  //! Processing function with zero-copy and GUI on JeVois-Pro
163  // ####################################################################################################
164  virtual void process(jevois::InputFrame && inframe, jevois::GUIhelper & helper) override
165  {
166  // Compute overall frame rate, CPU usage, etc:
167  static jevois::Timer timer("main", 20, LOG_DEBUG);
168  std::string const & fpscpu = timer.stop();
169  timer.start();
170 
171  // Start the display frame: winw, winh will be set by startFrame() to the display size, e.g., 1920x1080
172  unsigned short winw, winh;
173  bool idle = helper.startFrame(winw, winh);
174 
175  // Display the camera input frame: if all zeros, x, y, w, h will be set by drawInputFrame() so as to show the
176  // video frame as large as possible and centered within the display (of size winw,winh)
177  int x = 0, y = 0; unsigned short w = 0, h = 0;
178  helper.drawInputFrame("c", inframe, x, y, w, h, true);
179 
180  // Process and draw any results (e.g., detected boxes) as OpenGL overlay:
181  doprocess(inframe, nullptr, &helper, idle);
182 
183  // Show overall frame rate, CPU, camera resolution, and display resolution, at bottom of screen:
184  helper.iinfo(inframe, fpscpu, winw, winh);
185 
186  // Render the image and GUI:
187  helper.endFrame();
188  }
189 #endif
190 
191  // ####################################################################################################
192  protected:
193  std::shared_ptr<jevois::dnn::Pipeline> itsPipeline;
194 };
195 
196 // Allow the module to be loaded as a shared object (.so) file:
jevois::GUIhelper
Pipeline.H
jevois::OutputFrame
jevois::InputFrame::getp
const RawImage & getp(bool casync=false) const
jevois::GUIhelper::startFrame
bool startFrame(unsigned short &w, unsigned short &h)
Timer.H
Module.H
jevois::GUIhelper::endFrame
void endFrame()
jevois::RawImage
DNN::~DNN
virtual ~DNN()
Virtual destructor for safe inheritance.
Definition: DNN.C:111
DNN::itsPipeline
std::shared_ptr< jevois::dnn::Pipeline > itsPipeline
Definition: DNN.C:193
jevois::RawImage::require
void require(char const *info, unsigned int w, unsigned int h, unsigned int f) const
jevois::GUIhelper
jevois::RawImage::width
unsigned int width
JEVOIS_REGISTER_MODULE
JEVOIS_REGISTER_MODULE(DNN)
jevois
jevois::StdModule::StdModule
StdModule(std::string const &instance)
DNN::process
virtual void process(jevois::InputFrame &&inframe) override
Processing function, no video output.
Definition: DNN.C:130
DNN::DNN
DNN(std::string const &instance)
Constructor.
Definition: DNN.C:103
DNN::doprocess
void doprocess(jevois::InputFrame const &inframe, jevois::RawImage *outimg, jevois::OptGUIhelper *helper, bool idle)
Processing function implementation.
Definition: DNN.C:117
RawImageOps.H
jevois::GUIhelper::drawInputFrame
void drawInputFrame(char const *name, InputFrame const &frame, int &x, int &y, unsigned short &w, unsigned short &h, bool noalias=false, bool casync=false)
jevois::RawImage::height
unsigned int height
jevois::GUIhelper::iinfo
void iinfo(jevois::InputFrame const &inframe, std::string const &fpscpu, unsigned short winw=0, unsigned short winh=0)
jevois::InputFrame
jevois::rawimage::paste
void paste(RawImage const &src, RawImage &dest, int dx, int dy)
jevois::RawImage::fmt
unsigned int fmt
h
int h
DNN::process
virtual void process(jevois::InputFrame &&inframe, jevois::OutputFrame &&outframe) override
Processing function with video output to USB on JeVois-A33.
Definition: DNN.C:138
jevois::StdModule
DNN
Detect and recognize multiple objects in scenes using OpenCV, NPU, TPU, or VPU Deep Neural Nets.
Definition: DNN.C:97
demo.w
w
Definition: demo.py:85
jevois::Timer