Blog

QR Code Reader

In this blog post, we'll be demonstrating how you can easily create your own QR code scanner using the DepthAI SDK library. With just a few lines of code, you can leverage advanced machine learning techniques to detect and translate QR codes in real-time.

Our QR code reader uses a neural network to identify the position of QR codes within images, and then leverages the pyzbar python library to translate the codes into text. The end result is a lightning-fast solution that can process large volumes of QR codes with ease.

To see our QR code reader in action, check out our demo video on YouTube. You'll be amazed at just how easy it is to get started with this technology and how quickly you can integrate it into your own projects.

So why wait? Start exploring the power of our QR code reader today and take your projects to the next level!

First update your depthai-sdk to the latest version and install pyzbar library

pip install depthai-sdk -U
pip install pyzbar

Here's the code you need to get started:

import blobconverter
import cv2
import numpy as np
from pyzbar import pyzbar

from depthai_sdk import OakCamera
from depthai_sdk.visualize.configs import TextPosition


def callback(packet):
    for detection in packet.detections:
        bbox = detection.top_left[0], detection.top_left[1], detection.bottom_right[0], detection.bottom_right[1]
        # Expand bounding box
        bbox = (max(0, bbox[0] * 0.9), max(0, bbox[1] * 0.9),
                min(packet.frame.shape[1], bbox[2] * 1.1), min(packet.frame.shape[0], bbox[3] * 1.1))
        bbox = np.int0(bbox)
        cropped_qr = packet.frame[bbox[1]:bbox[3], bbox[0]:bbox[2]]  # crop QR code
        cropped_qr = 255 - cropped_qr  # invert colors for revert black and white

        # Decode QR code
        barcodes = pyzbar.decode(cropped_qr)
        for barcode in barcodes:
            barcode_info = barcode.data.decode('utf-8')
            # Add text to the frame
            packet.visualizer.add_text(barcode_info, size=1, bbox=bbox, position=TextPosition.MID, outline=True)

    frame = packet.visualizer.draw(packet.frame)
    cv2.imshow('QR code recognition', frame)


with OakCamera() as oak:
    color = oak.create_camera('color', fps=30)
    nn_path = blobconverter.from_zoo(name="qr_code_detection_384x384", zoo_type="depthai")
    nn = oak.create_nn(nn_path, color, nn_type='mobilenet')
    nn.config_nn(resize_mode='stretch')

    visualizer = oak.visualize(nn, record_path='qr_video.mp4', callback=callback)
    oak.start(blocking=True)

Comments (2)

In this blog post, we'll be demonstrating how you can easily create your own QR code scanner using the DepthAI SDK library. With just a few lines of code, you can leverage advanced machine learning techniques to detect and translate QR codes in real-time.

Our QR code reader uses a neural network to identify the position of QR codes within images, and then leverages the pyzbar python library to translate the codes into text. The end result is a lightning-fast solution that can process large volumes of QR codes with ease.

To see our QR code reader in action, check out our demo video on YouTube. You'll be amazed at just how easy it is to get started with this technology and how quickly you can integrate it into your own projects.

So why wait? Start exploring the power of our QR code reader today and take your projects to the next level!

First update your depthai-sdk to the latest version and install pyzbar library

pip install depthai-sdk -U
pip install pyzbar

Here's the code you need to get started:

import blobconverter
import cv2
import numpy as np
from pyzbar import pyzbar

from depthai_sdk import OakCamera
from depthai_sdk.visualize.configs import TextPosition


def callback(packet):
    for detection in packet.detections:
        bbox = detection.top_left[0], detection.top_left[1], detection.bottom_right[0], detection.bottom_right[1]
        # Expand bounding box
        bbox = (max(0, bbox[0] * 0.9), max(0, bbox[1] * 0.9),
                min(packet.frame.shape[1], bbox[2] * 1.1), min(packet.frame.shape[0], bbox[3] * 1.1))
        bbox = np.int0(bbox)
        cropped_qr = packet.frame[bbox[1]:bbox[3], bbox[0]:bbox[2]]  # crop QR code
        cropped_qr = 255 - cropped_qr  # invert colors for revert black and white

        # Decode QR code
        barcodes = pyzbar.decode(cropped_qr)
        for barcode in barcodes:
            barcode_info = barcode.data.decode('utf-8')
            # Add text to the frame
            packet.visualizer.add_text(barcode_info, size=1, bbox=bbox, position=TextPosition.MID, outline=True)

    frame = packet.visualizer.draw(packet.frame)
    cv2.imshow('QR code recognition', frame)


with OakCamera() as oak:
    color = oak.create_camera('color', fps=30)
    nn_path = blobconverter.from_zoo(name="qr_code_detection_384x384", zoo_type="depthai")
    nn = oak.create_nn(nn_path, color, nn_type='mobilenet')
    nn.config_nn(resize_mode='stretch')

    visualizer = oak.visualize(nn, record_path='qr_video.mp4', callback=callback)
    oak.start(blocking=True)
a year later

I am having trouble running this demo. The code starts and the viewer opens and displays video without any issues. However, as soon as the camera sees a QR code the application crashes with a division by zero error. The issue appears to be a pixel width and height that are zero. The camera is an OAK-D-IOT-75, python 3.10, libs updated as per instructions…