|
| 1 | +--- |
| 2 | +title: MaixCAM MaixPy Barcode Recognition |
| 3 | +update: |
| 4 | + - date: 2024-12-16 |
| 5 | + author: lxowalle |
| 6 | + version: 1.0.0 |
| 7 | + content: Initial documentation |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +Before reading this article, make sure you know how to develop with MaixCAM. For details, please read [Quick Start](../README.md). |
| 12 | + |
| 13 | +## Introduction |
| 14 | + |
| 15 | +This article explains how to use MaixPy for Barcode recognition. |
| 16 | + |
| 17 | + |
| 18 | +## Using MaixPy to Recognize Barodes |
| 19 | + |
| 20 | +MaixPy's `maix.image.Image` includes the `find_barcodes` method for Barcode recognition. |
| 21 | + |
| 22 | +### How to Recognize Barcodes |
| 23 | + |
| 24 | +A simple example that recognizes Barcodes and draws a bounding box: |
| 25 | + |
| 26 | +```python |
| 27 | +from maix import image, camera, display |
| 28 | + |
| 29 | +cam = camera.Camera(480, 320) |
| 30 | +disp = display.Display() |
| 31 | + |
| 32 | +while 1: |
| 33 | + img = cam.read() |
| 34 | + |
| 35 | + barcodes = img.find_barcodes() |
| 36 | + for b in barcodes: |
| 37 | + rect = b.rect() |
| 38 | + img.draw_rect(rect[0], rect[1], rect[2], rect[3], image.COLOR_BLUE, 2) |
| 39 | + img.draw_string(0, 0, "payload: " + b.payload(), image.COLOR_GREEN) |
| 40 | + |
| 41 | + disp.show(img) |
| 42 | +``` |
| 43 | + |
| 44 | +Steps: |
| 45 | + |
| 46 | +1. Import the image, camera, and display modules: |
| 47 | + |
| 48 | + ```python |
| 49 | + from maix import image, camera, display |
| 50 | + ``` |
| 51 | + |
| 52 | +2. Initialize the camera and display: |
| 53 | + |
| 54 | + ```python |
| 55 | + cam = camera.Camera(480, 320) # Initialize the camera with a resolution of 480x320 in RGB format |
| 56 | + disp = display.Display() |
| 57 | + ``` |
| 58 | + |
| 59 | +3. Capture and display images from the camera: |
| 60 | + |
| 61 | + ```python |
| 62 | + while 1: |
| 63 | + img = cam.read() |
| 64 | + disp.show(img) |
| 65 | + ``` |
| 66 | + |
| 67 | +4. Use the `find_barcodes` method to detect barcodes in the camera image: |
| 68 | + |
| 69 | + ```python |
| 70 | + barcodes = img.find_barcodes() |
| 71 | + ``` |
| 72 | + |
| 73 | + - `img` is the camera image captured by `cam.read()`. When initialized as `cam = camera.Camera(480, 320)`, the `img` object is a 480x320 resolution RGB image. |
| 74 | + - `img.find_barcodes` searches for barcodes and saves the results in `barcodes` for further processing. |
| 75 | + - Note: The spacing of barcodes is small, and the width of barcodes is generally much larger than the height, so when adjusting the recognition rate and speed, you can try to make the width of the target image larger and the height smaller. |
| 76 | + |
| 77 | +5. Process and display the results of barcode recognition on the screen: |
| 78 | + |
| 79 | + ```python |
| 80 | + for b in barcodes: |
| 81 | + rect = b.rect() |
| 82 | + img.draw_rect(rect[0], rect[1], rect[2], rect[3], image.COLOR_BLUE, 2) |
| 83 | + img.draw_string(0, 0, "payload: " + b.payload(), image.COLOR_GREEN) |
| 84 | + ``` |
| 85 | + |
| 86 | + - `barcodes` is the result of querying barcodes by `img.find_barcodes()`, if no barcode is found then `barcodes` is empty. |
| 87 | + - `b.rect()` is used to get the position and size of the scanned barcode, `img.draw_rect()` uses the position information to draw the shape of the barcode. |
| 88 | + - `img.draw_string` is used to display the content and position of the barcode, `b.payload()` is used to get the content of the barcode. |
| 89 | + |
| 90 | + |
| 91 | +### 常用参数说明 |
| 92 | + |
| 93 | +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. |
| 94 | + |
| 95 | +| Parameter | Description | Example | |
| 96 | +| ---- | ------------------------------------------------------------ | ------------------------------------------------------------ | |
| 97 | +| 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:<br />`img.find_barcodes(roi=[50, 50, 100, 100])` | |
| 98 | + |
| 99 | +This article introduces common methods. For more API details, refer to the [image](../../../api/maix/image.md) section of the API documentation. |
0 commit comments