Skip to content

QR Reader#

Tool for detecting QR codes in images.

from PIL import Image, ImageDraw

from vision_agent_tools.models.qr_reader import QRReader

# Open the image containing the QR code
image = Image.open("sample_qr_image.jpeg")

# Create a QR code reader object
qr_reader = QRReader()

# Detect QR codes in the image
detections = qr_reader(image)


if detections:

    detection = detections[0]
    draw = ImageDraw.Draw(image)

    # Print the detected text
    print(f"Decoded Text: {detection.text}")

    # Draw the bounding box
    x_min, y_min, x_max, y_max = (
        int(detection.bbox[0]),
        int(detection.bbox[1]),
        int(detection.bbox[2]),
        int(detection.bbox[3]),
    )
    draw.rectangle(((x_min, y_min), (x_max, y_max)), outline="red", width=2)

    # Draw the text on top of the image
    draw.text((x_min + 10, y_min - 10), detection.text, fill="blue", anchor="mm")
    image.show()
else:
    print("No QR codes detected in the image.")
QR Result
Displaying the Detection Result

QRCodeDetection #

Bases: BaseModel

Represents a detected QR code.

QRReader #

Bases: BaseMLModel

This tool utilizes the qreader library to detect QR codes within an input image. It returns a list of QRCodeDetection objects for each detected QR code, containing the decoded text, confidence score, polygon coordinates, bounding box, and center point.

__call__(image) #

Detects QR codes in an image.

Parameters:

Name Type Description Default
image Image

The input image for QR code detection.

required

Returns:

Type Description
list[QRCodeDetection]

list[QRCodeDetection]: A list of QRCodeDetection objects containing information about each detected QR code, or an empty list if none are found.

__init__() #

Initializes the QR code reader tool.

Loads the QReader instance for QR code detection.