JeVoisBase  1.3
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 QRcode 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  Serial Messages
41  ---------------
42 
43  This module can send standardized serial messages as described in \ref UserSerialStyle. One message is issued for
44  every detected QR-code or barcode, on every video frame.
45 
46  - Serial message type: \b 2D
47  - `id`: type of symbol (e.g., \b QR-Code, \b ISBN13, etc).
48  - `x`, `y`, or vertices: standardized 2D coordinates of symbol center or of corners of bounding box
49  (depending on \p serstyle)
50  - `w`, `h`: standardized object size
51  - `extra`: decoded contents (e.g., URL that was in a QR-code, ISBN number from a barcode, etc)
52 
53  Note that when \p serstyle is \b Fine, only 4 corners are returned for each detected QR-code, but many points are
54  returned all over each detected barcode. Beware to not exceed your serial bandwidth in that case.
55 
56  @author Laurent Itti
57 
58  @displayname Demo QR-code
59  @videomapping YUYV 640 526 15.0 YUYV 640 480 15.0 JeVois DemoQRcode
60  @videomapping YUYV 320 286 30.0 YUYV 320 240 30.0 JeVois DemoQRcode
61  @videomapping NONE 0 0 0 YUYV 640 480 15.0 JeVois DemoQRcode
62  @videomapping NONE 0 0 0 YUYV 320 240 30.0 JeVois DemoQRcode
63  @email itti\@usc.edu
64  @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
65  @copyright Copyright (C) 2016 by Laurent Itti, iLab and the University of Southern California
66  @mainurl http://jevois.org
67  @supporturl http://jevois.org/doc
68  @otherurl http://iLab.usc.edu
69  @license GPL v3
70  @distribution Unrestricted
71  @restrictions None
72  \ingroup modules */
74 {
75  public:
76 
77  // ####################################################################################################
78  //! Constructor
79  // ####################################################################################################
80  DemoQRcode(std::string const & instance) : jevois::StdModule(instance)
81  { itsQRcode = addSubComponent<QRcode>("qrcode"); }
82 
83  // ####################################################################################################
84  //! Virtual destructor for safe inheritance
85  // ####################################################################################################
86  virtual ~DemoQRcode()
87  { }
88 
89  // ####################################################################################################
90  //! Processing function, no video output
91  // ####################################################################################################
92  virtual void process(jevois::InputFrame && inframe) override
93  {
94  // Wait for next available camera image:
95  jevois::RawImage const inimg = inframe.get();
96  unsigned int const w = inimg.width, h = inimg.height;
97 
98  // Convert the image to grayscale:
99  cv::Mat grayimg = jevois::rawimage::convertToCvGray(inimg);
100 
101  // Let camera know we are done processing the input image:
102  inframe.done();
103 
104  // Process gray image through zbar:
105  zbar::Image zgray(grayimg.cols, grayimg.rows, "Y800", grayimg.data, grayimg.total());
106  itsQRcode->process(zgray);
107 
108  // Send all the results over serial:
109  itsQRcode->sendSerial(this, zgray, w, h);
110 
111  // Cleanup zbar image data:
112  zgray.set_data(nullptr, 0);
113  }
114 
115  // ####################################################################################################
116  //! Processing function with video output to USB
117  // ####################################################################################################
118  virtual void process(jevois::InputFrame && inframe, jevois::OutputFrame && outframe) override
119  {
120  static jevois::Timer timer("processing", 60, LOG_DEBUG);
121  static unsigned short const txtcol = jevois::yuyv::White;
122  size_t const nshow = 4; // number of lines to show
123 
124  // Wait for next available camera image:
125  jevois::RawImage const inimg = inframe.get();
126 
127  timer.start();
128 
129  // We only handle one specific pixel format, any size in this demo:
130  unsigned int const w = inimg.width, h = inimg.height;
131  inimg.require("input", w, h, V4L2_PIX_FMT_YUYV);
132 
133  // While we process it, start a thread to wait for out frame and paste the input into it:
134  jevois::RawImage outimg;
135  auto paste_fut = std::async(std::launch::async, [&]() {
136  outimg = outframe.get();
137  outimg.require("output", w, h + nshow * 10 + 6, inimg.fmt);
138  jevois::rawimage::paste(inimg, outimg, 0, 0);
139  jevois::rawimage::writeText(outimg, "JeVois QR-code + Barcode Detection Demo", 3, 3, txtcol);
140  jevois::rawimage::drawFilledRect(outimg, 0, h, w, outimg.height-h, 0x8000);
141  });
142 
143  // Convert the image to grayscale and process it through zbar:
144  cv::Mat grayimg = jevois::rawimage::convertToCvGray(inimg);
145  zbar::Image zgray(grayimg.cols, grayimg.rows, "Y800", grayimg.data, grayimg.total());
146  itsQRcode->process(zgray);
147 
148  // Wait for paste to finish up:
149  paste_fut.get();
150 
151  // Let camera know we are done processing the input image:
152  inframe.done();
153 
154  // Draw all detections:
155  itsQRcode->drawDetections(outimg, 3, h + 3, zgray, w, h, nshow);
156 
157  // Send all serial messages:
158  itsQRcode->sendSerial(this, zgray, w, h);
159 
160  // Cleanup zbar image data:
161  zgray.set_data(nullptr, 0);
162 
163  // Show processing fps:
164  std::string const & fpscpu = timer.stop();
165  jevois::rawimage::writeText(outimg, fpscpu, 3, h - 13, txtcol);
166 
167  // Send the output image with our processing results to the host over USB:
168  outframe.send();
169  }
170 
171  // ####################################################################################################
172  protected:
173  std::shared_ptr<QRcode> itsQRcode;
174 };
175 
176 // Allow the module to be loaded as a shared object (.so) file:
DemoQRcode(std::string const &instance)
Constructor.
Definition: DemoQRcode.C:80
virtual void process(jevois::InputFrame &&inframe, jevois::OutputFrame &&outframe) override
Processing function with video output to USB.
Definition: DemoQRcode.C:118
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
Simple demo of QRcode and barcode detection and decoding using the ZBar library.
Definition: DemoQRcode.C:73
virtual void process(jevois::InputFrame &&inframe) override
Processing function, no video output.
Definition: DemoQRcode.C:92
unsigned int fmt
virtual ~DemoQRcode()
Virtual destructor for safe inheritance.
Definition: DemoQRcode.C:86
JEVOIS_REGISTER_MODULE(DemoQRcode)
StdModule(std::string const &instance)
cv::Mat convertToCvGray(RawImage const &src)
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:173
unsigned int width
std::string const & stop()
void paste(RawImage const &src, RawImage &dest, int dx, int dy)