JeVoisBase  1.5
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
DemoQRcode.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/Debug/Timer.H>
22 #include <opencv2/core/core.hpp>
23 #include <opencv2/imgproc/imgproc.hpp>
24 
25 // icon by Vaadin in shapes at flaticon
26 
27 //! Simple demo of QR-code and barcode detection and decoding using the ZBar library
28 /*! Detect barcodes and QR-codes, and decode their contents.
29 
30  QR-codes (Quick Response Codes) are popular 2D patterns that contain embedded information, such as a string of text,
31  a URL, etc. They basically work like barcodes, coding information into a high-contrast, geometric pattern that is
32  easier to detect and decode by a machine that more conventional human-written text or drawings.
33 
34  One can generate QR-codes containing different kinds of information, for example using online QR-code generators,
35  such as http://www.qr-code-generator.com/
36 
37  JeVois detects and decodes QR-codes and other barcodes. The implementation of the detection and decoding algorithm
38  used in JeVois is from the popular library ZBar, found at http://zbar.sourceforge.net/
39 
40  You should be able to sustain 30 frames/s with camera resolution 320x240, and 15 frames/s with camera resolution
41  640x480 when running this module inside the JeVois smart camera.
42 
43  Serial Messages
44  ---------------
45 
46  This module can send standardized serial messages as described in \ref UserSerialStyle. One message is issued for
47  every detected QR-code or barcode, on every video frame.
48 
49  - Serial message type: \b 2D
50  - `id`: type of symbol (e.g., \b QR-Code, \b ISBN13, etc).
51  - `x`, `y`, or vertices: standardized 2D coordinates of symbol center or of corners of bounding box
52  (depending on \p serstyle)
53  - `w`, `h`: standardized object size
54  - `extra`: decoded contents (e.g., URL that was in a QR-code, ISBN number from a barcode, etc)
55 
56  Note that when \p serstyle is \b Fine, only 4 corners are returned for each detected QR-code, but many points are
57  returned all over each detected barcode. Beware to not exceed your serial bandwidth in that case.
58 
59  @author Laurent Itti
60 
61  @displayname Demo QR-code
62  @videomapping YUYV 640 526 15.0 YUYV 640 480 15.0 JeVois DemoQRcode
63  @videomapping YUYV 320 286 30.0 YUYV 320 240 30.0 JeVois DemoQRcode
64  @videomapping NONE 0 0 0 YUYV 640 480 15.0 JeVois DemoQRcode
65  @videomapping NONE 0 0 0 YUYV 320 240 30.0 JeVois DemoQRcode
66  @email itti\@usc.edu
67  @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
68  @copyright Copyright (C) 2016 by Laurent Itti, iLab and the University of Southern California
69  @mainurl http://jevois.org
70  @supporturl http://jevois.org/doc
71  @otherurl http://iLab.usc.edu
72  @license GPL v3
73  @distribution Unrestricted
74  @restrictions None
75  \ingroup modules */
77 {
78  public:
79 
80  // ####################################################################################################
81  //! Constructor
82  // ####################################################################################################
83  DemoQRcode(std::string const & instance) : jevois::StdModule(instance)
84  { itsQRcode = addSubComponent<QRcode>("qrcode"); }
85 
86  // ####################################################################################################
87  //! Virtual destructor for safe inheritance
88  // ####################################################################################################
89  virtual ~DemoQRcode()
90  { }
91 
92  // ####################################################################################################
93  //! Processing function, no video output
94  // ####################################################################################################
95  virtual void process(jevois::InputFrame && inframe) override
96  {
97  // Wait for next available camera image:
98  jevois::RawImage const inimg = inframe.get();
99  unsigned int const w = inimg.width, h = inimg.height;
100 
101  // Convert the image to grayscale:
102  cv::Mat grayimg = jevois::rawimage::convertToCvGray(inimg);
103 
104  // Let camera know we are done processing the input image:
105  inframe.done();
106 
107  // Process gray image through zbar:
108  zbar::Image zgray(grayimg.cols, grayimg.rows, "Y800", grayimg.data, grayimg.total());
109  itsQRcode->process(zgray);
110 
111  // Send all the results over serial:
112  itsQRcode->sendSerial(this, zgray, w, h);
113 
114  // Cleanup zbar image data:
115  zgray.set_data(nullptr, 0);
116  }
117 
118  // ####################################################################################################
119  //! Processing function with video output to USB
120  // ####################################################################################################
121  virtual void process(jevois::InputFrame && inframe, jevois::OutputFrame && outframe) override
122  {
123  static jevois::Timer timer("processing", 60, LOG_DEBUG);
124  static unsigned short const txtcol = jevois::yuyv::White;
125  size_t const nshow = 4; // number of lines to show
126 
127  // Wait for next available camera image:
128  jevois::RawImage const inimg = inframe.get();
129 
130  timer.start();
131 
132  // We only handle one specific pixel format, any size in this demo:
133  unsigned int const w = inimg.width, h = inimg.height;
134  inimg.require("input", w, h, V4L2_PIX_FMT_YUYV);
135 
136  // While we process it, start a thread to wait for out frame and paste the input into it:
137  jevois::RawImage outimg;
138  auto paste_fut = std::async(std::launch::async, [&]() {
139  outimg = outframe.get();
140  outimg.require("output", w, h + nshow * 10 + 6, inimg.fmt);
141  jevois::rawimage::paste(inimg, outimg, 0, 0);
142  jevois::rawimage::writeText(outimg, "JeVois QR-code + Barcode Detection Demo", 3, 3, txtcol);
143  jevois::rawimage::drawFilledRect(outimg, 0, h, w, outimg.height-h, 0x8000);
144  });
145 
146  // Convert the image to grayscale and process it through zbar:
147  cv::Mat grayimg = jevois::rawimage::convertToCvGray(inimg);
148  zbar::Image zgray(grayimg.cols, grayimg.rows, "Y800", grayimg.data, grayimg.total());
149  itsQRcode->process(zgray);
150 
151  // Wait for paste to finish up:
152  paste_fut.get();
153 
154  // Let camera know we are done processing the input image:
155  inframe.done();
156 
157  // Draw all detections:
158  itsQRcode->drawDetections(outimg, 3, h + 3, zgray, w, h, nshow);
159 
160  // Send all serial messages:
161  itsQRcode->sendSerial(this, zgray, w, h);
162 
163  // Cleanup zbar image data:
164  zgray.set_data(nullptr, 0);
165 
166  // Show processing fps:
167  std::string const & fpscpu = timer.stop();
168  jevois::rawimage::writeText(outimg, fpscpu, 3, h - 13, txtcol);
169 
170  // Send the output image with our processing results to the host over USB:
171  outframe.send();
172  }
173 
174  // ####################################################################################################
175  protected:
176  std::shared_ptr<QRcode> itsQRcode;
177 };
178 
179 // Allow the module to be loaded as a shared object (.so) file:
DemoQRcode(std::string const &instance)
Constructor.
Definition: DemoQRcode.C:83
virtual void process(jevois::InputFrame &&inframe, jevois::OutputFrame &&outframe) override
Processing function with video output to USB.
Definition: DemoQRcode.C:121
void writeText(RawImage &img, std::string const &txt, int x, int y, unsigned int col, Font font=Font6x10)
unsigned int height
Simple demo of QR-code and barcode detection and decoding using the ZBar library. ...
Definition: DemoQRcode.C:76
virtual void process(jevois::InputFrame &&inframe) override
Processing function, no video output.
Definition: DemoQRcode.C:95
unsigned int fmt
virtual ~DemoQRcode()
Virtual destructor for safe inheritance.
Definition: DemoQRcode.C:89
JEVOIS_REGISTER_MODULE(DemoQRcode)
StdModule(std::string const &instance)
cv::Mat convertToCvGray(RawImage const &src)
std::string const & stop()
void drawFilledRect(RawImage &img, int x, int y, unsigned int w, unsigned int h, unsigned int col)
std::shared_ptr< QRcode > itsQRcode
Definition: DemoQRcode.C:176
unsigned int width
void paste(RawImage const &src, RawImage &dest, int dx, int dy)
void require(char const *info, unsigned int w, unsigned int h, unsigned int f) const