Build a QR Code Scanner Using a BrainyPi

by dhruvarora561 in Circuits > Computers

210 Views, 1 Favorites, 0 Comments

Build a QR Code Scanner Using a BrainyPi

qr-code-g95540488d_640.png

1. In today's world, we are surrounded by an overwhelming amount of digital information. One way to quickly and efficiently share information is through the use of QR (Quick Response) codes. These codes can be found everywhere, from product packaging to digital tickets, and they allow users to quickly access information using their smartphone cameras.

2. In this blog, we will explore how to create a basic QR code scanner using Python and OpenCV. OpenCV is an open-source library used for computer vision applications, while Python is a powerful programming language used for a wide range of applications.

3. 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 in a simple application that can be used to scan and extract QR codes from live video feeds.

4. By the end of this blog, you will have a basic understanding of how to use Python and OpenCV to create a simple QR code scanner and how this technology can be applied to various applications in the real world.

A liitle 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.

3.The scanQRCodes function is included 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 `scanQRCode`s, 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 be used in conjunction with the `scanQRcode.py`to implement a basic QR code scanning application using the OpenCV library.

Supplies

  1. Keyboard
  2. Monitor
  3. Mouse
  4. Internet Connection
  5. Bariny Pi

Setting Up

We will start by cloning the git repository

git clone https://github.com/brainypi/brainypi-opencv-examples.git

Now, let us create a virtual environment

python -m venv venv

Activating the virtual environment

cd venv/Scripts
activate

Installing dependencies

pip install -r requirements.txt

Running the Code

We can now run the code.

python scanQRcodes.py

To exit out of the program press `q`.