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
The code for the QR code scanner contains two files
scanQRcodes
andscanQRcodesUtils
.
scanQRcodes.py
This Python code imports the OpenCV library, a QR code scanner utility module named scanQRcodeUtils, and the time module.
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
This Python code defines a function named
scanQRCode
s, which takes an input image and an optional draw flag as parameters. The function uses the decode function from thepyzba
r 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.The decode function accepts the
ZBarSymbol.QRCODE
constant as a parameter to specify that only QR codes should be detected in the input image.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.
Finally, the function appends the decoded QR code data to the
qrDataList
and returns it, along with the modified image.Overall, this code provides a function to detect and extract QR codes from an input image and can use in conjunction with the
scanQRcode.py
to implement a basic QR code scanning application using the OpenCV library.
Setting up
We need to install all the dependencies before we can run the code.
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
Installing the dependencies
pip install -r requirements.txt
Running the code
We can now run the code and start scanning the QR codes.
python scanQRcodes.py
Press
q
exit out of the program.