STM32 CUBE.AI 在 OpenMV MNIST 示例中的应用

# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# STM32 CUBE.AI 在 OpenMV MNIST 示例中的应用
# 参见 https://github.com/openmv/openmv/blob/master/src/stm32cubeai/README.MD

import csi
import time
import nn_st
csi0 = csi.CSI()
csi0.reset()  # 重置并初始化传感器。
csi0.contrast(3)
csi0.brightness(0)
csi0.auto_gain(True)
csi0.auto_exposure(True)
csi0.pixformat(csi.GRAYSCALE)  # 将像素格式设置为灰度
csi0.framesize(csi.QQQVGA)  # 将帧大小设置为80x60
csi0.snapshot(time=2000)  # 等待设置生效。
clock = time.clock()  # 创建一个时钟对象来跟踪FPS。

# [CUBE.AI] 初始化网络
net = nn_st.loadnnst("network")

nn_input_sz = 28  # 神经网络输入为28x28

while True:
    clock.tick()  # 更新FPS时钟。
    img = csi0.snapshot()  # 拍照并返回图像。

    # 在中间裁剪(避免渐晕)
    img.crop(
        (
            img.width() // 2 - nn_input_sz // 2,
            img.height() // 2 - nn_input_sz // 2,
            nn_input_sz,
            nn_input_sz,
        )
    )

    # 二值化图像
    img.midpoint(2, bias=0.5, threshold=True, offset=5, invert=True)

    # [CUBE.AI] 运行推理
    out = net.predict(img)
    print("Network argmax output: {}".format(out.index(max(out))))
    img.draw_string((0, 0), str(out.index(max(out))))
    print(
        "FPS {}".format(clock.fps())
    )  # 注意:OpenMV摄像头在连接至IDE时运行速度会降低约一半。

results matching ""

    No results matching ""