Getting started with K210, model training + MaixPy calling model

Getting started with K210, model training + MaixPy calling model

I use Maix Dock, and I can fine-tune different development boards by myself

Model training

First go to GITHUB to transfer the yolo-for-k210 project to Code Cloud for easy download.

Jump link github →yolo-for-k210

Then look at the author's description of his own step by step to build a good environment and then download good tools, such as: ncc, pay attention to the environment when preparing the pip source replaced with domestic sources! Then follow the prompts step by step...

MaixPY

After converting the trained model to Kmodel, download the corresponding firmware, here I am using the official maixpy_v0.5.0_22_g7ac6b09.bin The corresponding download link is here firmware download link

After downloading the firmware, I burned the firmware and model into the board one by one. I found that the board is not working properly, so here I pack the model and firmware into a file as a new file (.kfpkg)

Insert picture description here
Note here that the address of kmodel is set to 0x300000, and the address of firmware is set to 0x000000. Then burn the .kfpkg into the board.

Then open MaixPY and put the following code into the workspace↓


from fpioa_manager import *
from Maix import GPIO    #从包 Maix 导入了 GPIO 这个类
import sensor,image,lcd,time  #导入sensor、image、lcd、time模块

import KPU as kpu
task = kpu.load(0x300000) #从flash或者系统文件中加载模型,这里为模型在 flash 中的偏移大小为:0x3000000

fm.register(board_info.LED_R, fm.fpioa.GPIO0)   #把 fm.fpioa.GPIO0 注册到了 引脚 board_info.LED_R
led_r=GPIO(GPIO.GPIO0, GPIO.OUT)   #把GPIO0设置成输出

lcd.init(freq=15000000) #初始化LCD(freq为SPI通信速率)

sensor.reset()   #初始化单目摄像头
sensor.set_pixformat(sensor.RGB565) #设置帧格式
sensor.set_framesize(sensor.QVGA)   #设置帧大小

sensor.set_hmirror(1) #开启水平镜像
sensor.set_vflip(1)  #开启垂直镜像

sensor.set_windowing((224, 224))
sensor.set_brightness(2)  #设置亮度(范围为[-2~2])
#sensor.set_contrast(-1)  #设置对比度(范围为[-2,+2])
#sensor.set_auto_gain(1,2) #设置摄像自动增益模式
sensor.run(1)  #图像捕捉控制(1:开始捕捉;0:关闭捕捉)

clock = time.clock() #获取clock对象
classes = ['huanxiong']

anchor = (1, 1.2, 2, 3, 4, 3, 6, 4, 5, 6.5)
a = kpu.init_yolo2(task, 0.17, 0.3, 5, anchor) #为yolov2网络模型传入初始化参数
#(task, 0.17, 0.3, 5, anchor)分别为 kpu网络对象、概率阙值、box_iou门限、锚点数、锚点参数与模型参数一致

while(True):
     clock.tick() #记录开始时间(ms)

     img = sensor.snapshot()    #使用摄像头拍摄一张照片
     #lcd.display(img) #在液晶屏上显示一张 image

     code = kpu.run_yolo2(task, img)  #task为 kpu_load 返回的 kpu_net 对象
                                      #img为从sensor 采集到的图像
                                      #run_yolo2返回的值为kpu_yolo2_find 的列表

     print("FPS当前为:",clock.fps())  #clock.fps于clock.tick搭配使用计算帧率
     if code:
         for i in code:
             a=img.draw_rectangle(i.rect()) #在图像上绘制一个矩形。此处为作为元组传递回坐标框出矩形
                                            #传回的是检测到的图像的四个坐标

             a = lcd.display(img) #在液晶屏上显示被框框框起来的image

             led_r.value(0) #如果检测到物体就亮LED灯
             print("物体是:",classes[i.classid()])#打印出识别的类别
             print("概率为:",100.00*i.value())#打印出置信度

             for i in code:
                 lcd.draw_string(i.x(), i.y(), classes[i.classid()], lcd.RED, lcd.WHITE)
                 #在左上角打印出id
                 lcd.draw_string(i.x(), i.y()+12, '%f1.3'%i.value(), lcd.RED, lcd.WHITE)
                 #在左上角打印出置信度
     else:
         a = lcd.display(img) #如果没有识别出物体,则继续呈现图像
         led_r.value(1)  #如果没有检测到物体就灭LED灯

a = kpu.deinit(task) #反初始化。kpu_load 返回 kpu_net 对象

The LED light code on the control board is added here to realize the function of recognizing that the image LED is on, but not recognizing that the picture LED is off. Later, it can be expanded to recognize that the image communicates with the host computer through wifi.

The training set is a group of dry noodles. The
renderings are as follows:

The target is recognized: the target is Insert picture description here
not recognized:
Insert picture description here
the recognition accuracy of the later model can be adjusted, or you can use YOLOV4 or a better model.

Guess you like

Origin blog.csdn.net/weixin_45010829/article/details/109398701