为语音识别库启用音频输入

时间:2020-01-27 10:11:03

标签: python machine-learning nlp speech-recognition pyaudio

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

2 个答案:

答案 0 :(得分:0)

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

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()

答案 1 :(得分:0)

首先,您需要在系统上安装以下物品。 1. Python 2. 语音识别包 3. PyAudio

现在,您可以运行此代码以了解您的版本

import speech_recognition as s_r
print(s_r.__version__)

输出

3.8.1

它将打印您的语音识别包的当前版本。

然后,将麦克风设置为接受声音:

my_mic = s_r.Microphone()

您必须在此处传递参数device_index =?

要识别来自麦克风的输入,必须使用识别器类。让我们来创建一个。

r = s_r.Recognizer()

现在,我将语音转换为Python文本

要使用Google语音识别进行转换,我们可以使用以下行:

r.recognize_google(audio)

它将返回带有一些文本的字符串。 (它将您的声音转换为文本,并以字符串形式返回。

您可以使用以下行简单地打印它:

print(r.recognize_google(audio))

现在完整的程序将如下所示:

import speech_recognition as s_r
print(s_r.__version__) # just to print the version not required
r = s_r.Recognizer()
my_mic = s_r.Microphone(device_index=1) #my device index is 1, you have to put your device index
with my_mic as source:
    print("Say now!!!!")
    audio = r.listen(source) #take voice input from the microphone
print(r.recognize_google(audio)) #to print voice into text

如果运行此命令,则应获得输出。

但是,如果没有任何输出,请稍等片刻,然后检查您的互联网连接。