Menory Error是什么原因?
-
import sensor, image, time, math, pyb
from pyb import Pin
################变量定义##############
led1 = pyb.LED(1)
led2 = pyb.LED(2)
led3 = pyb.LED(3)
thresholds =[(25, 38, 29, 43, 51, 19),
(26, 46, -41, -20, 38, 10),
(55, 68, -13, 1, -2, 26),
(38, 55, 7, 27, -19, 7)]
r = [(320//2)-(120//2), (240//2)+(50//2), 100, 50] #感兴趣区域(roi=r可以设置检测的范围)
#寻找计数
t =0
flag_grass = 0
flag_green = 0
flag_yellow =0
flag_grey = 0
pin0 = Pin('P0', Pin.IN, Pin.PULL_UP)#灰色值班-高电平检测到-P0-F0
pin1 = Pin('P1', Pin.OUT_PP, Pin.PULL_UP)#草的颜色-高电平检测到-P1-F6
pin2 = Pin('P2', Pin.OUT_PP, Pin.PULL_UP)#绿色纸板-高电平检测到-P2-F7
pin3 = Pin('P3', Pin.OUT_PP, Pin.PULL_UP)#黄色纸板-高电平检测到-P3-F8
pin4 = Pin('P4', Pin.OUT_PP, Pin.PULL_UP)#灰色值班-高电平检测到-P4-F9
#######################################################
############函数定义###################################
#寻找最大色块
#入口参数:色块列表
def find_maxblob(blobs):
max_size=0
for blob in blobs:
if blob.pixels() > max_size:
max_blob=blob
max_size = blob.pixels()
return max_blob
########################################################
############初始化######################################
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()
#########################################################
#####################测试代码#####################
#while(True):
#pass##################主函数#################################
while(True):
if pin0.value()==0:
print("开始识别颜色")
img = sensor.snapshot().lens_corr(1.8)#拍摄一张照片
#寻找小草#连续检测5次,如果都符合判定成功 Pin1 低电平持续两秒
blobs_grass=img.find_blobs([thresholds[0]])
if blobs_grass:
print("找到色块1")
blob = find_maxblob(blobs_grass)
flag_grass==0
if blob.pixels()/(blob.w()*blob.h())>0.60 and blob.pixels()>500:
flag_grass=1
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
print(t)
if flag_grass==1:
t+=1
else:
t=0
if t>5:
print("找到小草")
pin1.value(False)
t=0
time.sleep(2000)
pin1.value(True)
else:
pin1.value(True)
#寻找绿色#连续检测5次,如果都符合判定成功 Pin2 低电平持续两秒
blobs_green=img.find_blobs([thresholds[1]])
if blobs_green:
print("找到色块2")
blob = find_maxblob(blobs_green)
flag_green==0
if blob.pixels()/(blob.w()*blob.h())>0.60 and blob.pixels()>400:
flag_green=1
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
print(t)
if flag_green==1:
t+=1
else:
t=0
if t>5:
print("找到绿色")
pin2.value(False)
t=0
time.sleep(2000)
pin2.value(True)
else:
pin2.value(True)
#寻找黄色#连续检测5次,如果都符合判定成功 Pin2 低电平持续两秒
blobs_yellow=img.find_blobs([thresholds[2]])
if blobs_yellow:
print("找到色块3")
blob = find_maxblob(blobs_yellow)
flag_yellow==0
if blob.pixels()/(blob.w()*blob.h())>0.60 and blob.pixels()>400:
flag_yellow=1
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
print(t)
if flag_yellow==1:
t+=1
else:
t=0
if t>5:
print("找到黄色")
pin3.value(False)
t=0
time.sleep(2000)
pin3.value(True)
else:
pin3.value(True)
#寻找灰色#连续检测5次,如果都符合判定成功 Pin2 低电平持续两秒
blobs_grey=img.find_blobs([thresholds[3]])
if blobs_grey:
print("找到色块4")
blob = find_maxblob(blobs_grey)
flag_grey=0
if blob.pixels()/(blob.w()*blob.h())>0.60 and blob.pixels()>400:
flag_grey=1
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
print(t)
if flag_grey==1:
t+=1
else:
t=0
if t>5:
print("找到灰色")
pin4.value(False)
t=0
time.sleep(2000)
pin4.value(True)
else:
pin4.value(True)#########################################################
-
我直接运行代码没有出现错误。
-
你设置阈值,识别两种颜色以上,运行一会儿就出行memory error。我看了其他的帖子,可能是内存问题吗,是因为识别两种颜色内存没来得及释放吗?QQVGA大概是130K,OpenMv RAM是512K,求解释Eeeeeeem
-
你用的是OpenMV3?换OpenMV4试一下。