Last active
November 26, 2020 10:35
-
-
Save kkmonster/32b8dd80190174a50a10003d02cccaf3 to your computer and use it in GitHub Desktop.
t watch : run classifier model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sensor, image, lcd, time | |
import KPU as kpu | |
from fpioa_manager import * | |
from Maix import I2S, GPIO | |
from board import board_info | |
import audio | |
def play_wav(fname): | |
player = audio.Audio(path = fname) | |
player.volume(80) | |
wav_info = player.play_process(wav_dev) | |
wav_dev.channel_config(wav_dev.CHANNEL_1, | |
I2S.TRANSMITTER,resolution = I2S.RESOLUTION_16_BIT, | |
align_mode = I2S.STANDARD_MODE) | |
wav_dev.set_sample_rate(wav_info[1]) | |
while True: | |
ret = player.play() | |
if ret == None: | |
break | |
elif ret == 0: | |
break | |
player.finish() | |
fm.register (board_info.I2S_DA, fm.fpioa.I2S0_OUT_D1) | |
fm.register (board_info.I2S_BCK, fm.fpioa.I2S0_SCLK) | |
fm.register (board_info.I2S_WS, fm.fpioa.I2S0_WS) | |
wav_dev = I2S(I2S.DEVICE_0) | |
sensor.reset() | |
sensor.set_pixformat(sensor.RGB565) | |
sensor.set_framesize(sensor.QVGA) | |
sensor.set_windowing((0, 0, 224, 224)) | |
sensor.set_vflip(1) | |
sensor.run(1) | |
lcd.init(freq=20000000, color=lcd.BLACK) | |
labels=['c1','c2','c3','c4'] #number of labels should match the number of labels the model was trained with | |
task = kpu.load(0x500000) #change to "/sd/name_of_the_model_file.kmodel" if loading from SD card | |
kpu.set_outputs(task, 0, 1, 1, 4) #the actual shape needs to match the last layer shape of your model | |
while(True): | |
#kpu.memtest() | |
img = sensor.snapshot() | |
#img = img.rotation_corr(z_rotation=90.0) uncomment if need rotation correction - only present in full maixpy firmware | |
a = img.pix_to_ai() | |
fmap = kpu.forward(task, img) | |
plist=fmap[:] | |
pmax=max(plist) | |
max_index=plist.index(pmax) | |
if pmax > 0.85 : | |
a = img.draw_string(0,0, str(labels[max_index].strip()), color=(255,0,0), scale=3) | |
a = img.draw_string(0,40, str(pmax), color=(255,0,0), scale=2) | |
print((pmax, labels[max_index].strip())) | |
a = lcd.display(img) | |
if pmax > 0.95 : #if pmax > 0.85 : | |
if max_index == 0: | |
play_wav("/flash/c1.wav") | |
if max_index == 1: | |
play_wav("/flash/c2.wav") | |
if max_index == 2: | |
play_wav("/flash/c3.wav") | |
if max_index == 3: | |
play_wav("/flash/c4.wav") | |
time.sleep_ms(20) | |
a = kpu.deinit(task) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment