Example: 03-Machine-Learning/01-ST-CubeAI/nn_stm32cubeai.py
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)
sensor.skip_frames(time=2000)
clock = time.clock()
net = nn_st.loadnnst("network")
nn_input_sz = 28
while True:
clock.tick()
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)
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())
)