Example: 03-Machine-Learning/01-ST-CubeAI/nn_stm32cubeai.py

# 本作品采用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 sensor
import time
import nn_st

sensor.reset()  # 重置并初始化传感器。
sensor.set_contrast(3)
sensor.set_brightness(0)
sensor.set_auto_gain(True)
sensor.set_auto_exposure(True)
sensor.set_pixformat(sensor.GRAYSCALE)  # 将像素格式设置为灰度
sensor.set_framesize(sensor.QQQVGA)  # 将帧大小设置为80x60
sensor.skip_frames(time=2000)  # 等待设置生效。
clock = time.clock()  # 创建一个时钟对象来跟踪FPS。

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

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

while True:
    clock.tick()  # 更新FPS时钟。
    img = sensor.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 ""