Note

This is the documentation for the latest development branch and may refer to features that are not available in released versions. If you are looking for the documentation for a specific release, use the drop-down menu on the left and select the desired version.

Data Matrix Code Recognition Routine Explanation

Data Matrix Code Recognition Routine Explanation#

Overview#

The Data Matrix code is a type of two-dimensional barcode widely used for identification and tracking of small products. It consists of small black and white squares arranged in a rectangular or square grid.

CanMV supports the OpenMV algorithm and can recognize Data Matrix codes. The related interface is find_datamatrices.

Example#

This example sets the camera output to a 640x480 grayscale image and uses image.find_datamatrices to recognize Data Matrix codes.

Tip

If the recognition success rate is low, try adjusting the camera’s mirror and flip settings.

# Data Matrix Example
import time
import math
import os
import gc
import sys

from media.sensor import *
from media.display import *
from media.media import *

# Define the width and height of the detection image
DETECT_WIDTH = 640
DETECT_HEIGHT = 480

sensor = None

try:
    # Construct a Sensor object using the default configuration
    sensor = Sensor(width=DETECT_WIDTH, height=DETECT_HEIGHT)
    # Reset the sensor
    sensor.reset()

    # Set the output size and format
    sensor.set_framesize(width=DETECT_WIDTH, height=DETECT_HEIGHT)
    sensor.set_pixformat(Sensor.GRAYSCALE)

    # Initialize the display
    Display.init(Display.VIRT, width=DETECT_WIDTH, height=DETECT_HEIGHT, fps=100)

    # Initialize the media manager
    MediaManager.init()
    # Start the sensor
    sensor.run()

    fps = time.clock()

    while True:
        fps.tick()

        # Check whether to exit
        os.exitpoint()

        img = sensor.snapshot()
        for matrix in img.find_datamatrices():
            # Draw a rectangle around the recognized Data Matrix code
            img.draw_rectangle([v for v in matrix.rect()], color=(255, 0, 0))
            print_args = (matrix.rows(), matrix.columns(), matrix.payload(), (180 * matrix.rotation()) / math.pi, fps.fps())
            print("Matrix [%d:%d], Content \"%s\", Rotation %f (degrees), FPS %f" % print_args)

        # Display the result on the screen
        Display.show_image(img)
        gc.collect()

except KeyboardInterrupt:
    print("User stopped")
except BaseException as e:
    print(f"Exception '{e}'")
finally:
    # Stop the sensor
    if isinstance(sensor, Sensor):
        sensor.stop()
    # Destroy the display
    Display.deinit()

    os.exitpoint(os.EXITPOINT_ENABLE_SLEEP)
    time.sleep_ms(100)

    # Release the media buffer
    MediaManager.deinit()

Code Explanation#

  • Import Modules: Import the necessary libraries to use sensor, display, and media management functions.

  • Define Image Dimensions: Set the detection image width and height to 640x480.

  • Sensor Configuration: Create and configure the sensor object, setting the image output size and format (grayscale image).

  • Display Initialization: Configure the display output mode, selecting IDE output.

  • Main Loop:

    • Capture the image and check for exit conditions.

    • Recognize Data Matrix codes in the image and draw red rectangle boxes around them.

    • Print detailed information about the Data Matrix code, including the number of rows, columns, content, rotation angle, and frame rate.

  • Exception Handling: Capture user interrupts or other exceptions, ensuring the sensor stops properly and resources are released.

Tip

For detailed interface definitions, please refer to find_datamatrices.

Comments list
Comments
Log in