这个模型识别的还可以,离近点 识别率高达0.9
资源:
链接:https://pan.baidu.com/s/1D4ubJGMptqop1x_Nf8KqfQ?pwd=1234
提取码:1234
一:报错解决
报错的意思应该是模型文件错误
原程序可以在sd卡运行,但是在电脑上运行报错:ValueError: [MAIXPY]kpu: load error:2005ERR READ FILE: read file failed
注释掉原来的两句话
加上一句:
main(anchors = anchors, labels=labels, model_addr="/sd/ShuZiMoXing.kmodel")
这样就可以在电脑上调试了
二、串口打印数据
程序没有问题之后,我们可以在程序中加入串口让它输出识别到的消息:
需要在程序中加入:
1、初始化串口
from machine import UART #串口库函数
from fpioa_manager import fm # GPIO重定向函数fm.register(18, fm.fpioa.UART1_TX, force=True)
uart_A = UART(UART.UART1, 115200, 8, 0, 1, timeout=1000, read_buf_len=4096)
2、打印数据
print("value:",labels[obj.classid()],",",obj.value())
现象:
当成功识别到数字的时候,串口会打印识别信息
15-28-23_哔哩哔哩_bilibili
保存程序到开发板,上电自动运行
如图:识别到数字4,概率为0.9
完整程序:
# object detector boot.py
# generated by maixhub.comimport sensor, image, lcd, time
import KPU as kpu
import gc, sys
from machine import UART #串口库函数
from fpioa_manager import fm # GPIO重定向函数fm.register(18, fm.fpioa.UART1_TX, force=True)
uart_A = UART(UART.UART1, 115200, 8, 0, 1, timeout=1000, read_buf_len=4096)def sending_data(x,y,z):FH = bytearray([0x2C,0x12,x,y,z,0x5B])uart_A.write(FH);def lcd_show_except(e):import uioerr_str = uio.StringIO()sys.print_exception(e, err_str)err_str = err_str.getvalue()img = image.Image(size=(224,224))img.draw_string(0, 10, err_str, scale=1, color=(0xff,0x00,0x00))lcd.display(img)def main(anchors, labels = None, model_addr="/sd/m.kmodel", sensor_window=(224, 224), lcd_rotation=0, sensor_hmirror=False, sensor_vflip=False):sensor.reset()sensor.set_pixformat(sensor.RGB565)sensor.set_framesize(sensor.QVGA)sensor.set_windowing(sensor_window)sensor.set_hmirror(sensor_hmirror)sensor.set_vflip(sensor_vflip)sensor.run(1)lcd.init(type=1)lcd.rotation(lcd_rotation)lcd.clear(lcd.WHITE)if not labels:with open('labels.txt','r') as f:exec(f.read())if not labels:print("no labels.txt")img = image.Image(size=(320, 240))img.draw_string(90, 110, "no labels.txt", color=(255, 0, 0), scale=2)lcd.display(img)return 1try:img = image.Image("startup.jpg")lcd.display(img)except Exception:img = image.Image(size=(320, 240))img.draw_string(90, 110, "loading model...", color=(255, 255, 255), scale=2)lcd.display(img)task = kpu.load(model_addr)kpu.init_yolo2(task, 0.5, 0.3, 5, anchors) # threshold:[0,1], nms_value: [0, 1]try:while 1:img = sensor.snapshot()t = time.ticks_ms()objects = kpu.run_yolo2(task, img)t = time.ticks_ms() - tif objects:for obj in objects:pos = obj.rect()img.draw_rectangle(pos)img.draw_string(pos[0], pos[1], "%s : %.2f" %(labels[obj.classid()], obj.value()), scale=2, color=(255, 0, 0))print("value:",labels[obj.classid()],",",obj.value())img.draw_string(0, 200, "t:%dms" %(t), scale=2, color=(255, 0, 0))lcd.display(img)except Exception as e:raise efinally:kpu.deinit(task)if __name__ == "__main__":try:labels = ['1', '2', '3', '4', '5', '6', '7', '8']anchors = [1.40625, 1.8125000000000002, 5.09375, 5.28125, 3.46875, 3.8124999999999996, 2.0, 2.3125, 2.71875, 2.90625]#main(anchors = anchors, labels=labels, model_addr=0x300000, lcd_rotation=2, sensor_window=(224, 224))main(anchors = anchors, labels=labels, model_addr="/sd/ShuZiMoXing.kmodel")#main(anchors = anchors, labels=labels, model_addr="/sd/m.kmodel", lcd_rotation=2, sensor_window=(224, 224))except Exception as e:sys.print_exception(e)lcd_show_except(e)finally:gc.collect()