加载edgeimpulse模型时候报错,一般识别抓取一两个就会有如此报错,换了几个不同大小模型依然如此
-
import sensor, image, time,os, tf, math, uos, gc from pyb import Pin,Timer,UART import kinematics,robotMoveCmd class Apriltag(): sensor.reset() #初始化摄像头 sensor.set_pixformat(sensor.RGB565) #图像格式为 RGB565 灰度 GRAYSCALE sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240) sensor.set_windowing((240, 240)) # Set 240x240 window. sensor.skip_frames(n=2000) #在更改设置后,跳过n张照片,等待感光元件变稳定 sensor.set_auto_gain(False) #使用颜色识别时需要关闭自动自动增益 sensor.set_auto_whitebal(False)#使用颜色识别时需要关闭自动自动白平衡 uart = UART(3,115200) #设置串口波特率,与stm32一致 uart.init(115200, bits=8, parity=None, stop=1 ) tim = Timer(4, freq=1000) # Frequency in Hz led_dac = tim.channel(1, Timer.PWM, pin=Pin("P7"), pulse_width_percent=50) led_dac.pulse_width_percent(50) #机械臂移动位置 # move_x=0 # move_y=180 # mid_block_cnt=0#用来记录机械臂已对准物块计数,防止误差 # move_status=0#机械臂移动的方式 # oled_show_flag=0#oled显示字符 #用来记录已经抓取到标签 # apriltag_succeed_flag=0 #block_degress=0#机械爪旋转角度 # car_spin_status=0#用来判断小车旋转状态 #机器人运动 robot_move_cmd = robotMoveCmd.RobotMoveCmd() kinematic = kinematics.Kinematics() def init(self,cx=60.5,cy=60.5):#初始化巡线配置,传入两个参数调整中位值 sensor.reset() #初始化摄像头 sensor.set_pixformat(sensor.RGB565) #图像格式为 RGB565 灰度 GRAYSCALE sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240) sensor.set_windowing((240, 240)) # Set 240x240 window. sensor.skip_frames(n=2000) #在更改设置后,跳过n张照片,等待感光元件变稳定 sensor.set_auto_gain(True) #使用颜色识别时需要关闭自动自动增益 sensor.set_auto_whitebal(True)#使用颜色识别时需要关闭自动自动白平衡 self.uart.init(115200, bits=8, parity=None, stop=1 ) self.led_dac.pulse_width_percent(50) self.cap_color_status=0#抓取物块颜色标志,用来判断物块抓取 #机械臂移动位置 self.move_x=0 #机械臂初始状态的X轴坐标 self.move_y=180 #机械臂初始状态的X轴坐标 self.mid_block_cx=cx self.mid_block_cy=cy self.mid_block_cnt=0#用来记录机械臂已对准物块计数,防止误差 self.move_status=1#机械臂移动的方式 self.oled_show_flag=0#oled显示字符 self.block_cnt=0#记录码垛物块数量 #用来记录已经抓取到标签 self.apriltag_succeed_flag=0 self.block_degress=0#机械爪旋转角度 self.car_spin_status=0#用来判断小车旋转状态 self.kinematic.kinematics_move(self.move_x,self.move_y,160,1000) #机械臂初始状态逆运动学 time.sleep_ms(1000) def run_sort(self):#分拣 #物块中心点 block_cx=self.mid_block_cx block_cy=self.mid_block_cy block_read_ID=0#识别到的ID color_read_succed=0#是否识别到颜色 # 获取图像 img = sensor.snapshot() net = None labels = None min_confidence = 0.5 try: # load the model, alloc the model file on the heap if we have at least 64K free after loading net = tf.load("trained.tflite", load_to_fb=uos.stat('trained.tflite')[6] > (gc.mem_free() - (64*1024))) except Exception as e: raise Exception('Failed to load "trained.tflite", did you copy the .tflite and labels.txt file onto the mass-storage device? (' + str(e) + ')') try: labels = [line.rstrip('\n') for line in open("labels.txt")] except Exception as e: raise Exception('Failed to load "labels.txt", did you copy the .tflite and labels.txt file onto the mass-storage device? (' + str(e) + ')') colors = [ # Add more colors if you are detecting more than 7 types of classes at once. 0, 0), ( 0, 255, 0), (255, 255, 0), ( 0, 0, 255), (255, 0, 255), ] clock = time.clock() clock.tick() for i, detection_list in enumerate(net.detect(img, thresholds=[(math.ceil(min_confidence * 255), 255)])): if (i == 0): continue # background class if (len(detection_list) == 0): continue # no detections for this class? print("********** %s **********" % labels[i]) for d in detection_list: [x, y, w, h] = d.rect() center_x = math.floor(x + (w / 2)) center_y = math.floor(y + (h / 2)) print('x %d\ty %d' % (center_x, center_y)) img.draw_circle((center_x, center_y, 12), color=colors[i], thickness=2) block_read_ID=i#识别到的ID print("ID",i) color_read_succed=1 print(clock.fps(), "fps", end="\n\n") #************************************************ 运动机械臂************************************************************************************* if color_read_succed==1:#识别到颜色或者到路口 if self.move_status==1:#第1阶段:机械臂寻找物块位置 if(abs(center_x-self.mid_block_cx)>10): if center_x > self.mid_block_cx: self.move_x+=0.2 else: self.move_x-=0.2 if(abs(center_y-self.mid_block_cy)>10): if center_y > self.mid_block_cy and self.move_y>80: self.move_y-=0.3 else: self.move_y+=0.3 if abs(center_y-self.mid_block_cy)<=10 and abs(center_x-self.mid_block_cx)<=10: #寻找到物块,机械臂进入第二阶段 self.mid_block_cnt += 1 if self.mid_block_cnt>20:#计数100次对准物块,防止误差 self.mid_block_cnt=0 self.move_status=2 else: self.mid_block_cnt=0 self.kinematic.kinematics_move(self.move_x,self.move_y,160,1000) #必须与前面机械臂的初始状态Z轴一致,否者机械臂识别成功率不高 time.sleep_ms(10) elif self.move_status==2:#第2阶段:机械臂抓取物块 self.move_status=3 #判断矩形倾角,改变机械爪 spin_calw=1500 if self.block_degress%90<45: spin_calw=int(1500-self.block_degress%90*500/90) else: spin_calw=int((90-self.block_degress%90)*500/90+1500) if spin_calw>=2500 and spin_calw<=500: spin_calw=1500 self.kinematic.send_str("{{#004P{:0^4}T1000!}}".format(spin_calw))#旋转和张开机械爪 l=math.sqrt(self.move_x*self.move_x+self.move_y*self.move_y) sin=self.move_y/l cos=self.move_x/l print("当前 x 的值为:",self.move_x) print("当前 y 的值为:",self.move_y) self.move_x=(l+90)*cos self.move_y=(l+90)*sin time.sleep_ms(100) self.kinematic.send_str("{#005P500T1000!}") time.sleep_ms(100) self.kinematic.kinematics_move(self.move_x,self.move_y,160,1000)#移动机械臂到物块上方 print("当前 x 的值为:",self.move_x) print("当前 y 的值为:",self.move_y) time.sleep_ms(1200) self.kinematic.kinematics_move(self.move_x,self.move_y,150,1000)#移动机械臂下移到物块 print("当前 x 的值为:",self.move_x) print("当前 y 的值为:",self.move_y) time.sleep_ms(1200) self.kinematic.send_str("{#005P1700T1000!}")#机械爪抓取物块 time.sleep_ms(1200) self.kinematic.send_str("{#004P1500T1000!}")#旋转和张开机械爪 time.sleep_ms(1200) self.move_x=0 self.move_y=160 self.kinematic.kinematics_move(self.move_x,self.move_y,220,1000) self.apriltag_succeed_flag=1#已经抓到标签 if block_read_ID==3 and self.apriltag_succeed_flag==1: time.sleep_ms(1200) self.kinematic.send_str("{#000P2150T2500!}") time.sleep_ms(1200) self.kinematic.send_str("{#001P1304T2500!}") time.sleep_ms(1200) self.kinematic.send_str("{#002P2083T2500!}") time.sleep_ms(1200) self.kinematic.send_str("{#003P900T2500!}") time.sleep_ms(1200) self.kinematic.send_str("{#004P1500T2500!}") time.sleep_ms(1200) self.kinematic.send_str("{#005P1500T2500!}") time.sleep_ms(1200) self.kinematic.send_str("{#005P1000T1000!}")#旋转和张开机械爪 time.sleep_ms(2200) if block_read_ID==2 and self.apriltag_succeed_flag==1: time.sleep_ms(1200) self.kinematic.send_str("{#000P833T2500!}") time.sleep_ms(1200) self.kinematic.send_str("{#001P1240T2500!}") time.sleep_ms(1200) self.kinematic.send_str("{#002P1917T2500!}") time.sleep_ms(1200) self.kinematic.send_str("{#003P833T2500!}") time.sleep_ms(1200) self.kinematic.send_str("{#004P1500T2500!}") time.sleep_ms(1200) self.kinematic.send_str("{#005P1500T1500!}") time.sleep_ms(1200) self.kinematic.send_str("{#005P1000T1000!}")#旋转和张开机械爪 time.sleep_ms(2200) #机械臂复位 # self.move_x=0 #self.move_y=160 #self.kinematic.kinematics_move(self.move_x,self.move_y,220,1000) #time.sleep_ms(1200) #self.robot_move_cmd.car_move(0,0,-500)#小车右转 #self.mid_block_cnt=0 self.move_x=0 #机械臂初始状态的X轴坐标 self.move_y=180 self.apriltag_succeed_flag=0#抓取到标签后颜色分拣 self.kinematic.kinematics_move(self.move_x,self.move_y,160,1000) if self.move_status==3:#第3阶段:机械臂寻找物块位置 if(abs(center_x-self.mid_block_cx)>10): if center_x > self.mid_block_cx: self.move_x+=0.2 else: self.move_x-=0.2 if(abs(center_y-self.mid_block_cy)>10): if center_y > self.mid_block_cy and self.move_y>80: self.move_y-=0.3 else: self.move_y+=0.3 if abs(center_y-self.mid_block_cy)<=10 and abs(center_x-self.mid_block_cx)<=10: #寻找到物块,机械臂进入第二阶段 self.mid_block_cnt += 1 if self.mid_block_cnt>20:#计数100次对准物块,防止误差 self.mid_block_cnt=0 self.move_status=4 else: self.mid_block_cnt=0 self.kinematic.kinematics_move(self.move_x,self.move_y,180,1000) time.sleep_ms(10) elif self.move_status==4:#第4阶段:机械臂抓取物块 self.move_status=5 #判断矩形倾角,改变机械爪 spin_calw=1500 if self.block_degress%90<45: spin_calw=int(1500-self.block_degress%90*500/90) else: spin_calw=int((90-self.block_degress%90)*500/90+1500) if spin_calw>=2500 and spin_calw<=500: spin_calw=1500 self.kinematic.send_str("{{#004P{:0^4}T1000!}}".format(spin_calw))#旋转和张开机械爪 l=math.sqrt(self.move_x*self.move_x+self.move_y*self.move_y) sin=self.move_y/l cos=self.move_x/l print("当前 x 的值为:",self.move_x) print("当前 y 的值为:",self.move_y) self.move_x=(l+110)*cos self.move_y=(l+110)*sin time.sleep_ms(100) self.kinematic.send_str("{#005P500T1000!}") time.sleep_ms(100) self.kinematic.kinematics_move(self.move_x,self.move_y,160,1000)#移动机械臂到物块上方 print("当前 x 的值为:",self.move_x) print("当前 y 的值为:",self.move_y) time.sleep_ms(1200) self.kinematic.kinematics_move(self.move_x,self.move_y,150,1000)#移动机械臂下移到物块 print("当前 x 的值为:",self.move_x) print("当前 y 的值为:",self.move_y) time.sleep_ms(1200) self.kinematic.send_str("{#005P1700T1000!}")#机械爪抓取物块 time.sleep_ms(1200) self.kinematic.send_str("{#004P1500T1000!}")#旋转和张开机械爪 time.sleep_ms(1200) self.move_x=0 self.move_y=160 self.kinematic.kinematics_move(self.move_x,self.move_y,220,1000) self.apriltag_succeed_flag=1#已经抓到标签 if self.cap_color_status==1 and self.apriltag_succeed_flag==1: time.sleep_ms(1200) self.kinematic.send_str("{#000P2150T2500!}") time.sleep_ms(1200) self.kinematic.send_str("{#001P1304T2500!}") time.sleep_ms(1200) self.kinematic.send_str("{#002P2083T2500!}") time.sleep_ms(1200) self.kinematic.send_str("{#003P900T2500!}") time.sleep_ms(1200) self.kinematic.send_str("{#004P1500T2500!}") time.sleep_ms(1200) self.kinematic.send_str("{#005P1500T2500!}") time.sleep_ms(1200) self.kinematic.send_str("{#005P1000T1000!}")#旋转和张开机械爪 if self.cap_color_status==3 and self.apriltag_succeed_flag==1: time.sleep_ms(1200) self.kinematic.send_str("{#000P833T2500!}") time.sleep_ms(1200) self.kinematic.send_str("{#001P1240T2500!}") time.sleep_ms(1200) self.kinematic.send_str("{#002P1917T2500!}") time.sleep_ms(1200) self.kinematic.send_str("{#003P833T2500!}") time.sleep_ms(1200) self.kinematic.send_str("{#004P1500T2500!}") time.sleep_ms(1200) self.kinematic.send_str("{#005P1500T1500!}") time.sleep_ms(1200) self.kinematic.send_str("{#005P1000T1000!}")#旋转和张开机械爪 time.sleep_ms(1200)
-
报错为
Exception: Failed to load "trained.tflite", did you copy the .tflite and labels.txt file onto the mass-storage device? (Arena size is too small for all buffers. Needed 399408 but only 346512 was available,
-
使用的是openmv 4plus
-
你把业务代码都删除,只保留加载模型的程序测试。
-
@kidswong999 单纯运行他自带的ei object detection.py文件的话,运行没问题
-
你的代码我没有办法测试。