当前位置:   article > 正文

Canmv K210开发板案例——条形码识别_128b0

128b0

学习目标:

在Canmv IDE环境下使用K210学习学习条形码识别技术,分析条形码的信息、解码出条形码的文数信息、框出条形码,优化提高识别速度

条形码识别原理:

  • 自1949年诞生以来,条形码技术已被广泛应用于各种场合,用于存储和传递关键信息,如商品的产地、制造商、名称、生产日期、图书分类号、邮件路径及日期等。这种编码系统由不同宽度的黑色条带(代表数据)和白色空格(分隔数据)组成,遵循特定的编码规则,将数字和字母信息转换成可读的图形标识。
  • 市面上常见的条形码类型包括EAN-13、Code128、UPC、ITF-14、交叉25码、矩阵25码、Codabar码、Code93码和SSCC-18码等,每种都有其独特的应用场景和优势。

学习内容:

1、导入相关库,并初始化摄像头和LCD显示屏,将摄像头的颜色设置为灰度

import sensor, image, time, lcd    # sensor 引入感光元件的模块

lcd.init()
sensor.reset()# 初始化相机传感器
sensor.set_pixformat(sensor.GRAYSCALE) # GRAYSCALE
#sensor.set_framesize(sensor.VGA) # 高分辨率
sensor.set_framesize(sensor.QVGA)
#因为我们的条形码一般都是一个长的矩形,一般要设置窗口大小,小和窄一点。一般窗口设置为640X80.意思是只显示条形码的这一部分。这样能够提高检测速度
#sensor.set_windowing((640, 80))       # V Res的80 ==更少的工作(40为2倍的速度)。
sensor.skip_frames(time = 2000)  # 跳过n张照片,在更改设置后,跳过一些帧,等待感光元件变稳定。
sensor.set_auto_gain(False)  # 必须关闭此功能,以防止图像冲洗…
sensor.set_auto_whitebal(False)  # 必须关闭此功能,以防止图像冲洗…
clock = time.clock()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

2、转化条形码的类型名称为字符串

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"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
'
运行

3、通过while循环 将摄像头采集的画面进行条形码分析,如果图像中有条形码,则将条形码的信息打印出来,并用绿色框出条形码的位置,本文使用I25条形码

while(True):
    clock.tick() #记录开始时间 与clock.fps()搭配使用
    img = sensor.snapshot() #摄像头拍摄一张照片
    fps = clock.fps() # clock.tick()到开始时间计算出频率
    codes = img.find_barcodes() #检测条形码
    for code in codes:
        img.draw_rectangle(code.rect(), color=(0, 255, 0))  # 将返回的条形码框选

        # 打印条形码信息  条形码类型,返回条形码的有效载荷的字符串,返回以弧度计的条形码的旋度(浮点数)此处再转换成角度,条形码在图像中被检测到的次数(int),图片帧率
        print_args = (barcode_name(code), code.payload(), (180 * code.rotation()) / math.pi, code.quality(),clock.fps())
        print("Barcode %s, Payload \"%s\", rotation %f (degrees), quality %d,FPS %f" % print_args)
    img.draw_string(0, 0, "%2.1ffps" %(fps), color=(0, 60, 128), scale=2.0) #打印二维码内容
    lcd.display(img)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

实验分析:

实验条形码:
在这里插入图片描述


未优化时屏幕识别效果:

在这里插入图片描述


未优化时识别结果: 检测到的次数平均30左右,FPS平均9左右

在这里插入图片描述


未优化时代码展示: 未使用高分辨率、未设置适应与条形码长宽比例的分辨率

在这里插入图片描述


优化后代码展示: 使用高分辨率、设置适应与条形码长宽比例的分辨率

在这里插入图片描述


优化后屏幕识别效果:

在这里插入图片描述


优化后识别结果: 检测到的次数平均8左右,FPS平均13左右;与为优化之前对比明显可以看出识别速度提高,效率提高

在这里插入图片描述


实验过程与总结:

实验过程:

  • 实验通过检测条形码的位置后解码获得条形码信息。首先导入需要的库,初始化摄像头和LCD显示屏,并设置摄像头捕获图像为灰度模式,以提高处理效率。然后程序进入一个持续的循环过程,对摄像头画面进行实时分析,寻找并识别条形码。当条形码被检测到,系统会自动将其信息并通过终端打印,并在图像上用绿色框标识其位置,展示了颜色识别技术在自动化和信息提取中的应用。

总结:

  • 系统初始化后,LCD实时显示摄像头捕获的图像,能够自动识别并框出扫描的条形码,并在IDE的串行监视器中展示结果。支持多种条形码标准,用户可通过在线工具或IDE内置功能生成自定义条形码,实现高效数据处理。为提高识别准确度,建议在光线较好环境中操作,以减少误识别的可能性

完整代码展示:

# Find_barcodes.py - By: Jack - 周日 4月 14 2024

import sensor, image, time, math, lcd

lcd.init()
sensor.reset()# 初始化相机传感器
sensor.set_pixformat(sensor.GRAYSCALE) # GRAYSCALE
#sensor.set_framesize(sensor.VGA) # 高分辨率
sensor.set_framesize(sensor.QVGA)
#因为我们的条形码一般都是一个长的矩形,一般要设置窗口大小,小和窄一点。一般窗口设置为640X80.意思是只显示条形码的这一部分。这样能够提高检测速度
#sensor.set_windowing((640, 80))       # V Res的80 ==更少的工作(40为2倍的速度)。
sensor.skip_frames(time = 2000)  # 跳过n张照片,在更改设置后,跳过一些帧,等待感光元件变稳定。
sensor.set_auto_gain(False)  # 必须关闭此功能,以防止图像冲洗…
sensor.set_auto_whitebal(False)  # 必须关闭此功能,以防止图像冲洗…
clock = time.clock()

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"

while(True):
    clock.tick() #记录开始时间 与clock.fps()搭配使用
    img = sensor.snapshot() #摄像头拍摄一张照片
    fps = clock.fps() # clock.tick()到开始时间计算出频率
    codes = img.find_barcodes() #检测条形码
    for code in codes:
        img.draw_rectangle(code.rect(), color=(0, 255, 0))  # 将返回的条形码框选

        # 打印条形码信息  条形码类型,返回条形码的有效载荷的字符串,返回以弧度计的条形码的旋度(浮点数)此处再转换成角度,条形码在图像中被检测到的次数(int),图片帧率
        print_args = (barcode_name(code), code.payload(), (180 * code.rotation()) / math.pi, code.quality(),clock.fps())
        print("Barcode %s, Payload \"%s\", rotation %f (degrees), quality %d,FPS %f" % print_args)
    img.draw_string(0, 0, "%2.1ffps" %(fps), color=(0, 60, 128), scale=2.0) #打印二维码内容
    lcd.display(img)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号