Q
使用IDE给OPENMV4 H7 PLUS写入代码时,运行到110行net = tf.load("trained.tflite", load_to_fb=uos.stat('trained.tflite')[6] > (gc.mem_free() - (64*1024)))时,IDE会出现Attribute Error:'module' object has no attribute 'load'报错,如何解决呢
'''
openMV1
实现:识别路标
方式:神经网络数据集
'''
#------------------------------------------------------------
##使用库导入##
import sensor, image, time, os, tf, uos, gc
from pyb import UART,Timer
#------------------------------------------------------------
##摄像头初始化##
#像素与图片大小
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)#320*240
#摄像头翻转
sensor.set_vflip (False)
sensor.set_hmirror(False)
#等待配置时间
sensor.skip_frames(time = 1000)
#设置画面
sensor.set_windowing(0,30,320,180)
#------------------------------------------------------------
##通信功能##
#--------------------
##通信初始化
uart = UART(3, 9600) # 定义串口3变量
uart.init(9600, bits=8, parity=None, stop=1)#串口3初始化
#--------------------
##通信协议
def send_data(mode,num,x,y):
start = 0x24
end = 0x23
length = 5
class_num = 0x05 #例程编号
class_group = 0xBB #例程组
data_num = 0x00 #数据量
fenge = 0x2c #逗号
crc = 0 #校验位
data = [] #数据组
#参数都为0
if mode==0 and num==0 and x==0 and y ==0:
pass
else:
#mode(小端模式)
low = mode & 0xFF #低位
high = mode >> 8& 0xFF #高位
data.append(low)
data.append(fenge) #增加","
data.append(high)
data.append(fenge) #增加","
#num(小端模式)
low = num & 0xFF #低位
high = num >> 8& 0xFF #高位
data.append(low)
data.append(fenge) #增加","
data.append(high)
data.append(fenge) #增加","
#x(小端模式)
low = x & 0xFF #低位
high = x >> 8& 0xFF #高位
data.append(low)
data.append(fenge) #增加","
data.append(high)
data.append(fenge) #增加","
#y(小端模式)
low = y & 0xFF #低位
high = y >> 8& 0xFF #高位
data.append(low)
data.append(fenge) #增加","
data.append(high)
data.append(fenge) #增加","
data_num = len(data)
length += len(data)
#print(length)
send_merr = [length,class_num,class_group,data_num]
for i in range(data_num):
send_merr.append(data[i])
#print(send_merr)
#不加上CRC位,进行CRC运算
for i in range(len(send_merr)):
crc +=send_merr[i]
crc = crc%256
send_merr.insert(0,start) #插入头部
send_merr.append(crc)
send_merr.append(end)
#print(send_merr)
global send_buf
send_buf = send_merr
FH=bytearray(send_buf)
print(send_buf)
uart.write(FH)
#--------------------
##发送识别结果1
def send_temp(id):
send_data(1,id,0,0)
#------------------------------------------------------------
##识别功能##
#--------------------
net = tf.load("trained.tflite", load_to_fb=uos.stat('trained.tflite')[6] > (gc.mem_free() - (64*1024)))
labels = [line.rstrip('\n') for line in open("labels.txt")]
idbuffer=[0,0,0,0,0]#阈值清空buffer
#色块识别阈值
red=(26, 100, 29, 127, -37, 52)
blue=(0, 58, -38, 80, -128, -32)
green=(10, 75, -128, 20, 20, 106)
#阈值清空标准,减少误识别概率
buffer1=3
buffer2=3
buffer3=3
buffer4=3
#停止时间,防止识别同一个标识两次
sleep1=800
sleep2=600
sleep3=400
#------------------------------------------------------------
clock = time.clock()
while (True):
clock.tick()
img = sensor.snapshot()#.lens_corr(strength = 1.8, zoom = 1.0)
#视上车情况开抗鱼眼
#对数据集tensor进行匹配
for obj in net.classify(img, min_scale=1.0, scale_mul=0.8, x_overlap=0.5, y_overlap=0.5):
data=obj.output()
if (data[0]>0.35):#数据集匹配率
idbuffer[0]=idbuffer[0]+1
idbuffer[4]=0
elif (data[3]>0.55):
idbuffer[3]=idbuffer[3]+1
idbuffer[4]=0
elif (data[1]>0.67):
idbuffer[1]=idbuffer[1]+1
idbuffer[4]=0
elif (data[2]>0.75):
idbuffer[2]=idbuffer[2]+1
idbuffer[4]=0
else:#无有效匹配值时,idbuffer[4]+1,用以清空idbuffer
idbuffer[4]=idbuffer[4]+1
if idbuffer[0]>=buffer1:#红色标识1
blobs=img.find_blobs([red],pixels_threshold=3, area_threshold=9, merge=True, margin=3)
#对色块进行额外匹配,防止误识别
if blobs:
send_temp(1)
idbuffer=[0,0,0,0,0]
time.sleep_ms(sleep1)
else:
idbuffer[0]=0
elif idbuffer[1]>=buffer2:#蓝色标识2
blobs=img.find_blobs([blue],pixels_threshold=3, area_threshold=9, merge=True, margin=3)
if blobs:
send_temp(2)
idbuffer=[0,0,0,0,0]
time.sleep_ms(sleep2)
else:
idbuffer[1]=0
elif idbuffer[2]>=buffer3:#绿色标识3
blobs=img.find_blobs([green],pixels_threshold=3, area_threshold=9, merge=True, margin=3)
if blobs:
send_temp(3)
idbuffer=[0,0,0,0,0]
time.sleep_ms(sleep3)
else:
idbuffer[2]=0
elif idbuffer[3]>=buffer4:#红色标识停
blobs=img.find_blobs([red],pixels_threshold=3, area_threshold=9, merge=True, margin=3)
if blobs:
send_temp(4)
idbuffer=[0,0,0,0,0]
time.sleep_ms(sleep1)
else:
idbuffer[4]=0
elif idbuffer[4]>=2*buffer1:#无有效识别对象后清空阈值,防止后续干扰
idbuffer=[0,0,0,0,0]
gc.collect()