使用Speechrecognition在Python中将Blob转换为文本

2024-09-27 21:26:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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方面的经验较少。请帮我解决这个问题。如有解释,将不胜感激。多谢各位


Tags: textapirequestas语音postaudiomethod

热门问题