首页 > 解决方案 > 为语音识别库启用音频输入

问题描述

如何使用语音识别库为所有设备索引打开音频输入?由于我想传递音频进行测试,并且库可能使用不同的音频输入设备。如何让它从所有索引中获取音频输入?

标签: pythonmachine-learningnlpspeech-recognitionpyaudio

解决方案


您可以将麦克风用作默认音频输入设备,下面是代码片段:

import speech_recognition as sr
r=sr.Recognizer() # this is a recognizer which recognize our voice3
with sr.Microphone() as source: # in this we are using a microphone to record our voicecmd
  speak.speak("What can i do for you!") # this a speak invoke method w3hich ask us something
  print("Ask me Something!") # this a print statement which come on console to ask something
  audio=r.listen(source,timeout=60,phrase_time_limit=3)

data = ""
try:
        """
        this is a try block it will recognize it our voice and say what we have told
        """
        data= r.recognize_google(audio,language="en-US")
        print("dynamo think you said!" + "  "+data) # this will print on your console what will going to recognize by google apis
except:
        """
        this is a except block which except the error which come in try block and the code is not able to run it will pass a value
        """
        print("not able to listen you or your microphone is not good")
        exit()

推荐阅读