title | update | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
MaixCAM MaixPy QR Code Recognition |
|
Before reading this article, make sure you are familiar with how to develop with MaixCAM. For details, please read Quick Start.
This article explains how to use MaixPy for QR code recognition.
MaixPy's maix.image.Image
includes the find_qrcodes
method for QR code recognition.
A simple example that recognizes QR codes and draws a bounding box:
from maix import image, camera, display
cam = camera.Camera(320, 240)
disp = display.Display()
while True:
img = cam.read()
qrcodes = img.find_qrcodes()
for qr in qrcodes:
corners = qr.corners()
for i in range(4):
img.draw_line(corners[i][0], corners[i][1], corners[(i + 1) % 4][0], corners[(i + 1) % 4][1], image.COLOR_RED)
img.draw_string(qr.x(), qr.y() - 15, qr.payload(), image.COLOR_RED)
disp.show(img)
Steps:
-
Import the image, camera, and display modules:
from maix import image, camera, display
-
Initialize the camera and display:
cam = camera.Camera(320, 240) # Initialize the camera with a resolution of 320x240 in RGB format disp = display.Display()
-
Capture and display images from the camera:
while True: img = cam.read() disp.show(img)
-
Use the
find_qrcodes
method to detect QR codes in the camera image:qrcodes = img.find_qrcodes()
img
is the camera image captured bycam.read()
. When initialized ascam = camera.Camera(320, 240)
, theimg
object is a 320x240 resolution RGB image.img.find_qrcodes
searches for QR codes and saves the results inqrcodes
for further processing.
-
Process and display the results of QR code recognition on the screen:
for qr in qrcodes: corners = qr.corners() for i in range(4): img.draw_line(corners[i][0], corners[i][1], corners[(i + 1) % 4][0], corners[(i + 1) % 4][1], image.COLOR_RED) img.draw_string(qr.x(), qr.y() - 15, qr.payload(), image.COLOR_RED)
qrcodes
contains the results fromimg.find_qrcodes()
. If no QR codes are found,qrcodes
will be empty.qr.corners()
retrieves the coordinates of the four corners of the detected QR code.img.draw_line()
uses these coordinates to draw the QR code outline.img.draw_string
displays information about the QR code content and position.qr.x()
andqr.y()
retrieve the x and y coordinates of the QR code's top-left corner, andqr.payload()
retrieves the content of the QR code.
List common parameters and their explanations. If you cannot find parameters that fit your application, consider whether to use a different algorithm or extend the functionality based on the current algorithm's results.
Parameter | Description | Example |
---|---|---|
roi | Sets the rectangular area for the algorithm to compute, where roi=[x, y, w, h], x and y denote the top-left coordinates of the rectangle, and w and h denote the width and height of the rectangle, defaulting to the entire image. | Compute the area with coordinates (50,50) and width and height of 100:img.find_qrcodes(roi=[50, 50, 100, 100]) |
qrcoder_type | Set the QR code library decoder type; you can choose either image.QRCodeDecoderType.QRCODE_DECODER_TYPE_ZBAR or image::QRCodeDecoderType::QRCODE_DECODER_TYPE_QUIRC . QRCODE_DECODER_TYPE_ZBAR offers faster recognition speed and higher accuracy at lower resolutions. QRCODE_DECODER_TYPE_QUIRC is relatively faster at higher resolutions but with slightly lower accuracy. By default, QRCODE_DECODER_TYPE_ZBAR is used.Effective in version 4.7.7 and later. |
img.find_qrcodes(decoder_type=image.QRCodeDecoderType.QRCODE_DECODER_TYPE_ZBAR) |
This article introduces common methods. For more API details, refer to the image section of the API documentation.