This recipe shows how to make an app that reads QR codes and barcodes.
The application targeted at Raspberry Pi, which has rather limited processing power. The standard camera produces 5 MPx images which are too large to be processed in real time as such. Therefore, some speed optimization techniques are introduced.
The processing graph is too large to fit one page, which is why we'll walk through it from top to bottom in smaller pieces.
- The images originate from the default webcam as configured in Image Source. Notice that the default camera is selected automatically by setting the
/^webcam/, a regular expression that finds the first webcam connected to the computer.
- The frame rate is reduced by a factor of 3 with Count and Gate tools. The counter counts from zero to 2 and sets
Truewhen it wraps around to zero.
Resetoutput is connected to
Enabledinput of the Gate tool. Gate lets an image pass through from input to output only when
Enabledis true. That is, only one out of every three images makes it through.
Input parameterstable in Gate tool has the name of the output socket (here
image) in the first column and the type of the output (here
Image) in the second column. The output is connected to three tools as we will see later.
- Convert Colors has been configured to convert the image from color to grayscale. This will speed up the processing downstream because grayscale image has only a quarter of the bytes compared to ordinary RGB color image.
Sizeaccordingly. They are passed on to Crop Image. The downside of cropping is that the barcodes and QR codes will be read only if they are close to the center of the image.
Now the processing path is split into three branches. The right one detects and reads barcodes, whereas the left one detects and reads QR codes. The middle branch measures histogram from the cropped grayscale image and analyzes the histogram to find the optimal intensity threshold between dark and bright bars or modules in the code. See the Histogram and Find Threshold tools for details. The user can enable and disable the code reading branches by toggling the
enabled input parameters of two gates.
The branches for finding and reading barcodes and QR codes are identical. Let us walk through the one for barcodes.
- Find Barcode finds the positions of barcodes in the customary
Sizeformat. The intensity threshold between the bars and the background comes from the
Find Thresholdtool. You can limit the maximum number of codes the detector tries to find with the
Max Detectionsinput parameter. The tool outputs sets of
Sizematrices which locate the positions and orientations of the found codes.
- Begin Iterate has been configured to input the sets of frames and sizes from the detector. It splits frame and size matrix sets back to single entities. For instance, assume that there are four barcodes in the image. In this case Iterate Tool splits the 16-by-4 frame matrix into four individual 4-by-4 matrices and the 4-by-2 size matrix into four 1-by-2 matrices.
- Read Barcode inputs the image and a
Size-locator and reads the code bounded by the box defined by
Size. If you know the code type in advance (e.g. EAN-13, Code128 etc), you can configure it with the
Code Typeinput parameter. This makes the reading faster and more reliable. If you don't know the code type, set the value to
Auto-detectwhich is the default.
- End Iterate is a counterpart to
Begin Iteratedescribed above. It stacks up entries received in its inputs, producing either matrices or tables as output. In this example, the entries received at
Element 0input are the code strings and the entries at
Element 1are the code types. If there are, say, four codes found in the original input image, the output at
Result 0will be a 4-by-2 table. Notice that
Syncinput has to be connected to the corresponding
Begin Iterate Tool.
The processing sequence for the QR code branch is nearly identical. Notice, however, that the code type is set as
Auto-detect like it was in the barcode branch.
When both analysis branches are enabled, the app reads QR codes and bar codes simultaneously. The picture below shows a typical result.