import sensor, image, time, lcd
from pyb import UART,Timer,LED
sensor.reset()#初始化摄像头
sensor.set_pixformat(sensor.RGB565)#设置彩图
sensor.set_framesize(sensor.QVGA)#设置图像像素为QVGA
sensor.skip_frames(1)#里面如果是数字就是跳过几帧,如果是time=1000就是跳过1s的帧数 # 等待设置生效 "设置跳帧成功,如果卡摄像头将这句话注释"
sensor.set_auto_gain(False)#关闭自动增益
sensor.set_auto_whitebal(False)#关闭白平衡
clock = time.clock()#记录帧率
lcd.init()#初始化LCE屏幕
uart = UART(3,115200,8,None,1)#初始化串口
#============================================================
LEDR = LED(1)
LEDG = LED(2)
LEDB = LED(3)
data = []#一个列表
left_roi= [0,0,260,200]#设置了一个感兴趣区,如果框不住请调整 160宽度是最小的,但是不建议
'''
#交通灯的阈值(经过曝光处理)主要使用这个阈值
red_threshold_01 = (29, 100, 38, 96, 20, 63)
green_threshold_01 = (29, 91, -96, -18, 19, 67)
yellow_threshold_01 = (34, 100, -31, 42, 48, 84)
'''
FlagOK = 0
colorFlag = 0
yellow_threshold_01 = (34, 100, -31, 42, 48, 84)
green_threshold_01 = (29, 91, -96, -18, 19, 67)
#===================================================================================================
#UART发送函数
def USART_Send(src,length):
for i in range(length):
uart.writechar(src[i])
#===================================================================================================
#二维码识别函数#
def qrcode_check(srcbuf):
global FlagOK
if(FlagOK == 1):
LEDB.on()
time.sleep_ms(50)
LEDB.off()
# img.lens_corr(1.8)
for qrcode in img.find_qrcodes():
print(qrcode)
FlagOK = 0
qr_Tab = qrcode.payload()
uart.writechar(0x55)
uart.writechar(0x02)
uart.writechar(0x92)
uart.writechar(0x01)
uart.writechar(len(qr_Tab))
for qrdata in qr_Tab:
uart.writechar(ord(qrdata))
uart.writechar(0xBB)
if(FlagOK == 2):
FlagOK = 0
#===================================================================================================
#交通灯识别函数
def colorfind(srcbuf):
global colorFlag
if(colorFlag == 1):#==================
LEDR.on()
time.sleep_ms(50)
LEDR.off()
#看参数根据距离更改 ,x_stride =5,y_stride=5 增加识别的速度 软件复位:machine.reset()
# pixels_threshold=30 最小的色块
# area_threshold=30 最小边界框区域
# merge=True 合并色块
# margin=2 合并的时候边界距离
yellow = img.find_blobs([yellow_threshold_01],roi=left_roi,x_stride =5,y_stride=5, pixels_threshold=30, area_threshold=30, merge=True, margin=2)
green = img.find_blobs([green_threshold_01] ,roi=left_roi,x_stride =5,y_stride=5, pixels_threshold=30, area_threshold=30, merge=True, margin=2)
if yellow:
LEDB.off()
LEDR.on()
LEDG.on()
LEDR.off()
LEDG.off()
uart.writechar(0x55)
uart.writechar(0x0E)
uart.writechar(0x02)
uart.writechar(0x03)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(0x05)
uart.writechar(0xBB)
print("发送黄色")
colorFlag = 0
elif green:
LEDB.off()
LEDG.on()
LEDG.off()
uart.writechar(0x55)
uart.writechar(0x0E)
uart.writechar(0x02)
uart.writechar(0x02)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(0x04)
uart.writechar(0xBB)
print("发送绿色")
colorFlag = 0
else :
LEDR.on()
LEDG.off()
LEDB.off()
uart.writechar(0x55)
uart.writechar(0x0E)
uart.writechar(0x02)
uart.writechar(0x01)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(0x03)
uart.writechar(0xBB)
print("发送红色")
colorFlag = 0
if(colorFlag == 2):
colorFlag = 0
#===================================================================================================
print("准备进入主循环")
#55 02 92 01 00 00 00 BB 二维码识别串口协议
#55 02 91 01 00 00 00 BB 交通灯识别串口协议
####################################################################################################
while(True):
# LEDB.on()
img = sensor.snapshot()# 拍一张照片并返回图像。
#.lens_corr(1.8) #如果二维码识别不到把注释去掉
img.draw_rectangle( left_roi, color=(255,0,0))
if(uart.any()):
print("收到串口发送的数据")
data = uart.read(8)
print(data)
if(len(data) >= 8):
if((data[0] == 0x55)&(data[1] == 0x02)& (data[2] == 0x92) & (data[7] == 0xBB)):
if(data[3] == 0x01):
if(FlagOK == 0):
FlagOK = 1
print("开始识别二维码")
sensor.set_auto_exposure(True)#这里设置自动曝光
if(data[3] == 0x02):
print("停止QR识别")
FlagOK = 2
#主循环函数
####################################################################################################
if((data[0] == 0x55)&(data[1] == 0x02)& (data[2] == 0x91) & (data[7] == 0xBB)):
if(data[3] == 0x01):
if(colorFlag == 0):
colorFlag = 1
print("开始识别颜色")
sensor.set_auto_exposure(False, 1000)#这里设置曝光时间
if(data[3] == 0x02):
print("停止颜色识别")
colorFlag = 2
#主循环函数
#===================================================================================================
lcd.display(img)
qrcode_check(data)
colorfind(data)
#重点循环
#===================================================================================================
E
edow
@edow
0
声望
1
楼层
265
资料浏览
0
粉丝
1
关注
edow 发布的帖子
-
OpenMv多个二维码识别后的值怎么回传给单片机?单个的值在 代码 数组 qrdata 中,那多个的呢?