openmv3会在运行过程就很卡(有时甚至卡住),之后会出现断连,要怎么解决呢?
-
以下是我的代码以及内存使用情况,拜托大佬们了,困扰很多天了,呜呜呜。
import sensor, image, time,math,pyb from pyb import UART,LED import json import ustruct uart = UART(3,115200) #定义串口3变量 uart.init(115200, bits=8, parity=None, stop=1) sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time = 2000) sensor.set_auto_gain(False) #关增益 sensor.set_auto_whitebal(False) #关白平衡 clock = time.clock() up_roi = [0,0,80,15] #上采样区 down_roi = [0,55,80,15] #下采样区 mid_roi = [15,15,50,30] #中心横向采样区 left_roi = [0,0,25,60] #左采样区 right_roi = [55,0,25,40] #右采样区 thresholds = [(30,0,-128,127,-128,127)] #黑色物体阈值,需修改 THRESHOLD = (138,186) #灰度图 黑色物体阈值,可能需修改 class singleline_check(): ok = 0 flag1 = 0 flag2 = 0 rho_err = 0 theta_err = 0 up = singleline_check() down = singleline_check() left = singleline_check() right = singleline_check() line = singleline_check() singleline_check = singleline_check() #线检测数据打包 def pack_linetrack_data(): pack_data = bytearray([0xAA, 0x61, 0X88, singleline_check.rho_err, singleline_check.theta_err]) #清零线检测偏移数据和倾角数据,在未检测到线时,输出为零 singleline_check.rho_err = 0 singleline_check.theta_err = 0 return pack_data def fine_border(img,area,area_roi): #线性回归算法——by最小二乘法;返回一个image.line对象 singleline_check.flag1 = img.get_regression([(255,255)],roi = area_roi,robust = True) if(singleline_check.flag1): area.ok = 1 def found_line(img): singleline_check.flag2 = img.get_regression([(255,255)],robust = True) if(singleline_check.flag2): #求线段偏移量的绝对值 singleline_check.rho_err = abs(singleline_check.flag2.rho())-0 if singleline_check.flag2.theta()>90: #求角度的偏移量 singleline_check.theta_err = singleline_check.flag2.theta()-0 else: singleline_check.theta_err = singleline_check.flag2.theta()-0 #画条直线(颜色可为灰度) img.draw_line(singleline_check.flag2.line(),color = 127) def check_line(img): fine_border(img,up,up_roi) #上边界区域检测 fine_border(img,down,down_roi) #下边界区域检测 fine_border(img,left,left_roi) #左边界区域检测 fine_border(img,right,right_roi) #右边界区域检测 line.flag = 0 if up.ok: line.flag = line.flag | 0x01 #0000 0001将line.flag第1位置置1 if down.ok: line.flag = line.flag | 0x02 #0000 0010 if left.ok: line.flag = line.flag | 0x04 #0000 0100 if right.ok: line.flag = line.flag | 0x08 #0000 1000 #测试,记得屏蔽 print(line.flag) #线检测 found_line(img) #清零标志位 up.ok = down.ok = left.ok = right.ok = 0 #发送数据函数调用 uart.write(pack_linetrack_data()) while(True): clock.tick() sensor.set_pixformat(sensor.GRAYSCALE) img = sensor.snapshot().binary([THRESHOLD]).erode(0) #erode(0) check_line(img) #cx=0;cy=1;cw=3;ch=4; #FH = bytearray([0xAA,0x61,0X88,cx,cy,cw,ch]) #uart.write(FH) #print(cx,cy,cw,ch)
-
原因很简单,就是因为算法调用太多了,cpu算的太慢了。你一个循环内调用了5次get_regression,当然慢。
-
谢谢解答,呜呜呜,含泪去改代码了。