例程讲解25-Machine-Learning->LetNet数字识别

(注意:3.6.5及以后固件删除此例程,使用TensorFlow Lite替代, 效果更好,见视频教程42 - 神经网络目标检测:https://singtown.com/learn/50918/

视频教程35 - Lenet数字识别:https://singtown.com/learn/50543/

运行此例程前,请先在OpenMV IDE->工具->机器视觉->CNN网络库 中,将相应的神经网络文件保存到OpenMV的SD内存卡中哦。

神经网络是一个非常新的功能,目前还处于测试阶段。截至目前2018-08-15,OpenMV IDE中内置的lenet.network网络模型在OpenMV3 M7会超出内存,建议自己训练小一点的lenet模型在OpenMV3 M7使用。在OpenMV4 H7 R1上,是可以使用的。

# LetNet数字识别例程
import sensor, image, time, os, nn

sensor.reset()                         # 复位并初始化传感器。

sensor.set_contrast(3)
sensor.set_pixformat(sensor.GRAYSCALE) # Set pixel format to RGB565 (or GRAYSCALE)
#设置图像色彩格式,有RGB565色彩图和GRAYSCALE灰度图两种

sensor.set_framesize(sensor.QVGA)      # 将图像大小设置为QVGA (320x240)

sensor.set_windowing((128, 128))       # 设置128 x128窗口。
sensor.skip_frames(time=100)
sensor.set_auto_gain(False)
sensor.set_auto_exposure(False)

# 加载lenet神经网络模型
net = nn.load('/lenet.network')
labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

clock = time.clock()                # 创建一个时钟对象来跟踪FPS帧率。
while(True):
    clock.tick()                    # 更新FPS帧率时钟。
    img = sensor.snapshot()         # 拍一张照片并返回图像。
    out = net.forward(img.copy().binary([(150, 255)], invert=True))
    max_idx = out.index(max(out))
    score = int(out[max_idx]*100)
    if (score < 70):
        score_str = "??:??%"
    else:
        score_str = "%s:%d%% "%(labels[max_idx], score)
    img.draw_string(0, 0, score_str)

    print(clock.fps())             
    # 注意: 当连接电脑后,OpenMV会变成一半的速度。当不连接电脑,帧率会增加。
    #打印当前的帧率。

星瞳科技OpenMV官方中文文档函数讲解:

results matching ""

    No results matching ""