JeVois  1.2
JeVois Smart Embedded Machine Vision Toolkit
Module.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/Core/VideoInput.H>
21 #include <jevois/Core/Engine.H>
24 
25 // ####################################################################################################
26 jevois::InputFrame::InputFrame(std::shared_ptr<jevois::VideoInput> const & cam, bool turbo) :
27  itsCamera(cam), itsDidGet(false), itsDidDone(false), itsTurbo(turbo)
28 { }
29 
30 // ####################################################################################################
32 {
33  // If itsCamera is invalidated, we have been moved to another object, so do not do anything here:
34  if (itsCamera.get() == nullptr) return;
35 
36  // If we did not yet get(), just end now, camera will drop this frame:
37  if (itsDidGet == false) return;
38 
39  // If we did get() but not done(), signal done now:
40  if (itsDidDone == false) try { itsCamera->done(itsImage); } catch (...) { }
41 }
42 
43 // ####################################################################################################
44 jevois::RawImage const & jevois::InputFrame::get(bool casync) const
45 {
46  itsCamera->get(itsImage);
47  itsDidGet = true;
48  if (casync && itsTurbo) itsImage.buf->sync();
49  return itsImage;
50 }
51 
52 // ####################################################################################################
54 {
55  itsCamera->done(itsImage);
56  itsDidDone = true;
57 }
58 
59 // ####################################################################################################
60 cv::Mat jevois::InputFrame::getCvGRAY(bool casync) const
61 {
62  jevois::RawImage const & rawimg = get(casync);
63  cv::Mat cvimg = jevois::rawimage::convertToCvGray(rawimg);
64  done();
65  return cvimg;
66 }
67 
68 // ####################################################################################################
69 cv::Mat jevois::InputFrame::getCvBGR(bool casync) const
70 {
71  jevois::RawImage const & rawimg = get(casync);
72  cv::Mat cvimg = jevois::rawimage::convertToCvBGR(rawimg);
73  done();
74  return cvimg;
75 }
76 
77 // ####################################################################################################
78 cv::Mat jevois::InputFrame::getCvRGB(bool casync) const
79 {
80  jevois::RawImage const & rawimg = get(casync);
81  cv::Mat cvimg = jevois::rawimage::convertToCvRGB(rawimg);
82  done();
83  return cvimg;
84 }
85 
86 // ####################################################################################################
87 cv::Mat jevois::InputFrame::getCvRGBA(bool casync) const
88 {
89  jevois::RawImage const & rawimg = get(casync);
90  cv::Mat cvimg = jevois::rawimage::convertToCvRGBA(rawimg);
91  done();
92  return cvimg;
93 }
94 
95 // ####################################################################################################
96 // ####################################################################################################
97 jevois::OutputFrame::OutputFrame(std::shared_ptr<jevois::VideoOutput> const & gad, jevois::RawImage * excimg) :
98  itsGadget(gad), itsDidGet(false), itsDidSend(false), itsImagePtrForException(excimg)
99 { }
100 
101 // ####################################################################################################
103 {
104  // If itsGadget is invalidated, we have been moved to another object, so do not do anything here:
105  if (itsGadget.get() == nullptr) return;
106 
107  // If we did not get(), just end now:
108  if (itsDidGet == false) return;
109 
110  // If Engine gave us a non-zero image pointer for exceptions, and we did get(), pass down the buffer we did get, so
111  // that Engine can write exception text into it, unless it is too late (exception occurred after send()) or too early
112  // (before get()), in which case Engine will get its own buffer:
113  if (itsImagePtrForException)
114  {
115  if (itsDidSend == false) { *itsImagePtrForException = itsImage; }
116  // Engine will be responsible for the final send()
117  }
118  else
119  {
120  // If we did get() but not send(), send now (the image will likely contain garbage):
121  if (itsDidSend == false) try { itsGadget->send(itsImage); } catch (...) { }
122  }
123 }
124 
125 // ####################################################################################################
127 {
128  itsGadget->get(itsImage);
129  itsDidGet = true;
130  return itsImage;
131 }
132 
133 // ####################################################################################################
135 {
136  itsGadget->send(itsImage);
137  itsDidSend = true;
138  if (itsImagePtrForException) itsImagePtrForException->invalidate();
139 }
140 
141 // ####################################################################################################
142 void jevois::OutputFrame::sendCvGRAY(cv::Mat const & img, int quality) const
143 {
144  jevois::RawImage rawimg = get();
145  jevois::rawimage::convertCvGRAYtoRawImage(img, rawimg, quality);
146  send();
147 }
148 
149 // ####################################################################################################
150 void jevois::OutputFrame::sendCvBGR(cv::Mat const & img, int quality) const
151 {
152  jevois::RawImage rawimg = get();
153  jevois::rawimage::convertCvBGRtoRawImage(img, rawimg, quality);
154  send();
155 }
156 // ####################################################################################################
157 void jevois::OutputFrame::sendCvRGB(cv::Mat const & img, int quality) const
158 {
159  jevois::RawImage rawimg = get();
160  jevois::rawimage::convertCvRGBtoRawImage(img, rawimg, quality);
161  send();
162 }
163 
164 // ####################################################################################################
165 void jevois::OutputFrame::sendCvRGBA(cv::Mat const & img, int quality) const
166 {
167  jevois::RawImage rawimg = get();
168  jevois::rawimage::convertCvRGBAtoRawImage(img, rawimg, quality);
169  send();
170 }
171 
172 // ####################################################################################################
173 // ####################################################################################################
174 jevois::Module::Module(std::string const & instance) :
175  jevois::Component(instance)
176 { }
177 
178 // ####################################################################################################
180 { }
181 
182 // ####################################################################################################
183 void jevois::Module::process(InputFrame && JEVOIS_UNUSED_PARAM(inframe), OutputFrame && JEVOIS_UNUSED_PARAM(outframe))
184 { LFATAL("Not implemented in this module"); }
185 
186 // ####################################################################################################
187 void jevois::Module::process(InputFrame && JEVOIS_UNUSED_PARAM(inframe))
188 { LFATAL("Not implemented in this module"); }
189 
190 // ####################################################################################################
191 void jevois::Module::sendSerial(std::string const & str)
192 {
193  jevois::Engine * e = dynamic_cast<jevois::Engine *>(itsParent);
194  if (e == nullptr) LFATAL("My parent is not Engine -- CANNOT SEND SERIAL");
195 
196  e->sendSerial(str);
197 }
198 
199 // ####################################################################################################
200 void jevois::Module::parseSerial(std::string const & str,
201  std::shared_ptr<jevois::UserInterface> JEVOIS_UNUSED_PARAM(s))
202 { throw std::runtime_error("Unsupported command [" + str + ']'); }
203 
204 // ####################################################################################################
205 void jevois::Module::supportedCommands(std::ostream & os)
206 { os << "None" << std::endl; }
207 
void done() const
Indicate that user processing is done with the image previously obtained via get() ...
Definition: Module.C:53
void sendCvGRAY(cv::Mat const &img, int quality=75) const
Shorthand to send a GRAY cv::Mat after converting it to the current output format.
Definition: Module.C:142
RawImage const & get(bool casync=false) const
Get the next captured camera image.
Definition: Module.C:44
cv::Mat convertToCvRGB(RawImage const &src)
Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV RGB byte...
Definition: RawImageOps.C:249
cv::Mat convertToCvRGBA(RawImage const &src)
Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV RGB-A byte...
Definition: RawImageOps.C:272
Exception-safe wrapper around a raw camera input frame.
Definition: Module.H:55
cv::Mat convertToCvBGR(RawImage const &src)
Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV BGR byte...
Definition: RawImageOps.C:226
RawImage const & get() const
Get a pre-allocated image so that we can fill the pixel data and later send out over USB using send()...
Definition: Module.C:126
cv::Mat getCvGRAY(bool casync=false) const
Shorthand to get the input image as a GRAY cv::Mat and release the raw buffer.
Definition: Module.C:60
std::shared_ptr< VideoBuf > buf
The pixel data buffer.
Definition: RawImage.H:149
Module(std::string const &instance)
Constructor.
Definition: Module.C:174
InputFrame(InputFrame &&other)=default
Move constructor.
virtual void supportedCommands(std::ostream &os)
Human-readable description of this Module's supported custom commands.
Definition: Module.C:205
void sendSerial(std::string const &str, bool islog=false)
Send a string to all serial ports.
Definition: Engine.C:825
A component of a model hierarchy.
Definition: Component.H:176
virtual void process(InputFrame &&inframe, OutputFrame &&outframe)
Processing function, version that receives a frame from camera and sends a frame out over USB...
A raw image as coming from a V4L2 Camera and/or being sent out to a USB Gadget.
Definition: RawImage.H:110
void sendCvRGBA(cv::Mat const &img, int quality=75) const
Shorthand to send a RGBA cv::Mat after converting it to the current output format.
Definition: Module.C:165
void sendCvRGB(cv::Mat const &img, int quality=75) const
Shorthand to send a RGB cv::Mat after converting it to the current output format. ...
Definition: Module.C:157
cv::Mat getCvRGBA(bool casync=false) const
Shorthand to get the input image as a RGBA cv::Mat and release the raw buffer.
Definition: Module.C:87
void send() const
Send an image out over USB to the host computer.
Definition: Module.C:134
cv::Mat convertToCvGray(RawImage const &src)
Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV gray byte...
Definition: RawImageOps.C:203
void convertCvBGRtoRawImage(cv::Mat const &src, RawImage &dst, int quality)
Convert a BGR cv::Mat to RawImage with already-allocated pixels and pixel type.
Definition: RawImageOps.C:1061
virtual void sendSerial(std::string const &str)
Send a string over the 'serout' serial port.
Definition: Module.C:191
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level. ...
Definition: Log.H:212
void sendCvBGR(cv::Mat const &img, int quality=75) const
Shorthand to send a BGR cv::Mat after converting it to the current output format. ...
Definition: Module.C:150
cv::Mat getCvRGB(bool casync=false) const
Shorthand to get the input image as a RGB cv::Mat and release the raw buffer.
Definition: Module.C:78
void convertCvRGBAtoRawImage(cv::Mat const &src, RawImage &dst, int quality)
Convert an RGBA cv::Mat to RawImage with already-allocated pixels and pixel type. ...
Definition: RawImageOps.C:1127
JeVois processing engine - gets images from camera sensor, processes them, and sends results over USB...
Definition: Engine.H:229
void convertCvGRAYtoRawImage(cv::Mat const &src, RawImage &dst, int quality)
Convert a Gray cv::Mat to RawImage with already-allocated pixels and pixel type.
Definition: RawImageOps.C:1147
virtual void parseSerial(std::string const &str, std::shared_ptr< UserInterface > s)
Receive a string from a serial port which contains a user command.
Definition: Module.C:200
Exception-safe wrapper around a raw image to be sent over USB.
Definition: Module.H:143
~InputFrame()
Destructor, returns the buffers to the driver as needed.
Definition: Module.C:31
cv::Mat getCvBGR(bool casync=false) const
Shorthand to get the input image as a BGR cv::Mat and release the raw buffer.
Definition: Module.C:69
~OutputFrame()
Destructor, returns the buffers to the driver as needed.
Definition: Module.C:102
virtual ~Module()
Virtual destructor for safe inheritance.
Definition: Module.C:179
void convertCvRGBtoRawImage(cv::Mat const &src, RawImage &dst, int quality)
Convert a RGB cv::Mat to RawImage with already-allocated pixels and pixel type.
Definition: RawImageOps.C:1082