|
楼主 |
发表于 2020-5-23 09:11
|
显示全部楼层
9、带提示音提示灯的简单语音识别系统
(红灯后识别二秒钟)
[mw_shl_code=arduino,true]#MicroPython动手做(25)——语音合成与语音识别
#带提示音提示灯的简单语音识别系统(红灯后识别二秒钟)
from mpython import *
import network
import time
import music
import audio
import urequests
import json
import machine
import ubinascii
my_wifi = wifi()
my_wifi.connectWiFi("zh", "zy1567")
def on_button_a_down(_):
time.sleep_ms(10)
if button_a.value() == 1: return
music.play('D5:1')
rgb.fill((int(102), int(0), int(0)))
rgb.write()
time.sleep_ms(1)
oled.fill(0)
oled.DispChar("--语音识别结果--", 18, 13, 1)
oled.DispChar(get_asr_result(2), 28, 33, 1)
oled.show()
time.sleep(2)
oled.fill(0)
oled.DispChar(" 按下A键开始语音识别", 0, 16, 1)
oled.show()
rgb.fill((int(0), int(102), int(0)))
rgb.write()
time.sleep_ms(1)
music.play('G5:1')
def get_asr_result(_time):
audio.recorder_init()
audio.record("temp.wav", int(_time))
audio.recorder_deinit()
_response = urequests.post("http://119.23.66.134:8085/file_upload",
files={"file""temp.wav", "audio/wav")},
params={"appid":"1", "mediatype":"2", "deviceid":ubinascii.hexlify(machine.unique_id()).decode().upper()})
rsp_json = _response.json()
_response.close()
if "text" in rsp_json:
return rsp_json["text"]
elif "Code" in rsp_json:
return "Code:%s" % rsp_json["Code"]
else:
return rsp_json
button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)
rgb.fill((int(0), int(102), int(0)))
rgb.write()
time.sleep_ms(1)
oled.fill(0)
oled.DispChar(" 按下A键开始语音识别", 0, 16, 1)
oled.show()
music.play('G5:1')[/mw_shl_code]
|
|