Salted fish K210 experience notes-object detection

Salted fish K210 experience notes-object detection

Write in front: I am a non-professional, only this kind of enthusiast. Share how novices are experiencing the K210. Getting started stepping on the pit (a variety of jumping pits), I hope the big brother will help fill in one or two ~. (The selected devices are all K210. I want to experience them. I welcome the advice.) Because some hardware used to be basic before, K210 only uses visual related functions.

Object recognition is a very typical application in machine vision. What is to be achieved is to detect various objects in a picture, and then compare it with a known model to determine what the object is.

Constructor

First briefly introduce the KPU of K210. KPU is a neural network processor inside K210. In short, KPU can load and run various ready-made AI algorithm models to achieve various machine vision and other functions. Face recognition in MaixPy is essentially target detection, which is mainly achieved by running the YOLO (You Only Look Once) target detection algorithm on the K210 KPU. Let's take a look at the usage of KPU under MaixPy.

import KPU as kpu
#常用的 KPU 模块导入方法。

Instructions

function parameter
kpu.load(offset or file_path) Load the model. [Offset] The offset of the model stored in flash, such as 0x300000; [file_path] The file name of the model in the file system, such as "xxx.kmodel"
kpu.init_yolo2(kpu_net,threshold,nms_value,anchor_num,anchor) Initialize the yolo2 network; [kpu_net] kpu network object [threshold] probability threshold; [nms_value] box_iou threshold; [anchor_num] trace points; [anchor] trace point parameters are consistent with model parameters.
kpu.run_yolo2(kpu_net,image) Run yolo2 network; [kpu_net] network object returned from kpu_load (); [image] image collected from sensor
kpu.deinit (kpu_net) Anti-initialization. [Kpu_net] kpu network object

Reference address: https://maixpy.sipeed.com/zh/libs/Maix/kpu.html

Follow the steps below to unlock object recognition (comical)

1. Download the model model address
Insert picture description here
Download
Insert picture description here
2. Use kflash slurry to add the model file to the development board, the parameters are as follows
Insert picture description here
3. The download can be successful
Insert picture description here
4. The sample program is also in the 01 technology file: path: sample program-machine vision ——Object detection.

5. Import code

#https://blog.sipeed.com/p/677.html (代码地址)
import sensor,image,lcd,time
import KPU as kpu

lcd.init(freq=15000000)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_vflip(1)     #摄像头后置方式
sensor.run(1)
clock = time.clock()
#模型分类,按照 20class 顺序
#“飞机”、“自行车”、“鸟”、“船”、“瓶子”、“公交车”、“汽车”、“猫”、“椅子”、“牛”、“餐桌”、
#“狗”、“马”、“摩托车”、“人”、“土豆植物”、“羊”、“沙发”、“火车”、“电视监视器”
classes = ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']

#需要将模型(20class.kfpkg)烧写到 flash 的 0x500000 位置
task = kpu.load(0x500000)
anchor = (1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52)
#初始化 yolo2 网络,识别可信概率为 0.5(50%)
a = kpu.init_yolo2(task, 0.5, 0.3, 5, anchor)
while(True):
    clock.tick()
    img = sensor.snapshot()
    code = kpu.run_yolo2(task, img)
    print(clock.fps())
    if code:
        for i in code:
            a=img.draw_rectangle(i.rect())
            a = lcd.display(img)
            for i in code:
                lcd.draw_string(i.x(), i.y(), classes[i.classid()], lcd.RED, lcd.WHITE)
                lcd.draw_string(i.x(), i.y()+12, '%f1.3'%i.value(), lcd.RED, lcd.WHITE)
    else:
        a = lcd.display(img)
a = kpu.deinit(task)

Save the code to the boot.py of the development board ; we can save the connection without connecting the data cable. Just put a battery and it will runInsert picture description here

Achieve effect
Insert picture description here

Run the program and prepare pictures of related objects in 20class. You can see that pyAI-K210 can easily identify related objects.

Published 166 original articles · 22 praises · 10,000+ views

Guess you like

Origin blog.csdn.net/weixin_45020839/article/details/105092097