nn_stm32cubeai.py

# この作品はMITライセンスの下で提供されています。
# Copyright (c) 2013-2023 OpenMV LLC. 全著作権所有。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# OpenMV上のSTM32 CUBE.AIによる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を計測するためのclockオブジェクトを作成します。

# [CUBE.AI] ネットワークを初期化
net = nn_st.loadnnst("network")

nn_input_sz = 28  # NNの入力は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 CamはIDEに接続されている間、

results matching ""

    No results matching ""