Last active
September 18, 2020 13:17
-
-
Save mongonta0716/6132b64f9957a459324bb4f2de768899 to your computer and use it in GitHub Desktop.
Speech & Image Recognizer for M5StickV(要ファームウェアの最小構成ビルド)
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 | |
import image | |
import lcd | |
import KPU as kpu | |
import json | |
import time | |
from fpioa_manager import * | |
from machine import UART | |
from Maix import I2S, GPIO | |
import struct | |
from modules import SpeechRecognizer | |
import _thread | |
fm.register(35, fm.fpioa.UART2_TX, force=True) | |
fm.register(34, fm.fpioa.UART2_RX, force=True) | |
uart_Port = UART(UART.UART2, 1500000, 8, None, 1, timeout=1000, read_buf_len= 4096) | |
# -------------------- Button and Led | |
fm.register(board_info.BUTTON_A, fm.fpioa.GPIO1) | |
button_a = GPIO(GPIO.GPIO1, GPIO.IN, GPIO.PULL_UP) | |
fm.register(board_info.BUTTON_B, fm.fpioa.GPIO2) | |
button_b = GPIO(GPIO.GPIO2, GPIO.IN, GPIO.PULL_UP) | |
fm.register(board_info.LED_W, fm.fpioa.GPIO3) | |
led_w = GPIO(GPIO.GPIO3, GPIO.OUT) | |
led_w.value(1) #RGBW LEDs are Active Low | |
fm.register(board_info.LED_R, fm.fpioa.GPIO4) | |
led_r = GPIO(GPIO.GPIO4, GPIO.OUT) | |
led_r.value(1) #RGBW LEDs are Active Low | |
fm.register(board_info.LED_G, fm.fpioa.GPIO5) | |
led_g = GPIO(GPIO.GPIO5, GPIO.OUT) | |
led_g.value(1) #RGBW LEDs are Active Low | |
fm.register(board_info.LED_B, fm.fpioa.GPIO6) | |
led_b = GPIO(GPIO.GPIO6, GPIO.OUT) | |
led_b.value(1) #RGBW LEDs are Active Low | |
lcd.init(type=3) | |
lcd.rotation(2) | |
img = image.Image() | |
img.draw_string(0, 0, "FaceDetect\nDemo", scale=2, color=(255,255,255), x_spacing=1, mono_space=1) | |
led_w.value(0) | |
lcd.display(img) | |
time.sleep(3) | |
led_w.value(1) | |
# --------------------- speech Initialize | |
def sr_data_load(s, keyword_num, model_num, frame_num, path): | |
# print(path) | |
with open(path, 'r') as f: | |
data = f.read() | |
s.add_voice_model(keyword_num, model_num, frame_num, data) | |
def sr_data_allload(): | |
sr_data_load(s, 0, 0, 33, "/flash/sr/0_0.sr") | |
sr_data_load(s, 0, 1, 19, "/flash/sr/0_1.sr") | |
sr_data_load(s, 0, 2, 69, "/flash/sr/0_2.sr") | |
sr_data_load(s, 1, 0, 22, "/flash/sr/1_0.sr") | |
sr_data_load(s, 1, 1, 20, "/flash/sr/1_1.sr") | |
sr_data_load(s, 1, 2, 20, "/flash/sr/1_2.sr") | |
sr_data_load(s, 2, 0, 27, "/flash/sr/2_0.sr") | |
sr_data_load(s, 2, 1, 22, "/flash/sr/2_1.sr") | |
sr_data_load(s, 2, 2, 29, "/flash/sr/2_2.sr") | |
print("load successful!") | |
print("voice recognize Init") | |
# register i2s(i2s0) pin | |
fm.register(board_info.MIC_DAT, fm.fpioa.I2S0_IN_D0, force=True) | |
fm.register(board_info.MIC_CLK, fm.fpioa.I2S0_SCLK, force=True) | |
fm.register(board_info.MIC_LRCLK, fm.fpioa.I2S0_WS, force=True) | |
sample_rate = 16000 | |
# init i2s(i2s0) | |
i2s_dev = I2S(I2S.DEVICE_0) | |
# config i2s according to speechrecognizer | |
i2s_dev.channel_config(i2s_dev.CHANNEL_0, | |
I2S.RECEIVER, | |
resolution=I2S.RESOLUTION_16_BIT, | |
cycles=I2S.SCLK_CYCLES_32, | |
align_mode=I2S.STANDARD_MODE) | |
i2s_dev.set_sample_rate(sample_rate) | |
print("------") | |
s = SpeechRecognizer(i2s_dev) | |
print("------") | |
s.set_threshold(0, 0, 15000) # 设置所处环境的噪声阈值, 环境噪声越大设置最后一个参数越 | |
# --------------------- speech Initialize end | |
def image_recognize(): | |
print("image recognize Init") | |
sensor.reset() | |
sensor.set_pixformat(sensor.RGB565) | |
sensor.set_framesize(sensor.QVGA) | |
sensor.set_vflip(False) | |
sensor.run(1) | |
task = kpu.load(0x300000) # you need put model(face.kfpkg) in flash at address 0x300000 | |
anchor = (0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282, 3.52778, 9.77052, 9.16828) | |
a = kpu.init_yolo2(task, 0.5, 0.3, 5, anchor) | |
print("image recognize End") | |
while True: | |
img = sensor.snapshot() | |
img = img.resize(224,224) | |
a = img.pix_to_ai() | |
code = kpu.run_yolo2(task, img) | |
if code: | |
for i in code: | |
print(i) | |
print(type(i)) | |
a = img.draw_rectangle(i.rect())#, fill=True) | |
uart_Port.write(json.dumps(i)) | |
print("Send") | |
time.sleep_ms(100) | |
a = lcd.display(img) | |
def voice_recognize(): | |
sr_data_allload() | |
while True: | |
# recognize | |
s.recognize() | |
ret = s.get_result() | |
if ret > 0: | |
if ret == 1: | |
print("ret:{}-{}".format(ret, "homura")) | |
led_r.value(0) | |
time.sleep(1) | |
led_r.value(1) | |
elif ret == 2: | |
print("ret:{}-{}".format(ret, "konbanwa")) | |
led_g.value(0) | |
time.sleep(1) | |
led_g.value(1) | |
elif ret == 3: | |
print("ret:{}-{}".format(ret, "ohayou")) | |
led_b.value(0) | |
time.sleep(1) | |
led_b.value(1) | |
time.sleep_ms(100) | |
_thread.start_new_thread(image_recognize,()) | |
_thread.start_new_thread(voice_recognize,()) | |
while True: | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment