JeVois  1.20
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
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>
20 #include <jevois/Debug/Timer.H>
21 
22 //! JeVois sample module
23 /*! This module is provided as an example of how to create a new standalone module.
24 
25  By default, we get the next video frame from the camera as an OpenCV BGR (color) image named 'inimg'.
26  We then apply some image processing to it to create an overlay in Pro/GUI mode, an output BGR image named
27  'outimg' in Legacy mode, or no image in Headless mode.
28 
29  - In Legacy mode (JeVois-A33 or JeVois-Pro acts as a webcam connected to a host): process(inframe, outframe) is
30  called on every frame. A video frame from the camera sensor is given in 'inframe' and the process() function
31  create an output frame that is sent over USB to the host computer (JeVois-A33) or displayed (JeVois-Pro).
32 
33  - In Pro/GUI mode (JeVois-Pro is connected to an HDMI display): process(inframe, helper) is called on every frame. A
34  video frame from the camera is given, as well as a GUI helper that can be used to create overlay drawings.
35 
36  - In Headless mode (JeVois-A33 or JeVois-Pro only produces text messages over serial port, no video output):
37  process(inframe) is called on every frame. A video frame from the camera is given, and the module sends messages
38  over serial to report what it sees.
39 
40  Which mode is activated depends on which VideoMapping was selected by the user. The VideoMapping specifies camera
41  format and framerate, and what kind of mode and output format to use.
42 
43  See http://jevois.org/tutorials for tutorials on getting started with programming JeVois in Python without having
44  to install any development software on your host computer.
45 
46  @author __AUTHOR__
47 
48  @videomapping __VIDEOMAPPING__
49  @email __EMAIL__
50  @address fixme
51  @copyright Copyright (C) 2023 by __AUTHOR__
52  @mainurl __WEBSITE__
53  @supporturl
54  @otherurl
55  @license __LICENSE__
56  @distribution Unrestricted
57  @restrictions None
58  @ingroup modules */
60 {
61  public:
62  //! Default base class constructor ok
64 
65  //! Virtual destructor for safe inheritance
66  virtual ~__MODULE__() { }
67 
68  // ####################################################################################################
69  //! Processing function
70  // ####################################################################################################
71  virtual void process(jevois::InputFrame && inframe, jevois::OutputFrame && outframe) override
72  {
73  // Wait for next available camera image:
74  jevois::RawImage const inimg = inframe.get(true);
75 
76  // We only support YUYV pixels in this example, any resolution:
77  inimg.require("input", inimg.width, inimg.height, V4L2_PIX_FMT_YUYV);
78 
79  // Wait for an image from our gadget driver into which we will put our results:
80  jevois::RawImage outimg = outframe.get();
81 
82  // Enforce that the input and output formats and image sizes match:
83  outimg.require("output", inimg.width, inimg.height, inimg.fmt);
84 
85  // Just copy the pixel data over:
86  memcpy(outimg.pixelsw<void>(), inimg.pixels<void>(), std::min(inimg.buf->length(), outimg.buf->length()));
87 
88  // Print a text message:
89  jevois::rawimage::writeText(outimg, "Hello JeVois!", 100, 230, jevois::yuyv::White, jevois::rawimage::Font20x38);
90 
91  // Let camera know we are done processing the input image:
92  inframe.done(); // NOTE: optional here, inframe destructor would call it anyway
93 
94  // Send the output image with our processing results to the host over USB:
95  outframe.send(); // NOTE: optional here, outframe destructor would call it anyway
96  }
97 
98  // ####################################################################################################
99  //! Processing function with no USB output (headless)
100  // ####################################################################################################
101  virtual void process(jevois::InputFrame && inframe) override
102  {
103  // Wait for next available camera image:
104  jevois::RawImage const inimg = inframe.get(true);
105 
106  // Do something with the frame...
107 
108  // ...
109 
110  // Let camera know we are done processing the input image:
111  inframe.done(); // NOTE: optional here, inframe destructor would call it anyway
112 
113  // Send some results to serial port:
114  sendSerial("This is an example -- fixme");
115  }
116 
117 #ifdef JEVOIS_PRO
118  // ####################################################################################################
119  //! Processing function with zero-copy and GUI on JeVois-Pro
120  // ####################################################################################################
121  virtual void process(jevois::InputFrame && inframe, jevois::GUIhelper & helper) override
122  {
123  static jevois::Timer timer("processing", 100, LOG_DEBUG);
124 
125  // Start the GUI frame: will return display size in winw, winh, and whether mouse/keyboard are idle
126  unsigned short winw, winh;
127  bool idle = helper.startFrame(winw, winh);
128 
129  // Draw the camera frame: will return its location (x,y) and size (iw,ih) on screen
130  int x = 0, y = 0; unsigned short iw = 0, ih = 0;
131  helper.drawInputFrame("camera", inframe, x, y, iw, ih);
132 
133  // Wait for next available camera image to be used for processing (possibly at a lower resolution):
134  jevois::RawImage const inimg = inframe.getp();
135  helper.itext("JeVois-Pro Sample module");
136 
137  timer.start();
138 
139  // Process inimg and draw some results in the GUI, possibly send some serial messages too...
140 
141  // ...
142 
143  // Show processing fps:
144  std::string const & fpscpu = timer.stop();
145  helper.iinfo(inframe, fpscpu, winw, winh);
146 
147  // Render the image and GUI:
148  helper.endFrame();
149 
150  (void)idle; // avoid compiler warning since we did not use idle in this example
151  }
152 
153 #endif // JEVOIS_PRO
154 };
155 
156 // Allow the module to be loaded as a shared object (.so) file:
jevois::RawImage::pixels
const T * pixels() const
Shortcut access to pixels, read-only.
jevois::OutputFrame
Exception-safe wrapper around a raw image to be sent over USB.
Definition: OutputFrame.H:52
jevois::rawimage::Font20x38
@ Font20x38
Definition: RawImageOps.H:165
Module.H
jevois::GUIhelper::startFrame
bool startFrame(unsigned short &w, unsigned short &h)
Start a new rendering frame.
Definition: GUIhelper.C:169
__MODULE__
JeVois sample module.
Definition: Module.C:59
jevois::GUIhelper::itext
void itext(char const *txt, ImU32 const &col=IM_COL32_BLACK_TRANS, int line=-1)
Draw some overlay text on top of an image.
Definition: GUIhelper.C:571
RawImageOps.H
jevois::GUIhelper::endFrame
void endFrame()
Finish current frame and render it.
Definition: GUIhelper.C:693
__MODULE__::process
virtual void process(jevois::InputFrame &&inframe) override
Processing function with no USB output (headless)
Definition: Module.C:101
jevois::RawImage
A raw image as coming from a V4L2 Camera and/or being sent out to a USB Gadget.
Definition: RawImage.H:110
jevois::Timer::start
void start()
Start a time measurement period.
Definition: Timer.C:40
jevois::RawImage::require
void require(char const *info, unsigned int w, unsigned int h, unsigned int f) const
Require a particular image size and format, issue a fatal error message and throw if no match.
Definition: RawImage.C:80
jevois::GUIhelper
Helper class to assist modules in creating graphical and GUI elements.
Definition: GUIhelper.H:128
jevois::RawImage::width
unsigned int width
Image width in pixels.
Definition: RawImage.H:145
jevois::RawImage::pixelsw
T * pixelsw()
Shortcut access to pixels, read-write.
jevois::rawimage::writeText
void writeText(RawImage &img, std::string const &txt, int x, int y, unsigned int col, Font font=Font6x10)
Write some text in an image.
Definition: RawImageOps.C:689
__MODULE__::process
virtual void process(jevois::InputFrame &&inframe, jevois::GUIhelper &helper) override
Processing function with zero-copy and GUI on JeVois-Pro.
Definition: Module.C:121
Timer.H
jevois::Timer::stop
const std::string & stop(double *seconds)
End a time measurement period, report time spent if reporting interval is reached.
Definition: Timer.C:47
JEVOIS_REGISTER_MODULE
JEVOIS_REGISTER_MODULE(__MODULE__)
jevois::StdModule::StdModule
StdModule(std::string const &instance)
Constructor.
Definition: Module.C:73
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)
Draw the input video frame from the camera using zero-copy.
Definition: GUIhelper.C:335
jevois::RawImage::height
unsigned int height
Image height in pixels.
Definition: RawImage.H:146
__MODULE__::process
virtual void process(jevois::InputFrame &&inframe, jevois::OutputFrame &&outframe) override
Processing function.
Definition: Module.C:71
jevois::GUIhelper::iinfo
void iinfo(jevois::InputFrame const &inframe, std::string const &fpscpu, unsigned short winw=0, unsigned short winh=0)
Display processing and video info at bottom of screen.
Definition: GUIhelper.C:584
jevois::InputFrame
Exception-safe wrapper around a raw camera input frame.
Definition: InputFrame.H:50
jevois::RawImage::fmt
unsigned int fmt
Pixel format as a V4L2_PIX_FMT_XXX.
Definition: RawImage.H:147
__MODULE__::~__MODULE__
virtual ~__MODULE__()
Virtual destructor for safe inheritance.
Definition: Module.C:66
jevois::RawImage::buf
std::shared_ptr< VideoBuf > buf
The pixel data buffer.
Definition: RawImage.H:149
jevois::Module::sendSerial
virtual void sendSerial(std::string const &str)
Send a string over the 'serout' serial port.
Definition: Module.C:54
jevois::StdModule
Base class for a module that supports standardized serial messages.
Definition: Module.H:232
jevois::Timer
Simple timer class.
Definition: Timer.H:34