赞
踩
在Canmv IDE环境下使用K210学习学习条形码识别技术,分析条形码的信息、解码出条形码的文数信息、框出条形码,优化提高识别速度
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()
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"
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)
实验条形码:
未优化时屏幕识别效果:
未优化时识别结果: 检测到的次数平均30左右,FPS平均9左右
未优化时代码展示: 未使用高分辨率、未设置适应与条形码长宽比例的分辨率
优化后代码展示: 使用高分辨率、设置适应与条形码长宽比例的分辨率
优化后屏幕识别效果:
优化后识别结果: 检测到的次数平均8左右,FPS平均13左右;与为优化之前对比明显可以看出识别速度提高,效率提高
# 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)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。