@kidswong999 在 uncaught exception in Timer(4) interrupt handler 这定时器多少有点叛逆。 中说:
我是用OpenMV4 H7 Plus,固件版本4.3.3,没有出现错误。
我现在手里有两个设备,一个可以工作(型号看不出来,固件版本是4.多),另外一个型号是MV3R2(固件版本是3.3.1),这个R2的就一直报错,也能运行就是摄像头工作一段之后串口就开始丢数据,而且一直报定时器错误,会不会升级一下固件会好一点
@kidswong999 在 uncaught exception in Timer(4) interrupt handler 这定时器多少有点叛逆。 中说:
我是用OpenMV4 H7 Plus,固件版本4.3.3,没有出现错误。
我现在手里有两个设备,一个可以工作(型号看不出来,固件版本是4.多),另外一个型号是MV3R2(固件版本是3.3.1),这个R2的就一直报错,也能运行就是摄像头工作一段之后串口就开始丢数据,而且一直报定时器错误,会不会升级一下固件会好一点
@kidswong999 在 uncaught exception in Timer(4) interrupt handler 这定时器多少有点叛逆。 中说:
你的代码需要串口接受数据才能运行,所以我没有办法复现你的问题。
如果你想要我复现问题的话,需要把串口部分闪掉,只保留运行错误的部分。建议1:每个定时器绑定一个回调函数。
建议1:回调函数从开机到下电都不要停。deinit用不到的,反正你是1秒运行一次,又没有什么损耗。
代码重新修改了一下
好了,给串口删了删,189行跟190行放了注释,因为不是从串口读了,会一直执行,所以直接给定了一个协议,不过切换功能需要手动改192行这个debug'的数组,救救娃qwq
import sensor, image,time,lcd
from pyb import UART,Timer,LED
sensor.reset()
print("初始化摄像头成功")
sensor.set_pixformat(sensor.RGB565)
print("设置彩图成功")
sensor.set_framesize(sensor.QVGA)
print("设置图像像素为QVGA")
sensor.skip_frames(1)#里面如果是数字就是跳过几帧,如果是time=1000就是跳过1s的帧数
print("设置跳帧成功,如果卡摄像头将这句话注释")
sensor.set_auto_whitebal(False)
print("关闭白平衡成功")
sensor.set_auto_gain(False)
print("关闭自动增益成功")
clock = time.clock()
print("记录帧率")
lcd.init()
print("初始化LCE屏幕成功")
uart = UART(3,115200,8,None,1)#初始化串口
print("初始化串口成功")
#进行初始化
#===================================================================================================
data = []#一个列表
LED_Red = LED(1)
LED_Green = LED(2)
LED_Blue = LED(3)
#设置了一个感兴趣区,如果框不住请调整 160宽度是最小的,但是不建议
left_roi= [0,0,240,120]
#第一个模糊阈值
red_color0 = (0, 100, -26, 65, -25, 59)
green_color0 = (0, 100, -73, 21, -40, 65)
yellow_color0 = (43, 100, -37, 58, -22, 72)
#第二个模糊阈值
red_color1 = (55, 98, 35, 59, 0, 19)
green_color1 = (0, 100, -128, -35, -128, 127)
yellow_color1 = (94, 100, -18, 5, 2, 40)
#交通灯的阈值(经过曝光处理)主要使用这个阈值
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)
#阈值
#===================================================================================================
Flaglight = 0 #标志灯
tim = Timer(4,freq=1)
tim.deinit()
tim1=Timer(3,freq=1)
tim1.deinit()
QR_flag = 0
light_flag = 0
show_numTab = ["0","1","2","3","4","5","6","7","8","9"]
qr_number = 0
traffic_number = 0
#设置标志位
#===================================================================================================
#摄像头识别停止或者识别失败的的协议?
returnData = [0x55,0x02,0x92,0x02,0x02,0x00,0x00,0xBB]
#摄像头正在识别的协议
runData = [0x55,0x02,0x92,0x03,0x02,0x00,0x00,0xBB]
#俩控制摄像头循迹和舵机上下调节的协议
returnData2 = [0x55,0x02,0x91,0x02,0x02,0x00,0x00,0xBB]
runData2 = [0x55,0x02,0x91,0x03,0x02,0x00,0x00,0xBB]
#返回协议
#===================================================================================================
#二维码定时器
def QR_timer(timer):
global QR_flag,qr_number,returnData
print("Timer Delaying")
qr_number = qr_number-1
if(qr_number == 0):
qr_number = 2
QR_flag = 2
tim1.deinit()
#交通灯定时器
def traffic_light_timer(timer):
global light_flag,traffic_number,returnData2
print("Timer Delaying")
traffic_number = traffic_number-1
if(traffic_number == 0):
traffic_number = 9
light_flag = 2
tim.deinit()
#定时器延时10s
#===================================================================================================
#发送函数
def USART_Send(src,length):
for i in range(length):
uart.writechar(src[i])
#发送函数
#===================================================================================================
#二维码 #协议是回传数据 #QR_flag等于1回传,等于2回传识别失败
def Color_Check(srcbuf):
global QR_flag,qr_number
if(QR_flag == 1):
for code in img.find_qrcodes():
QR_flag = 0
tim1.deinit()#停止定时器
print(code)
qr_Tab = code.payload()
if(QR_flag == 2):
QR_flag = 0
#二维码识别函数
#===================================================================================================
#交通灯的核对
def traffic_Check(srcbuf):
global light_flag,traffic_number
if(light_flag == 1):
LED_Red.on()
LED_Green.on()
LED_Blue.on()
LED_Red.off()
LED_Green.off()
LED_Blue.off()
#看参数根据距离更改
#,x_stride =5,y_stride=5 增加识别的速度
# 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)
red = img.find_blobs([red_threshold_01], roi=left_roi, x_stride =5,y_stride=5, pixels_threshold=30, area_threshold=30, merge=True, margin=2)
if green:
LED_Red.off()
LED_Green.on()
LED_Blue.off()
print("绿色")
tim.deinit()
light_flag = 0
elif yellow:
LED_Red.on()
LED_Green.on()
LED_Blue.off()
print("黄色")
tim.deinit()
light_flag = 0
elif red:
LED_Red.on()
LED_Green.off()
LED_Blue.off()
print("红色")
tim.deinit()
light_flag = 0
if(light_flag == 2):
light_flag = 0
#交通灯识别函数(注释的是 识别阈值更低级别 的识别)
#可以连用,但是要在串口发送之前关闭定时器跟标志位
#===================================================================================================
print("准备进入主循环")
#Debug 第1位02是交通灯 01是二维码
# 第2位01是开始 02是关闭识别
Debug = [0x55,0x01,0x01,0x00,0x00,0x00,0x00,0xBB]
while(True):
img = sensor.snapshot()#.lens_corr(1.8)#如果二维码识别不到把注释去掉
img.draw_rectangle( left_roi, color=(255,0,0))
if(len(Debug) >= 5):
if((Debug[0] == 0x55)):
if( (Debug[1] == 0x02) ):
Debug[1]=0x99 #debug用(只执行一便),如果要切换交通灯跟二维码需要重启设备
print("识别交通灯")
if((Debug[2] == 0x01)):
if(light_flag == 0):
light_flag = 1
traffic_number = 9
print("开始识别")
sensor.set_auto_exposure(False, 1000)#这里设置曝光时间
print(sensor.get_exposure_us())
tim.callback(traffic_light_timer)
else:
print("正在识别")
if((Debug[2] == 0x02)):
print("停止识别")
light_flag = 2
tim.deinit()
if( (Debug[1] == 0x01) ):
Debug[1]=0x99 #debug用(只执行一便),如果要切换交通灯跟二维码需要重启设备
print("识别二维码")
if((Debug[2] == 0x01)):
if(QR_flag == 0):
QR_flag = 1
qr_number = 9
print("开始识别")
#这里设置自动曝光
sensor.set_auto_exposure(True)
print("二维码的曝光参数%d"%sensor.get_exposure_us())
tim1.callback(QR_timer)
else:
print("正在识别")
if((Debug[2] == 0x02)):
print("停止识别")
QR_flag = 2
tim1.deinit()
#主循环函数
#===================================================================================================
traffic_Check(data)
Color_Check(data)
lcd.display(img)
#重点循环
#===================================================================================================
uncaught exception in Timer(4) interrupt handler RuntimeError:这定时器多少有点叛逆。
通过串口把协议发送过去之后,这个定时器工作了两三秒之后就会报这个异常
import sensor, image,time,lcd
from pyb import UART,Timer,LED
sensor.reset()
print("初始化摄像头成功")
sensor.set_pixformat(sensor.RGB565)
print("设置彩图成功")
sensor.set_framesize(sensor.QVGA)
print("设置图像像素为QVGA")
sensor.skip_frames(1)#里面如果是数字就是跳过几帧,如果是time=1000就是跳过1s的帧数
print("设置跳帧成功,如果卡摄像头将这句话注释")
sensor.set_auto_whitebal(False)
print("关闭白平衡成功")
sensor.set_auto_gain(False)
print("关闭自动增益成功")
clock = time.clock()
print("记录帧率")
lcd.init()
print("初始化LCE屏幕成功")
uart = UART(3,115200,8,None,1)#初始化串口
print("初始化串口成功")
#进行初始化
#===================================================================================================
data = []#一个列表
LED_Red = LED(1)
LED_Green = LED(2)
LED_Blue = LED(3)
#设置了一个感兴趣区,如果框不住请调整 160宽度是最小的,但是不建议
left_roi= [0,0,240,120]
#第一个模糊阈值
red_color0 = (0, 100, -26, 65, -25, 59)
green_color0 = (0, 100, -73, 21, -40, 65)
yellow_color0 = (43, 100, -37, 58, -22, 72)
#第二个模糊阈值
red_color1 = (55, 98, 35, 59, 0, 19)
green_color1 = (0, 100, -128, -35, -128, 127)
yellow_color1 = (94, 100, -18, 5, 2, 40)
#交通灯的阈值(经过曝光处理)主要使用这个阈值
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)
#阈值
#===================================================================================================
Flaglight = 0 #标志灯
tim = Timer(4,freq=1)
tim.deinit()
QR_flag = 0
light_flag = 0
show_numTab = ["0","1","2","3","4","5","6","7","8","9"]
qr_number = 0
traffic_number = 0
#设置标志位
#===================================================================================================
#摄像头识别停止或者识别失败的的协议?
returnData = [0x55,0x02,0x92,0x02,0x02,0x00,0x00,0xBB]
#摄像头正在识别的协议
runData = [0x55,0x02,0x92,0x03,0x02,0x00,0x00,0xBB]
#俩控制摄像头循迹和舵机上下调节的协议
returnData2 = [0x55,0x02,0x91,0x02,0x02,0x00,0x00,0xBB]
runData2 = [0x55,0x02,0x91,0x03,0x02,0x00,0x00,0xBB]
#返回协议
#===================================================================================================
#二维码定时器
def QR_timer(timer):
global QR_flag,qr_number,returnData
print("Timer Delaying")
qr_number = qr_number-1
if(qr_number == 0):
qr_number = 2
QR_flag = 2
tim.deinit()
#交通灯定时器
def traffic_light_timer(timer):
global light_flag,traffic_number,returnData2
print("Timer Delaying")
traffic_number = traffic_number-1
if(traffic_number == 0):
traffic_number = 9
light_flag = 2
tim.deinit()
#定时器延时10s
#===================================================================================================
#发送函数
def USART_Send(src,length):
for i in range(length):
uart.writechar(src[i])
#发送函数
#===================================================================================================
#二维码 #协议是回传数据 #QR_flag等于1回传,等于2回传识别失败
def Color_Check(srcbuf):
global QR_flag,qr_number
if(QR_flag == 1):
for code in img.find_qrcodes():
QR_flag = 0
tim.deinit()#停止定时器
print(code)
qr_Tab = code.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(QR_flag == 2):
for rdata in returnData:
uart.writechar(rdata)
QR_flag = 0
#二维码识别函数
#===================================================================================================
def find_max(blobs):
max_size=0
for blob in blobs:
if blob.pixels() > max_size:
max_blob = blob
max_size = blob.pixels()
return max_blob #寻找最大的色块
#==================================================================================================
#交通灯的核对
def traffic_Check(srcbuf):
global light_flag,traffic_number
if(light_flag == 1):
#看参数根据距离更改
#,x_stride =5,y_stride=5 增加识别的速度
# 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)
red = img.find_blobs([red_threshold_01], roi=left_roi, x_stride =5,y_stride=5, pixels_threshold=30, area_threshold=30, merge=True, margin=2)
if green:
max_blob1 = find_max(green)
img.draw_edges(max_blob1.min_corners(), color=(0,0,255))
img.draw_cross(max_blob1[5], max_blob1[6],color=(0,0,255))
print("绿色")
tim.deinit()
light_flag = 0
uart.writechar(0x55)
uart.writechar(0x02)
uart.writechar(0x02)
uart.writechar(0x02)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(0x04)
uart.writechar(0xBB)
elif yellow:
max_blob1 = find_max(yellow)
img.draw_edges(max_blob1.min_corners(), color=(0,0,255))
img.draw_cross(max_blob1[5], max_blob1[6],color=(0,0,255))
print("黄色")
tim.deinit()
light_flag = 0
uart.writechar(0x55)
uart.writechar(0x02)
uart.writechar(0x02)
uart.writechar(0x03)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(0x05)
uart.writechar(0xBB)
elif red:
max_blob1 = find_max(red)
img.draw_edges(max_blob1.min_corners(), color=(0,0,255))
img.draw_cross(max_blob1[5], max_blob1[6],color=(0,0,255))
print("红色")
tim.deinit()
light_flag = 0
uart.writechar(0x55)
uart.writechar(0x02)
uart.writechar(0x02)
uart.writechar(0x01)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(0x03)
uart.writechar(0xBB)
#else:
#red = img.find_blobs([red_color1],x_stride=3,y_stride=3)
#yellow = img.find_blobs([yellow_color1],x_stride=3,y_stride=3)
#green = img.find_blobs([green_color1],x_stride=3,y_stride=3)
#if green:
#LED_Red.off()
#LED_Green.on()
#LED_Blue.off()
#tim.deinit()
#uart.writechar(0x55)
#uart.writechar(0x02)
#uart.writechar(0x02)
#uart.writechar(0x02)
#uart.writechar(0x00)
#uart.writechar(0x00)
#uart.writechar(0x04)
#uart.writechar(0xBB)
#elif yellow:
#LED_Red.on()
#LED_Green.on()
#LED_Blue.off()
#tim.deinit()
#uart.writechar(0x55)
#uart.writechar(0x02)
#uart.writechar(0x02)
#uart.writechar(0x03)
#uart.writechar(0x00)
#uart.writechar(0x00)
#uart.writechar(0x05)
#uart.writechar(0xBB)
#elif red:
#LED_Red.on()
#LED_Green.off()
#LED_Blue.off()
#tim.deinit()
#uart.writechar(0x55)
#uart.writechar(0x02)
#uart.writechar(0x02)
#uart.writechar(0x01)
#uart.writechar(0x00)
#uart.writechar(0x00)
#uart.writechar(0x03)
#uart.writechar(0xBB)
##else:
##print("meng yi ge")
##uart.writechar(0x55)
##uart.writechar(0x02)
##uart.writechar(0x02)
##uart.writechar(0x03)
##uart.writechar(0x00)
##uart.writechar(0x00)
##uart.writechar(0x05)
##uart.writechar(0xBB)
if(light_flag == 2):
for rdata in returnData2:
uart.writechar(rdata)
light_flag = 0
#交通灯识别函数(注释的是 识别阈值更低级别 的识别)
#可以连用,但是要在串口发送之前关闭定时器跟标志位
#===================================================================================================
print("准备进入主循环")
#55 02 91 01 00 00 00 BB 交通灯识别串口协议
#55 02 92 01 00 00 00 BB 二维码识别串口协议
while(True):
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[7] == 0xBB)):
if(data[2] == 0x91):
print("识别交通灯")
if(data[3] == 0x01):
if(light_flag == 0):
light_flag = 1
traffic_number = 9
print("开始识别")
sensor.set_auto_exposure(False, 1000)#这里设置曝光时间
print(sensor.get_exposure_us())
tim.callback(traffic_light_timer)
else:
print("正在识别")
for rdata in runData2:
print(rdata)
uart.writechar(rdata)
if(data[3] == 0x02):
print("停止识别")
light_flag = 2
tim.deinit()
if(data[2] == 0x92):
print("识别二维码")
if(data[3] == 0x01):
if(QR_flag == 0):
QR_flag = 1
qr_number = 9
print("开始识别")
#这里设置自动曝光
sensor.set_auto_exposure(True)
print("二维码的曝光参数%d"%sensor.get_exposure_us())
tim.callback(QR_timer)
else:
print("正在识别")
for rdata in runData:
print(rdata)
uart.writechar(rdata)
if(data[3] == 0x02):
print("停止识别")
QR_flag = 2
tim.deinit()
#主循环函数
#===================================================================================================
traffic_Check(data)
Color_Check(data)
lcd.display(img)
#重点循环
#===================================================================================================
请在这里粘贴代码
我想着是 识别二维码 当识别到的时候将一个数组的第三位改变,判断 改变了之后跳出循环,当没识别到的时候numbb一直自减,但是现在没有识别到的时候他numbb会一直减到0才停止,并且在numbb自减的时候摄像头会一直卡住在某一帧的图像(或者有没有什么办法能 又避免卡住摄像头的同时 又能在没有识别到东西的时候进行再次识别,{功能:识别到了之后跳出,没识别到的时候可以转动摄像头调整角度同时识别,规定时间内还是没有识别到时候跳出防止程序卡死})
import sensor, image,time,lcd
from pyb import UART,Timer,LED
sensor.reset()#初始化摄像头
print("初始化摄像头成功")
sensor.set_pixformat(sensor.RGB565)#设置格式,RGB565为彩图
print("初始化格式成功")
sensor.set_framesize(sensor.QVGA)#设置图像像素大小
print("初始化图像类型成功")
#sensor.skip_frames(time = 1000)#里面如果是数字就是跳过几帧,如果是time=1000就是跳过1s的帧数
print("初始化跳帧成功")
sensor.set_auto_whitebal(False)#关闭白平衡
print("关闭白平衡")
sensor.set_auto_gain(False)#关闭自动增益
print("关闭自动增益")
clock = time.clock()#记录帧数
print("记录帧数")
lcd.init()#初始化附带的lcd屏幕
print("初始化lcd屏幕成功")
uart = UART(3,115200,8,None,1)#初始化串口
print("初始化串口成功") #初始化
#===================================================================================================
#交通灯的阈值(经过曝光处理)
red_threshold_01 = (29, 100, 38, 96, 20, 63)
green_threshold_01 = (29, 91, -128, -18, 19, 46)
yellow_threshold_01 = (34, 100, -31, 42, 48, 84)
green_yuzhi=(28, 94, -128, -16, 2, 46)
#阈值
#===================================================================================================
data = []#一个列表
runData = [0x55,0x02,0x92,0x03,0x02,0x00,0x00,0xBB]
if_Check = [0x55,0x00,0x00,0x00,0x00,0x00,0x00,0xBB] #返回的协议
#===================================================================================================
#串口发送函数
def Send(src,length):
for i in range(length):
uart.writechar(src[i]) #串口发送函数
#===================================================================================================
def find_max(blobs): #定义寻找色块面积最大的函数
max_size=0
for blob in blobs:
if blob.pixels() > max_size:
max_blob=blob
max_size = blob.pixels()
return max_blob #寻找面积最大的色块(暂时没用)
#===================================================================================================
def ceshi():
print("ceshi key")
uart.writechar(0x55)
#测试用
#===================================================================================================
#交通灯的核对
def traffic_Check():
#numbb=2000
sensor.set_auto_exposure(False, 800)#这里设置曝光时间
#while(numbb!=0):
#print (numbb)
#if(if_Check[3]!=0x00):
#if_Check.insert(3,0x00)
#numbb=0
#break
blobs = img.find_blobs([red_threshold_01], merge=True, margin=2); #红色物块
blobs1 = img.find_blobs([green_yuzhi], merge=True, margin=2); #绿色物块
blobs2 = img.find_blobs([yellow_threshold_01], merge=True, margin=2); #蓝色物块
if blobs1:
#如果找到了目标lv色
print("绿色")
if_Check.insert(3,0x02)
uart.writechar(0x55)
uart.writechar(0x02)
uart.writechar(0x02)
uart.writechar(0x03)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(0x04)
uart.writechar(0xBB)
elif blobs2:
print("huang色")
if_Check.insert(3,0x03)
uart.writechar(0x55)
uart.writechar(0x02)
uart.writechar(0x02)
uart.writechar(0x02)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(0x05)
uart.writechar(0xBB)
elif blobs:
#如果找到了目标红色
print("hong色")
if_Check.insert(3,0x01)
uart.writechar(0x55)
uart.writechar(0x02)
uart.writechar(0x02)
uart.writechar(0x01)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(0x03)
uart.writechar(0xBB)
else :
Send(if_Check,8)
#numbb=numbb-1 #交通灯识别函数
#===================================================================================================
def Color_Check():
for code in img.find_qrcodes():
qr_message = code.payload()
print(qr_message)
if_Check.insert(3,0x04)
uart.writechar(0x55)
uart.writechar(0x02)
uart.writechar(0x92)
uart.writechar(0x01)
uart.writechar(len(qr_message))
for qrdata in qr_message:
uart.writechar(ord(qrdata))
uart.writechar(0xBB)
#===================================================================================================
#55 02 91 01 00 00 00 BB 交通灯识别串口协议
#55 02 92 01 00 00 00 BB 二维码识别串口协议
print("准备初始化系统")
while(True):
img = sensor.snapshot().lens_corr(1.8)
if(uart.any()):
print("串口连接成功,准备打印串口传输内容")
data = uart.read(8)
print(data)#调试串口输入
if( len(data) >= 8):
if((data[0] == 0x55)and(data[1] == 0x02)and(data[7] == 0xBB)):
print("协议初始化成功")
if(data[2] == 0x91):
print("识别交通灯")
if(data[3] == 0x01):
print("开始识别交通灯")
while(numbb!=0):
print (numbb)
#当第三位有数据的时候跳出循环
if(if_Check[3]!=0x00):
if_Check.insert(3,0x00)
numbb=0
break
#识别交通灯函数
traffic_Check()
if(data[2] == 0x92):
print("识别二维码")
if(data[3] == 0x01 ):
print("开始识别二维码")
sensor.set_auto_exposure(True)#这里设置曝光时间
print(sensor.get_exposure_us())
numbb=2000
while(numbb!=0):
print (numbb)
#当第三位有数据的时候跳出循环
if(if_Check[3]!=0x00):
if_Check.insert(3,0x00)
numbb=0
break
else:
numbb=numbb-1
#识别二维码函数
Color_Check()
请在这里粘贴代码
!!!而且在第一次曝光值的时候图像的亮暗还有点不一样,想知道在一个程序里面可以调用两次不一样的曝光吗!!!
# LOTS OF Blob Detection
import sensor, image, time, math
from pyb import UART
import json
import ustruct
# 如果要保证颜色追踪效果的话, 需要对环境的严格控制
# 晚上光源的冷暖色等,都会对颜色追踪造成很大的影响
# 彩色图片颜色的阈值格式组成, 是由LAB颜色空间的各自最小值与最大值组成
# 点击右侧的颜色空间下拉选择按钮, 默认为RGB Color Space
# 参考右侧的LAB Color Space里面的参数
# (minL, maxL, minA, maxA, minB, maxB)
# 灰度图的阈值格式
# (min, max)
white_threshold_01 = ((95, 100, -18, 3, -8, 4)); #白色阈值
red_threshold_01 = ((59, 10![1_1676624143446_亮的.png](https://fcdn.singtown.com/1fb27ea1-a65a-4394-9dde-15a3c89e9c8e.png) ![0_1676624143444_较暗的.png](https://fcdn.singtown.com/7e827a4b-967f-4f1c-b602-b4337e12393f.png) 0, 38, 96, 20, 63));
green_threshold_01 = ((29, 91, -128, -18, 19, 46));
yellow_threshold_01 = ((34, 100, -31, 42, 48, 84));
runData2 = [0x55,0x02,0x91,0x00,0x02,0x00,0x00,0xBB]
sensor.reset() # 初始化摄像头
sensor.set_pixformat(sensor.RGB565) # 选择像素模式 RGB565.
sensor.set_framesize(sensor.QVGA) # use QQVGA for speed.
sensor.skip_frames(time = 1000)
sensor.set_auto_whitebal(False) #关闭白平衡。白平衡是默认开启的,在颜色识别中,需要关闭白平衡。
sensor.set_auto_gain(False)#关闭自动增益
clock = time.clock() # Tracks FPS.
ifdata = []#一个列表
def find_max(blobs): #定义寻找色块面积最大的函数
max_size=0
for blob in blobs:
if blob.pixels() > max_size:
max_blob=blob
max_size = blob.pixels()
return max_blob
numbb=200
while(numbb!=0):
time.sleep(6)
sensor.set_auto_exposure(False, 1000)#这里设置曝光时间
time.sleep(6)
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # Take a picture and return the image.
# pixels_threshold=100, area_threshold=100
blobs = img.find_blobs([red_threshold_01],roi=(20,20,280,200),x_stride =10,y_stride=10, pixels_threshold=280, area_threshold=200, merge=True, margin=10); #红色物块
blobs1 = img.find_blobs([green_threshold_01],roi=(20,20,280,200),x_stride =10,y_stride=10, pixels_threshold=280, area_threshold=200, merge=True, margin=10); #绿色物块
blobs2 = img.find_blobs([yellow_threshold_01],roi=(20,20,280,200),x_stride =10,y_stride=10, pixels_threshold=280, area_threshold=200, merge=True, margin=10); #蓝色物块
cx=0;cy=0;cx1=0;cy1=0;cx2=0;cy2=0;
if blobs1:
#如果找到了目标lv色
max_g = find_max(blobs1);
# Draw a rect around the blob.
img.draw_rectangle(max_g[0:4]) # rect
#用矩形标记出目标颜色区域
img.draw_cross(max_g[5], max_g[6]) # cx, cy
img.draw_cross(160, 120) # 在中心点画标记
#在目标颜色区域的中心画十字形标记
cx=max_g[5];
cy=max_g[6];
img.draw_line((160,120,cx,cy), color=(127));
#img.draw_string(160,120, "(%d, %d)"%(160,120), color=(127));
img.draw_string(cx, cy, "(%d, %d)"%(cx,cy), color=(127));
print("绿色")
runData2.insert(3,0x01)
print(hex(runData2[3]))
elif blobs2:
#如果找到了目标黄色
max_y = find_max(blobs2);
# Draw a rect around the blob.
img.draw_rectangle(max_y[0:4]) # rect
#用矩形标记出目标颜色区域
img.draw_cross(max_y[5], max_y[6]) # cx, cy
img.draw_cross(160, 120) # 在中心点画标记
#在目标颜色区域的中心画十字形标记
cx=max_y[5];
cy=max_y[6];
img.draw_line((160,120,cx,cy), color=(127));
#img.draw_string(160,120, "(%d, %d)"%(160,120), color=(127));
img.draw_string(cx, cy, "(%d, %d)"%(cx,cy), color=(127));
print("黄色")
runData2.insert(3,0x02)
elif blobs:
#如果找到了目标红色
max_b = find_max(blobs);
# Draw a rect around the blob.
img.draw_rectangle(max_b[0:4]) # rect
#用矩形标记出目标颜色区域
img.draw_cross(max_b[5], max_b[6]) # cx, cy
img.draw_cross(160, 120) # 在中心点画标记
#在目标颜色区域的中心画十字形标记
cx=max_b[5];
cy=max_b[6];
img.draw_line((160,120,cx,cy), color=(127));
#img.draw_string(160,120, "(%d, %d)"%(160,120), color=(127));
img.draw_string(cx, cy, "(%d, %d)"%(cx,cy), color=(127));
print(max_b[4])
print("红色")
runData2.insert(3,0x03)
numbb=numbb-1
print("exposure == %d" % sensor.get_exposure_us())
print(numbb)
if(runData2[3]==0x01 or runData2[3]==0x02 or runData2[3]==0x03):
numbb=0
numbbb=200
while(numbbb!=0):
sensor.set_auto_exposure(False, 10000)#这里设置曝光时间
numbbb=numbbb-1
print("exposure == %d" % sensor.get_exposure_us())
print("over,over,over,over,over,over,over,over,over,over,over,")
#blob = blobs[0]
#img_ball_r= calc_radius(blob)
## 小球离镜头的距离 根据我们的公式计算
#ball_distance = K / img_ball_r
#print("小球距离: %d"%ball_distance)