2. 识别条形码例程讲解

2. 识别条形码例程讲解#

1. 概述#

条形码(Barcode)是一种用来表示数据的视觉模式,通过使用不同宽度和间隔的条纹或图案来编码信息。条形码广泛应用于各种行业,用于自动识别、存储和管理数据。

CanMV支持OpenMV算法,支持识别条形码,相关接口为find_barcodes,支持多种条形码识别

2. 示例#

本示例设置摄像头输出640x480灰度图像,使用image.find_barcodes来识别条形码

小技巧

如果识别成功率低,可尝试修改摄像头输出的mirror和flip设置 以及注意条形码2边留出白色区域

# 条形码检测示例
import time, math, os, gc, sys

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

def barcode_name(code):
    if(code.type() == image.EAN2):
        return "EAN2"
    if(code.type() == image.EAN5):
        return "EAN5"
    if(code.type() == image.EAN8):
        return "EAN8"
    if(code.type() == image.UPCE):
        return "UPCE"
    if(code.type() == image.ISBN10):
        return "ISBN10"
    if(code.type() == image.UPCA):
        return "UPCA"
    if(code.type() == image.EAN13):
        return "EAN13"
    if(code.type() == image.ISBN13):
        return "ISBN13"
    if(code.type() == image.I25):
        return "I25"
    if(code.type() == image.DATABAR):
        return "DATABAR"
    if(code.type() == image.DATABAR_EXP):
        return "DATABAR_EXP"
    if(code.type() == image.CODABAR):
        return "CODABAR"
    if(code.type() == image.CODE39):
        return "CODE39"
    if(code.type() == image.PDF417):
        return "PDF417"
    if(code.type() == image.CODE93):
        return "CODE93"
    if(code.type() == image.CODE128):
        return "CODE128"

DETECT_WIDTH = 640
DETECT_HEIGHT = 480

sensor = None

try:
    # 使用默认配置构造一个Sensor对象
    sensor = Sensor(width = DETECT_WIDTH, height = DETECT_HEIGHT)
    # sensor复位
    sensor.reset()
    # 设置水平镜像
    # sensor.set_hmirror(False)
    # 设置垂直翻转
    # sensor.set_vflip(False)
    # 设置通道 0 输出大小
    sensor.set_framesize(width = DETECT_WIDTH, height = DETECT_HEIGHT)
    # 设置通道 0 输出格式
    sensor.set_pixformat(Sensor.GRAYSCALE)

    # 设置显示,如果您选择的屏幕无法点亮,请参考API文档中的K230_CanMV_Display模块API手册自行配置,下面给出四种显示方式
    # 使用 HDMI 作为显示输出,设置为 VGA
    # Display.init(Display.LT9611, width = 640, height = 480, to_ide = True)

    # 使用 HDMI 作为显示输出,设置为 1080P
    # Display.init(Display.LT9611, width = 1920, height = 1080, to_ide = True)

    # 使用 LCD 作为显示输出
    # Display.init(Display.ST7701, to_ide = True)

    # 使用 IDE 作为输出
    Display.init(Display.VIRT, width = DETECT_WIDTH, height = DETECT_HEIGHT, fps = 100)

    # 初始化媒体管理器
    MediaManager.init()
    # sensor开始运行
    sensor.run()

    fps = time.clock()

    while True:
        fps.tick()

        # 检查是否应该退出
        os.exitpoint()
        img = sensor.snapshot()

        for code in img.find_barcodes():
            img.draw_rectangle([v for v in code.rect()], color=(255, 0, 0))
            print_args = (barcode_name(code), code.payload(), (180 * code.rotation()) / math.pi, code.quality(), fps.fps())
            print("条形码 %s, 内容 \"%s\", 旋转 %f (度), 质量 %d, FPS %f" % print_args)

        # 将结果绘制到屏幕上
        Display.show_image(img)
        gc.collect()

        #print(fps.fps())
except KeyboardInterrupt as e:
    print(f"user stop")
except BaseException as e:
    print(f"Exception '{e}'")
finally:
    # sensor停止运行
    if isinstance(sensor, Sensor):
        sensor.stop()
    # 销毁display
    Display.deinit()

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

    # 释放媒体缓冲区
    MediaManager.deinit()

提示

具体接口定义请参考 find_barcodes