我正在使用rasa框架开发语音聊天机器人。通过HTML5的mediaRecorder录制语音 并在Flask web服务中作为表单数据发送
@app.route('api/stt',methods = ['POST'])
def STT():
if request.method == "POST":
file = request.files["audioBlob"]
r = sr.Recognizer()
with open("audio.wav", "wb") as aud:
aud_stream = file.read()
aud.write(aud_stream )
# with sr.Microphone() as source:
# print("Say something!")
audio = r.listen(aud)
try:
text = r.recognize_google(audio)
except sr.UnknownValueError:
text = "Google Speech Recognition could not understand audio"
except sr.RequestError as e:
text = "Could not request results from Google Speech Recognition service; {0}".format(e)
return text
html端的代码
mediaRecorder.addEventListener("stop", () => {
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
console.log(audioBlob)
var myHeaders = new Headers();
myHeaders.append("Content-Type", "audio/wave");
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: audioBlob,
redirect: 'follow'
};
// call the STT api
fetch("http://localhost:8000/api/stt", requestOptions)
我想将音频blob直接转换为文本
这是我的代码不起作用。我在python方面的经验较少。请帮我解决这个问题。如有解释,将不胜感激。多谢各位
目前没有回答
相关问题 更多 >
编程相关推荐