Skip to content
Search

QReader

lastmod 2024-09-02created 2024-08-015 edits

QReader is a robust and straightforward solution for reading difficult and tricky QR codes within images in Python. It is powered by a YOLOv8 model.

The library consists of two main components.

  • A YOLOv8 QR detector model trained to detect and segment QR codes (also offered standalone)
  • The Pyzbar QR decoder.

Using the information extracted by this QR detector, QReader transparently applies various image preprocessing techniques on top of Pyzbar to maximize the decoding rate on difficult images.

Install

On Linux:

sudo apt-get install libzbar0

On Mac OS X:

brew install zbar

For servers with very limited resources, installing the CPU version of PyTorch is recommended. pip install torch --no-cache-dir

API Reference

QReader(model_size = 's', min_confidence = 0.5, reencode_to = 'shift-jis', weights_folder = None)

This is the main class of the library. Please, try to instantiate it just once to avoid loading the model every time you need to detect a QR code.

  • model_size: str. The size of the model to use. It can be
    • 'n' (nano)
    • 's' (small)
    • 'm' (medium)
    • 'l' (large)
    • Larger models could be more accurate but slower.
    • Recommended: 's' (#37). Default: 's'.

Drawing the ROI

from qreader import QReader
import cv2
from numpy import ndarray

qreader = QReader()
ori = cv2.imread("test2.jpg")
image = cv2.cvtColor(ori, cv2.COLOR_BGR2RGB)

# decoded_text = qreader.detect_and_decode(image=image, return_detections=True)
# print(decoded_text)

detections = qreader.detect(image=image, is_bgr=True)
decoded_qrs = []

for detection in detections:
    assert isinstance(detection, dict)
    assert isinstance(detection['confidence'], float)

    assert isinstance(detection['bbox_xyxy'], ndarray)
    assert isinstance(detection['bbox_xyxyn'], ndarray)

    assert isinstance(detection['cxcy'], tuple)
    assert len(detection['cxcy']) == 2

    assert isinstance(detection['cxcyn'], tuple)
    assert len(detection['cxcyn']) == 2

    assert isinstance(detection['wh'], tuple)
    assert len(detection['wh']) == 2

    assert isinstance(detection['whn'], tuple)
    assert len(detection['whn']) == 2

    assert isinstance(detection['polygon_xy'], ndarray)
    assert isinstance(detection['polygon_xyn'], ndarray)

    assert isinstance(detection['quad_xy'], ndarray)
    assert isinstance(detection['quad_xyn'], ndarray)

    assert isinstance(detection['padded_quad_xy'], ndarray)
    assert isinstance(detection['padded_quad_xyn'], ndarray)

    assert isinstance(detection['image_shape'], tuple)
    assert len(detection['image_shape']) == 2

    decoded_qr = qreader.decode(image=image, detection_result=detection)
    assert isinstance(decoded_qr, str)

    print(decoded_qr)
    decoded_qrs.append(decoded_qr)

    roi = detection['bbox_xyxy'].tolist()
    p1 = int(roi[0]), int(roi[1])
    p2 = int(roi[2]), int(roi[3])
    ori = cv2.rectangle(ori, p1, p2, (0, 0, 255), 2, cv2.LINE_AA)

cv2.imwrite("result2.jpg", ori)

See also

  • QRCode
  • BarCode
  • libzbar - QR Code scanner engine for QR+Emoji
  • zbar-py - zbar package
  • pyzbar - Read one-dimensional barcodes and QR codes from Python 2 and 3.
  • QReader - Robust and Straight-Forward solution for reading difficult and tricky QR codes within images in Python. Supported by a YOLOv8 QR Segmentation model.
  • cv2.QRCodeDetector - QR code detector built into OpenCV.

Favorite site

author Zer0 sitemap