【问题标题】:Enabling Audio Input for Speech Recognition Library为语音识别库启用音频输入
【发布时间】:2020-01-27 20:45:32
【问题描述】:

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

【问题讨论】:

  • 请提供您正在使用的库的更多上下文

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


【解决方案1】:

您可以将您的麦克风用作默认音频输入设备下面是代码 sn-p:

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

【讨论】:

  • 为此,你需要一个pyaudio
【解决方案2】:

首先,您需要在系统上安装以下内容。 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

如果你运行它,你应该得到一个输出。

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

【讨论】: