Brainy Pi

Available to select audience (currently beta)

QR scanner
Today’s world bombards us with an overwhelming amount of digital information. One efficient means to share this information is through the use of QR (Quick Response) codes, which can be found almost everywhere, from digital tickets to product packaging. They enable users to access information swiftly using their smartphone cameras. In this blog, we will demonstrate how to create a basic QR code scanner using Opencv on BrainyPi. First, we will explore how to capture images using the OpenCV library, detect, and extract QR codes using the pyzbar library. We will also cover how to draw polygons around identified QR codes and append decoded data to the image. Finally, we will integrate all the components into a simple application on BrainyPi that can scan and extract QR codes from live video feeds.
We will start by examining how to capture images using the OpenCV library and how to detect and extract QR codes using the pyzbar library. We will also discuss how to draw polygons around detected QR codes and add decoded data to the image. Finally, we will put everything together on BrainyPi in a simple application  to scan and extract QR codes from live video feeds.
 

A little about the source code

  1. The code for the QR code scanner contains two files scanQRcodes and scanQRcodesUtils.

scanQRcodes.py

  1. This Python code imports the OpenCV library, a QR code scanner utility module named scanQRcodeUtils, and the time module.
  2. The code defines a main function that does the following:
    • Initializes a video camera object from the OpenCV library.
    • Waits for two seconds to give the camera time to warm up.
    • Loops indefinitely, capturing images from the camera, attempting to scan QR codes in the images, and displaying the images with any detected QR codes highlighted.
    • When the user presses q key, the loop terminates, and the program exits.
The scanQRCodes function is in the scanQRcodeUtils.py file.
Overall, this code appears to be a basic framework for capturing images and scanning QR codes using the OpenCV library.

scanQRcodeUtils.py

  1. This Python code defines a function named scanQRCodes, which takes an input image and an optional draw flag as parameters. The function uses the decode function from the pyzbar library to scan for QR codes in the input image, returning the decoded QR code data as a list of strings and a modified image with the detected QR codes highlighted.
  2. The decode function accepts the ZBarSymbol.QRCODE constant as a parameter to specify that only QR codes should be detected in the input image.
  3. For each detected QR code, the function extracts the decoded data as a UTF-8 encoded string and the polygon that encloses the QR code’s position in the image. The function then draws a polygon around the QR code in the image and adds the decoded data as text to the image if the draw flag is set to True.
  4. Finally, the function appends the decoded QR code data to the qrDataList and returns it, along with the modified image.
  5. Overall, this code provides a function to detect and extract QR codes from an input image and can use in conjunction with the scanQRcode.pyto implement a basic QR code scanning application using the OpenCV library.

Setting up

  1. We need to install all the dependencies before we can run the code.
  2. We also need to get the code before proceeding with the dependencies installation.
git clone https://github.com/brainypi/brainypi-opencv-examples.git
cd qrcode-scanner
  1. Installing the dependencies
pip install -r requirements.txt

Running the code

  1. We can now run the code and start scanning the QR codes.
python scanQRcodes.py
  1. Press q exit out of the program.

EndNotes

Now, we have a basic understanding of how to  perform QR code scanner using OpenCV on BrainyPi to create a simple QR code scanner and how we can apply this technology to various applications in the real world with BrainyPi.
0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*