JeVoisBase  1.0
JeVois Smart Embedded Machine Vision Toolkit Base Modules
DemoGPU.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/Types/Enum.H>
20 #include <jevois/Debug/Timer.H>
21 
23 
25 #include <linux/videodev2.h>
26 
27 static jevois::ParameterCategory const ParamCateg("DemoGPU Parameters");
28 
29 // Note: we use NoEffect here as None seems to be defined a s anumeric constant somewhere, when compiling on host
30 
31 //! Parameter \relates DemoGPU
32 JEVOIS_DEFINE_ENUM_CLASS(Effect, (NoEffect) (Blur) (Sobel) (Median) (Mult) (Thresh) (Dilate) (Erode) (Twirl) );
33 
34 //! Parameter \relates DemoGPU
35 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(effect, Effect, "GPU image processing effect to apply",
36  Effect::Twirl, Effect_Values, ParamCateg);
37 
38 // icon by by Freepik in technology at flaticon
39 
40 //! Simple image filtering demo using OpenGL-ES 2.0 shaders on the Mali-400MP2 GPU
41 /*! This class first copies the given input image into an OpenGL texture, then applies OpenGL-ES vertex and fragment
42  shaders to achieve some image processing, and finally gets the resulting pixels back into an image.
43 
44  This code is inspired somewhat from the tutorial and code examples found on this page:
45  http://robotblogging.blogspot.com/2013/10/gpu-accelerated-camera-processing-on.html
46 
47  But an important distinction is that we render to a framebuffer with an RGB565 renderbuffer attached, which
48  accelerates processing and transfer of the results a lot.
49 
50  @author Laurent Itti
51 
52  @displayname Demo GPU
53  @videomapping RGB565 320 240 22.0 YUYV 320 240 22.0 JeVois DemoGPU
54  @email itti\@usc.edu
55  @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
56  @copyright Copyright (C) 2016 by Laurent Itti, iLab and the University of Southern California
57  @mainurl http://jevois.org
58  @supporturl http://jevois.org/doc
59  @otherurl http://iLab.usc.edu
60  @license GPL v3
61  @distribution Unrestricted
62  @restrictions None
63  \ingroup modules */
64 class DemoGPU : public jevois::Module,
65  public jevois::Parameter<effect>
66 {
67  public:
68  //! Constrctor
69  DemoGPU(std::string const & instance) : jevois::Module(instance)
70  {
71  itsFilter = addSubComponent<FilterGPU>("gpu");
72  }
73 
74  //! Virtual destructor for safe inheritance
75  virtual ~DemoGPU() { }
76 
77  //! Processing function
78  virtual void process(jevois::InputFrame && inframe, jevois::OutputFrame && outframe) override
79  {
80  static jevois::Timer timer("DemoGPU");
81 
82  // Wait for next available camera image:
83  jevois::RawImage const inimg = inframe.get();
84 
85  timer.start();
86 
87  // Convert input image to RGBA:
88  cv::Mat inimgcv = jevois::rawimage::convertToCvRGBA(inimg);
89 
90  // Let camera know we are done processing the input image:
91  inframe.done();
92 
93  // Wait for an image from our gadget driver into which we will put our results:
94  jevois::RawImage outimg = outframe.get();
95  outimg.require("output", outimg.width, outimg.height, V4L2_PIX_FMT_RGB565);
96  cv::Mat outimgcv = jevois::rawimage::cvImage(outimg);
97 
98  // Process input to output, going from YUYV to RGBA internally, to RGB565 rendering:
99  itsFilter->process(inimgcv, outimgcv);
100 
101  // Show processing fps:
102  std::string const & fpscpu = timer.stop();
103  jevois::rawimage::writeText(outimg, fpscpu, 3, outimg.height - 13, jevois::rgb565::White);
104  std::ostringstream oss; oss << "JeVois DemoGPU - Effect: " << effect::get();
105  jevois::rawimage::writeText(outimg, oss.str(), 3, 3, jevois::rgb565::White);
106 
107  // Send the output image with our processing results to the host over USB:
108  outframe.send();
109  }
110 
111 
112  // ####################################################################################################
113  void onParamChange(effect const & JEVOIS_UNUSED_PARAM(param), Effect const & newval)
114  {
115  switch (newval)
116  {
117  case Effect::NoEffect:
118  itsFilter->setProgram("shaders/simplevertshader.glsl", "shaders/simplefragshader.glsl");
119  break;
120 
121  case Effect::Blur:
122  itsFilter->setProgram("shaders/simplevertshader.glsl", "shaders/blurfragshader.glsl");
123  break;
124 
125  case Effect::Sobel:
126  itsFilter->setProgram("shaders/simplevertshader.glsl", "shaders/sobelfragshader.glsl");
127  break;
128 
129  case Effect::Median:
130  itsFilter->setProgram("shaders/simplevertshader.glsl", "shaders/medianfragshader.glsl");
131  break;
132 
133  case Effect::Mult:
134  itsFilter->setProgram("shaders/simplevertshader.glsl", "shaders/multfragshader.glsl");
135  break;
136 
137  case Effect::Thresh:
138  itsFilter->setProgram("shaders/simplevertshader.glsl", "shaders/threshfragshader.glsl");
139  break;
140 
141  case Effect::Dilate:
142  itsFilter->setProgram("shaders/simplevertshader.glsl", "shaders/dilatefragshader.glsl");
143  break;
144 
145  case Effect::Erode:
146  itsFilter->setProgram("shaders/simplevertshader.glsl", "shaders/erodefragshader.glsl");
147  break;
148 
149  case Effect::Twirl:
150  itsFilter->setProgram("shaders/simplevertshader.glsl", "shaders/twirlfragshader.glsl");
151  itsFilter->setProgramParam1f("twirlamount", 2.0F);
152  break;
153  }
154 
155  // These parameters are used by the vertex shader and hence apply to all demo programs:
156  itsFilter->setProgramParam2f("offset", -1.0F, -1.0F);
157  itsFilter->setProgramParam2f("scale", 2.0F, 2.0F);
158  }
159 
160  private:
161  std::shared_ptr<FilterGPU> itsFilter;
162 };
163 
164 // Allow the module to be loaded as a shared object (.so) file:
cv::Mat convertToCvRGBA(RawImage const &src)
JEVOIS_DEFINE_ENUM_CLASS(Dict,(Original)(D4X4_50)(D4X4_100)(D4X4_250)(D4X4_1000)(D5X5_50)(D5X5_100)(D5X5_250)(D5X5_1000)(D6X6_50)(D6X6_100)(D6X6_250)(D6X6_1000)(D7X7_50)(D7X7_100)(D7X7_250)(D7X7_1000))
Enum for parameter.
cv::Mat cvImage(RawImage const &src)
friend friend class Module
void writeText(RawImage &img, std::string const &txt, int x, int y, unsigned int col, Font font=Font6x10)
void require(char const *info, unsigned int w, unsigned int h, unsigned int f) const
unsigned int height
#define JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(ParamName, ParamType,...)
Parameter.
virtual void process(jevois::InputFrame &&inframe, jevois::OutputFrame &&outframe) override
Processing function.
Definition: DemoGPU.C:78
JEVOIS_REGISTER_MODULE(DemoGPU)
void onParamChange(effect const &JEVOIS_UNUSED_PARAM(param), Effect const &newval)
Definition: DemoGPU.C:113
virtual ~DemoGPU()
Virtual destructor for safe inheritance.
Definition: DemoGPU.C:75
DemoGPU(std::string const &instance)
Constrctor.
Definition: DemoGPU.C:69
Simple image filtering demo using OpenGL-ES 2.0 shaders on the Mali-400MP2 GPU.
Definition: DemoGPU.C:64
unsigned int width
std::string const & stop()