JeVoisBase  1.20
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
MultiDNN2.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>
20 #include <jevois/DNN/Pipeline.H>
21 #include <jevois/Util/Utils.H>
22 #include <jevois/Debug/Timer.H>
23 
24 // icon from opencv
25 
26 static jevois::ParameterCategory const ParamCateg("MultiDNN2 Options");
27 
28 //! Parameter \relates MultiDNN2
29 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(num, unsigned int, "Number of networks to run and display",
30  4, jevois::Range<unsigned int>(1, 10), ParamCateg);
31 
32 //! Run multiple neural networks in parallel with an overlapping display
33 /*! See \jvmod{DNN} for more details about the JeVois DNN framework.
34 
35  Edit the module's \b params.cfg file to select which models to run.
36 
37  You can load any model at runtime by setting the \p pipe parameter for each pipeline.
38 
39  @author Laurent Itti
40 
41  @displayname MultiDNN2
42  @videomapping NONE 0 0 0.0 YUYV 640 480 15.0 JeVois MultiDNN2
43  @videomapping YUYV 640 498 15.0 YUYV 640 480 15.0 JeVois MultiDNN2
44  @email itti\@usc.edu
45  @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
46  @copyright Copyright (C) 2018 by Laurent Itti, iLab and the University of Southern California
47  @mainurl http://jevois.org
48  @supporturl http://jevois.org/doc
49  @otherurl http://iLab.usc.edu
50  @license GPL v3
51  @distribution Unrestricted
52  @restrictions None
53  \ingroup modules */
55  public jevois::Parameter<num>
56 {
57  public:
58  // ####################################################################################################
59  //! Constructor
60  // ####################################################################################################
61  using StdModule::StdModule;
62 
63  // ####################################################################################################
64  //! Virtual destructor for safe inheritance
65  // ####################################################################################################
66  virtual ~MultiDNN2()
67  { }
68 
69  // ####################################################################################################
70  //! Processing function implementation
71  // ####################################################################################################
72  void doprocess(std::shared_ptr<jevois::dnn::Pipeline> pipe, jevois::InputFrame const & inframe,
73  jevois::RawImage * outimg, jevois::OptGUIhelper * helper, bool idle)
74  {
75  // If we have a second (scaled) image, assume this is the one we want to process:
76  jevois::RawImage const & inimg = inframe.hasScaledImage() ? inframe.get2() : inframe.get();
77 
78  // Ok, process it:
79  pipe->process(inimg, this, outimg, helper, idle);
80  }
81 
82  // ####################################################################################################
83  //! Processing function, no video output
84  // ####################################################################################################
85  //virtual void process(jevois::InputFrame && inframe) override
86  //{
87  // LFATAL("todo");
88  //}
89 
90  // ####################################################################################################
91  //! Processing function with video output to USB
92  // ####################################################################################################
93  //virtual void process(jevois::InputFrame && inframe, jevois::OutputFrame && outframe) override
94  //{
95  // LFATAL("todo");
96  //}
97 
98 #ifdef JEVOIS_PRO
99  // ####################################################################################################
100  //! Processing function with zero-copy and GUI on JeVois-Pro
101  // ####################################################################################################
102  virtual void process(jevois::InputFrame && inframe, jevois::GUIhelper & helper) override
103  {
104  // Compute overall frame rate, CPU usage, etc:
105  static jevois::Timer timer("main", 20, LOG_DEBUG);
106  std::string const & fpscpu = timer.stop();
107  timer.start();
108 
109  // Start the display frame:
110  unsigned short winw, winh;
111  bool idle = helper.startFrame(winw, winh);
112 
113  // Display the camera input frame:
114  int x = 0, y = 0; unsigned short w = 0, h = 0;
115  helper.drawInputFrame("c", inframe, x, y, w, h, true);
116 
117  // Loop over all pipelines and run and display each:
118  for (auto & pipe : itsPipes) doprocess(pipe, inframe, nullptr, &helper, idle);
119 
120  // Show overall frame rate and camera and display info:
121  helper.iinfo(inframe, fpscpu, winw, winh);
122 
123  // Render the image and GUI:
124  helper.endFrame();
125  }
126 #endif
127 
128  // ####################################################################################################
129  protected:
130  void onParamChange(num const & JEVOIS_UNUSED_PARAM(param), unsigned int const & newval) override
131  {
132  // Try to preserve as many existing pipes as we can:
133  while (itsPipes.size() < newval)
134  itsPipes.emplace_back(addSubComponent<jevois::dnn::Pipeline>("p"+std::to_string(itsPipes.size())));
135 
136  while (newval < itsPipes.size()) { removeSubComponent(itsPipes.back()); itsPipes.pop_back(); }
137  }
138 
139  std::vector<std::shared_ptr<jevois::dnn::Pipeline>> itsPipes;
140 
141 };
142 
143 // Allow the module to be loaded as a shared object (.so) file:
jevois::GUIhelper
Pipeline.H
jevois::Range
jevois::GUIhelper::startFrame
bool startFrame(unsigned short &w, unsigned short &h)
Timer.H
Module.H
jevois::GUIhelper::endFrame
void endFrame()
jevois::RawImage
MultiDNN2::itsPipes
std::vector< std::shared_ptr< jevois::dnn::Pipeline > > itsPipes
Definition: MultiDNN2.C:139
MultiDNN2
Run multiple neural networks in parallel with an overlapping display.
Definition: MultiDNN2.C:54
jevois::Component::removeSubComponent
void removeSubComponent(std::shared_ptr< Comp > &component)
jevois::ParameterCategory
jevois::InputFrame::get
const RawImage & get(bool casync=false) const
jevois::GUIhelper
jevois::InputFrame::hasScaledImage
bool hasScaledImage() const
MultiDNN2::onParamChange
void onParamChange(num const &JEVOIS_UNUSED_PARAM(param), unsigned int const &newval) override
Processing function, no video output.
Definition: MultiDNN2.C:130
MultiDNN2::~MultiDNN2
virtual ~MultiDNN2()
Virtual destructor for safe inheritance.
Definition: MultiDNN2.C:66
jevois::InputFrame::get2
const RawImage & get2(bool casync=false) const
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::GUIhelper::iinfo
void iinfo(jevois::InputFrame const &inframe, std::string const &fpscpu, unsigned short winw=0, unsigned short winh=0)
to_string
std::string to_string(T const &val)
jevois::InputFrame
Utils.H
jevois::dnn::Pipeline::process
void process(jevois::RawImage const &inimg, jevois::StdModule *mod, jevois::RawImage *outimg, jevois::OptGUIhelper *helper, bool idle=false)
JEVOIS_REGISTER_MODULE
JEVOIS_REGISTER_MODULE(MultiDNN2)
MultiDNN2::doprocess
void doprocess(std::shared_ptr< jevois::dnn::Pipeline > pipe, jevois::InputFrame const &inframe, jevois::RawImage *outimg, jevois::OptGUIhelper *helper, bool idle)
Processing function implementation.
Definition: MultiDNN2.C:72
h
int h
jevois::StdModule
Darknet::JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(dataroot, std::string, "Root path for data, config, and weight files. " "If empty, use the module's path.", JEVOIS_SHARE_PATH "/darknet/single", ParamCateg)
Parameter.
demo.w
w
Definition: demo.py:85
jevois::Timer
jevois::Module::process
virtual void process(InputFrame &&inframe, OutputFrame &&outframe)