JeVoisBase  1.0
JeVois Smart Embedded Machine Vision Toolkit Base Modules
DemoEyeTracker.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>
21 #include <jevois/Debug/Timer.H>
22 
23 #include <linux/videodev2.h> // for v4l2 pixel types
24 #include <opencv2/imgproc/imgproc.hpp>
25 
26 // icon by Icomoon in medical at flaticon
27 
28 //! Simple demo of an eye-tracker using the openEyes toolkit
29 /*! This demo implements an eye tracker, which is based on detecting the outline of the pupil. Note that the camera has
30  to be very close to the eye for this to work well. To be useful in practice, some sort of prism or tele-lens should
31  be used so that the camera can be out of the field of view. This demo was mainly created as an example of an
32  algorithm that can use a 120 frames/s camera capture mode.
33 
34  The original eye tracking software used here can be found at http://thirtysixthspan.com/openEyes/software.html
35 
36  @author Laurent Itti
37 
38  @videomapping GREY 640 480 30.0 YUYV 640 480 30.0 JeVois DemoEyeTracker
39  @videomapping GREY 320 240 60.0 YUYV 320 240 60.0 JeVois DemoEyeTracker
40  @videomapping GREY 176 144 120.0 YUYV 176 144 120.0 JeVois DemoEyeTracker
41  @email itti\@usc.edu
42  @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
43  @copyright Copyright (C) 2016 by Laurent Itti, iLab and the University of Southern California
44  @mainurl http://jevois.org
45  @supporturl http://jevois.org/doc
46  @otherurl http://iLab.usc.edu
47  @license GPL v3
48  @distribution Unrestricted
49  @restrictions None
50  \ingroup modules */
52 {
53  public:
54 
55  //! Constructor
56  DemoEyeTracker(std::string const & instance) : jevois::Module(instance)
57  { itsEyeTracker = addSubComponent<EyeTracker>("eyetracker"); }
58 
59  //! Virtual destructor for safe inheritance
60  virtual ~DemoEyeTracker() { }
61 
62  //! Processing function
63  virtual void process(jevois::InputFrame && inframe, jevois::OutputFrame && outframe) override
64  {
65  static jevois::Timer timer("processing");
66  static double pupell[5]; // pupil ellipse data
67 
68  // Wait for next available camera image:
69  jevois::RawImage const inimg = inframe.get(true);
70 
71  // Wait for an image from our gadget driver into which we will put our results:
72  jevois::RawImage outimg = outframe.get();
73 
74  // Enforce that the input and output dims match, input should be YUYV, output gray:
75  inimg.require("input", outimg.width, outimg.height, V4L2_PIX_FMT_YUYV);
76  outimg.require("output", inimg.width, inimg.height, V4L2_PIX_FMT_GREY);
77 
78  timer.start();
79 
80  // Convert the input to grayscale, directly into the output's pixel buffer:
81  cv::Mat cvin = jevois::rawimage::cvImage(inimg);
82  cv::Mat cvout = jevois::rawimage::cvImage(outimg);
83  cv::cvtColor(cvin, cvout, CV_YUV2GRAY_YUYV);
84 
85  // Let camera know we are done processing the input image:
86  inframe.done();
87 
88  // Process through the eye tracker, it wil draw a bunch of things into the output image:
89  itsEyeTracker->process(cvout, pupell);
90 
91  // Show processing fps:
92  std::string const & fpscpu = timer.stop();
93  jevois::rawimage::writeText(outimg, fpscpu, 3, outimg.height - 13, 255);
94  jevois::rawimage::writeText(outimg, "JeVois EyeTracker", 3, 3, 255);
95 
96  // Send the output image with our processing results to the host over USB:
97  outframe.send();
98  }
99 
100  protected:
101  std::shared_ptr<EyeTracker> itsEyeTracker;
102 };
103 
104 // Allow the module to be loaded as a shared object (.so) file:
cv::Mat cvImage(RawImage const &src)
friend friend class Module
virtual void process(jevois::InputFrame &&inframe, jevois::OutputFrame &&outframe) override
Processing function.
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
virtual ~DemoEyeTracker()
Virtual destructor for safe inheritance.
DemoEyeTracker(std::string const &instance)
Constructor.
Simple demo of an eye-tracker using the openEyes toolkit.
JEVOIS_REGISTER_MODULE(DemoEyeTracker)
std::shared_ptr< EyeTracker > itsEyeTracker
unsigned int width
std::string const & stop()