Decoding QR Codes in Real-Time with OpenCV Python: A Practical Guide

Rohan Basu Roy
3 min readDec 17, 2023

--

QR Codes, short for Quick Response Codes, are two-dimensional barcodes that can store a wide variety of information, from URLs to simple text. Python, with its robust libraries and community support, has become a go-to language for various image processing tasks, including QR code reading. In this article, we explore how to build a real-time QR code reader using Python libraries like OpenCV and Pyzbar.

Encoding Process (Creating QR Codes)

  1. Data Conversion to a QR Code Pattern: The process begins by taking the input data (like a URL, text, or other information) and converting it into a format that can be represented as a QR code. The qrcode library in Python can handle this process.
  2. Error Correction: QR codes have built-in error correction to maintain integrity even if the code is partially damaged or obscured. When generating a QR code, a certain level of error correction is applied. This step adds redundancy to the data so that it can still be recovered if part of the QR code is unreadable.
  3. Generating the Pattern: The data, along with error correction information, is then used to create the QR code’s unique pattern of black and white squares. Each square or cell represents a bit of information.
  4. Output as an Image: The qrcode library in Python then generates an image file (like PNG) from this pattern. This image file can be printed, displayed on a screen, or embedded in a web page.
import pyqrcode
import png
from pyqrcode import QRCode


# String which represents the QR code
s = "something is not hidden, but is easily overlooked because it blends in with its surrounding"

# Generate QR code
url = pyqrcode.create(s)

# Create and save the png file naming "myqr.png"
url.png('myqr.png', scale = 6)

That’s it! You’ve now successfully generated and saved a QR code in Python

Decoding Process (Reading QR Codes)

  1. Image Capturing and Processing: When a QR code image is scanned using a camera or uploaded for processing, the first step is to correctly identify and isolate the QR code from the rest of the image. This may involve image processing techniques like filtering and edge detection, often handled by libraries like OpenCV.
  2. Interpreting the Pattern: The Pyzbar library comes into play here. It analyzes the black and white pattern of the QR code. Pyzbar decodes the pattern based on the QR code standard, which defines how data is encoded into the pattern of squares.
  3. Error Correction and Data Extraction: Pyzbar uses the error correction data embedded in the QR code to reconstruct the original data if the code is partially obscured or damaged. It then extracts the encoded data from the pattern.
  4. Output the Data: Finally, the decoded data (text, URL, etc.) is outputted by the Pyzbar library. This data can then be used within the Python program for various purposes like opening a URL, displaying text, or storing information.

Setting Up the Environment

Firstly, ensure that Python is installed on your system. Next, you will need to install OpenCV and Pyzbar. OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. Pyzbar is a Python library that facilitates barcode reading.

pip install opencv-python
pip install pyzbar
import cv2
import numpy as np
import pyzbar.pyzbar as pyzbar

cap = cv2.VideoCapture(0)

while True:
_, frame = cap.read()
decodedObjects = pyzbar.decode(frame)
for obj in decodedObjects:
print("Data", obj.data)
cv2.putText(frame, str(obj.data), (50, 50), font, 2, (255, 0, 0), 3)
cv2.imshow("Image", frame)
key = cv2.waitKey(1)
if key == 27: # ESC key
break

The cv2.putText method is used to draw the text on the video frame. The loop can be exited by pressing the ESC key (key code 27).

Closing the Video Stream

After breaking out of the loop, it’s important to release the video capture object and close any OpenCV windows to free up resources.

cap.release()
cv2.destroyAllWindows()

This script offers a basic yet powerful example of real-time QR code reading. The applications of such a tool are vast, including attendance systems, authentication processes, and information sharing.

For further exploration, one might consider enhancing the functionality by adding features like QR code generation or integrating with a database to retrieve information based on the decoded data.

By harnessing the power of Python and these libraries, we can create sophisticated image processing applications with minimal code.

--

--

Rohan Basu Roy

Robotics engineer passionate about harmonic notes and hardware-software interactions. Cricket,computer vision,music,art,the end of the world.