• Maixll-Dock 数字识别


    通过数字识别,来学习一下nn模块。顺便说下我病好了(doge)

    咸鱼出品,能用就行~
    选用版本:v831-m2dock-maixpy3-0.5.0-20220601


    在人工智能中,面对大量用户输入的数据/素材,如果要在杂乱无章的内容准确、容易地识别,输出我们期待输出的图像/语音,并不是那么容易的。因此算法就显得尤为重要了。算法就是我们所说的模型。

    我们玩硬件的时候用大佬提供的模型文件就行了。当然也可以自己训练。先学会用再说吧。

    数字识别

    首先去官网下载MaixHub模型文件
    在这里插入图片描述

    将文件放入root/app(U盘里,镜像文件版本0.5.0)
    在这里插入图片描述
    运行main.py,首次请在串口工具下运行,方便调试。

    from time import time
    
    class Number:
        mdsc_path = "./Number.mud" 
        labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']  #标签
        anchors = [1.0, 5.0, 1.35, 5.42, 0.49, 2.55, 0.86, 3.75, 0.65, 4.38] #描点数据匹配度列表
    
        def __init__(self) -> None:
    
            from maix import nn
            self.model = nn.load(self.mdsc_path)
            from maix.nn import decoder
            self.decoder = decoder.Yolo2(len(self.labels) , self.anchors , net_in_size = (224, 224) ,net_out_size = (7,7))
    
        def __del__(self):
            del self.model
            del self.decoder
    
        def cal_fps(self ,start , end):
            one_second = 1
            one_flash = end - start
            fps = one_second / one_flash
            return  fps
    
        def draw_rectangle_with_title(self ,img, box, disp_str , fps ):
            img.draw_rectangle(box[0], box[1], box[0] + box[2], box[1] + box[3],color=(255, 0, 0), thickness=2)
            img.draw_string(box[0], box[1]+ box[3] ,disp_str, scale=2,color=(255, 0, 30), thickness=2)
            img.draw_string(0, 0 ,'FPS :'+str(fps), scale=2 ,color=(0, 0, 255), thickness=2)
    
        def process(self,input):
            t =  time()
            out = self.model.forward(input, quantize=1, layout = "hwc")
            boxes, probs = self.decoder.run(out, nms=0.5, threshold=0.3, img_size=(224,224))
            for i, box in enumerate(boxes):
                class_id = probs[i][0]
                prob = probs[i][1][class_id]
                # disp_str = "{}:{:.2f}%".format(self.labels[class_id], prob*100)
                disp_str = "{}".format(self.labels[class_id])
                fps = self.cal_fps(t, time())
                self.draw_rectangle_with_title(input, box, disp_str, fps)
    
    def main():
        from maix import display, camera
        app = Number()
        camera.config((224,224))
        while True:
            img = camera.capture()
            app.process(img)
            display.show(img)
    
    main()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    效果如下,尽量只识别数字,碰到字母和数字混用的时候,不是很准。
    在这里插入图片描述

    nn模块 会用了就不用看下面了

    maix.nn

    整几个有用的就行~

    基础用法:

    from maix import nn
    
    • 1

    maix.nn.load()

    • model_path: 模型路径, 包含了字符串和字典两种形式
      字典形式:
    {
        "param": "/root/models/sobel_int8.param",
        "bin": "/root/models/sobel_int8.bin"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 字典形式应用如下
       model = {
            "param": "./number_awnn.param",
            "bin": "./number_awnn.bin"         #注意路径
        }
        # opt设置↓
        options = {   
            "model_type":  "awnn",               #模型类别, 目前仅支持 awnn
            "inputs": {
            # 输入层, 字典形式, 关键字是层名称, 为字符串, 如果是加密模型, 需要使用整型
            # 值是层形状, 为一个tuple类型:(h, w, c)
                "input0": (224, 224, 3)        
            },
            "outputs": {                        #输出层, 同理输入层. 支持多层输出	
                "output0": (7, 7, (1+4+len(labels))*5)
            },
            #如果在forward使参数standardize=True(默认为True), 则会使用这个参数对输入数据进行归一化, 计算方法为(x - mean) * norm;
            "mean": [127.5, 127.5, 127.5],    
            "norm": [0.0078125, 0.0078125, 0.0078125],  #定义参见上述mean描述
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 字符串形式:
    model_path = "./sobel_int8.mud"  #注意路径
    
    • 1

    当model_path选择字符串格式时, 此项opt不用进行赋值,默认为None

    MUD文件是模型统一描述文件,点此查看

    maix.nn.Model.forward()

    返回 maix.nn.Model 对象,包含了一系列神经网络操作, maix.nn.load() 会返回其对象。
    只能由具体的对象调用, 不能类直接调用

    inputs: 输入, 可以是_maix_image的Image对象, 也可以是HWC排列的bytes对象, 也可以是HWC排列的numpy.ndarray对象(还未支持), 多层输入使用list(还未支持)
    这个参数未来可能会进行优化
    
    standardize: int 类型,默认为1, 当load()加载字典类型时, opt 的mean,norm参数对数据进行标准化;当load()加载字符串类型时,会根据mud文件自动进行标准化;置为0时不会对输入数据进行处理,则输入前需要将数据进行手动处理,处理方式跟模型训练时的数据处理一致
    
    layout: "hwc" 或者 "chw", 默认设置为 "hwc"
    
    debug: int 类型,默认为0,该值不为0时会打印debug信息和forward的推理耗时
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    maix.nn.decoder

    maix.nn.decoder.Yolo2
    
    • 1

    YOLO V2 的后处理模块, 使用时需要创建一个对象,调用run方法对模型推理输出进行解码得到物体的坐标和类别.

    decoder.run(out, nms=0.5, threshold=0.3, img_size=(224,224))
    
    • 1

    参数

    • fmap: 网路输出的特征图, 一般是forward 函数的结果
    • nms: 非极大值抑制(Non-MaximumSuppression), 用来去掉重复的框, IOU(两个框的重叠区域)大于这个值就只取概率大的一个, 取值范围:[0, 1], 默认值为 0.3
    • threshold: 置信度阈值, 大于这个值才认为物体有效, 取值范围:[0, 1], 默认 0.5
    • img_size: 源图像大小, tuple类型, 比如(320, 240), 这会使返回值的box 坐标自动计算为相对于源图的坐标, 并且类型为整型,默认None 则不会计算, box 返回相对值(百分比), 浮点类型, 范围:[0, 1]

    返回值
    [boxes, probs], list 类型, 可以参考上面的使用例子, 其中:

    • boxes: list 类型, 每个元素也是一个list, 包含[x, y, w, h], 分别代表了框左上角坐标, 以及框的宽高
    • probs: list 类型, 每个元素也是一个list, 包含[max_prob_idx, all_classes_probs]
      1. all_classes_probs: list 类型, 包含了该框所有类别的置信度
      2. max_prob_idx: 整型,代表了all_classes_probs中最大值的下标, 取值范围: [0, classes_num - 1]
  • 相关阅读:
    Double 4 VR智能互动系统在法律法庭上的模拟演练教学
    从PMP理论看华为销售项目运作与管理
    awk语法-01-基础语法(命令、选项、内部变量)
    Django-filter
    NAT 概述
    【云原生】Docker的初步认识,安装与基本操作
    opencv+tesseract完成验证码识别(识别率99.99%)
    电子学:第010课——实验 8:继电振荡器
    流媒体直播播放协议:HLS、RTMP、HTTP-FLV
    kubernetes集群最新版安装
  • 原文地址:https://blog.csdn.net/weixin_45020839/article/details/126303731