编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

使用Google Speech API在Python中进行语音识别

wxchong 2024-10-12 12:24:57 开源技术 14 ℃ 0 评论

语音识别是家庭自动化、人工智能等应用中的一个重要特性,本文介绍了如何利用Python语言识别库。

所需的组件

必须安装以下组件:

1)Python语音识别模块:

sudo pip install SpeechRecognition

2)PyAudio:对Linux用户使用以下命令

sudo apt-get install python-pyaudio python3-pyaudio

如果存储库中的版本太旧,请使用以下命令安装pyaudio

sudo apt-get install portaudio19-dev python-all-dev python3-all-dev && 
sudo pip install pyaudio

对于Python3,请使用pip3而不是pip。

Windows用户可以通过在终端中执行以下命令来安装pyaudio

pip install pyaudio

使用麦克风的语音输入和语音到文本的翻译

1)配置麦克风(用于外部麦克风):建议在程序执行期间指定麦克风,以免出现毛刺。

在终端中键入lsusb。将显示已连接设备的列表。麦克风名称如下所示

USB Device 0x46d:0x825: Audio (hw:1, 0)

记下该注释,因为它将在程序中使用。

2)设置块大小(Chunk Size):这基本上涉及指定我们一次要读取多少字节的数据。 通常,此值以2的幂指定,例如1024或2048。

3)设置采样率(Sampling Rate):采样率定义记录值进行处理的频率。

4)将设备ID设置为选定的麦克风:在此步骤中,我们指定要使用的麦克风的设备ID,以避免在存在多个麦克风的情况下产生歧义。从某种意义上说,这也有助于调试,在运行程序时,我们将知道是否已识别指定的麦克风。在编程期间,我们指定参数device_id。 如果未识别到麦克风,程序将提示找不到device_id。

5)允许调整环境噪声:由于周围的噪声会发生变化,因此我们必须让程序稍等一秒钟来调整录音的能量阈值,以便根据外部噪声水平对其进行调整。

6)语音到文本的翻译:这是在Google语音识别的帮助下完成的。这需要有效的Internet连接才能工作。但是,有某些脱机识别系统,例如PocketSphinx,但是安装过程非常严格,需要几个依赖项。Google语音识别是最容易使用的一种。

上述步骤实施如下:

#Python 2.x program for Speech Recognition 
  
import speech_recognition as sr 
  
#enter the name of usb microphone that you found 
#using lsusb 
#the following name is only used as an example 
mic_name = "USB Device 0x46d:0x825: Audio (hw:1, 0)"
#Sample rate is how often values are recorded 
sample_rate = 48000
#Chunk is like a buffer. It stores 2048 samples (bytes of data) 
#here.  
#it is advisable to use powers of 2 such as 1024 or 2048 
chunk_size = 2048
#Initialize the recognizer 
r = sr.Recognizer() 
  
#generate a list of all audio cards/microphones 
mic_list = sr.Microphone.list_microphone_names() 
  
#the following loop aims to set the device ID of the mic that 
#we specifically want to use to avoid ambiguity. 
for i, microphone_name in enumerate(mic_list): 
    if microphone_name == mic_name: 
        device_id = i 
  
#use the microphone as source for input. Here, we also specify  
#which device ID to specifically look for incase the microphone  
#is not working, an error will pop up saying "device_id undefined" 
with sr.Microphone(device_index = device_id, sample_rate = sample_rate,  
                        chunk_size = chunk_size) as source: 
    #wait for a second to let the recognizer adjust the  
    #energy threshold based on the surrounding noise level 
    r.adjust_for_ambient_noise(source) 
    print "Say Something"
    #listens for the user's input 
    audio = r.listen(source) 
          
    try: 
        text = r.recognize_google(audio) 
        print "you said: " + text 
      
    #error occurs when google could not understand what was said 
      
    except sr.UnknownValueError: 
        print("Google Speech Recognition could not understand audio") 
      
    except sr.RequestError as e: 
        print("Could not request results from Google  
                                 Speech Recognition service; {0}".format(e)) 

将音频文件转录为文本

如果我们有一个要转换为文本的音频文件,我们只需要将源替换为音频文件而不是麦克风。

为了方便起见,请将音频文件和程序放在同一文件夹中。这适用于FLAC文件的WAV、AIFF。

实现如下所示

#Python 2.x program to transcribe an Audio file 
import speech_recognition as sr 
  
AUDIO_FILE = ("example.wav") 
  
# use the audio file as the audio source 
  
r = sr.Recognizer() 
  
with sr.AudioFile(AUDIO_FILE) as source: 
    #reads the audio file. Here we use record instead of 
    #listen 
    audio = r.record(source)   
  
try: 
    print("The audio file contains: " + r.recognize_google(audio)) 
  
except sr.UnknownValueError: 
    print("Google Speech Recognition could not understand audio") 
  
except sr.RequestError as e: 
    print("Could not request results from Google Speech  
              Recognition service; {0}".format(e)) 

故障排除

通常会遇到以下问题

1)麦克风静音:这导致无法接收输入。要检查这一点,可以使用alsamixer。

它可以使用

sudo apt-get install libasound2 alsa-utils alsa-oss

键入amixer。输出将看起来像这样

Simple mixer control 'Master', 0
  Capabilities: pvolume pswitch pswitch-joined
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 65536
  Mono:
  Front Left: Playback 41855 [64%] [on]
  Front Right: Playback 65536 [100%] [on]
Simple mixer control 'Capture', 0
  Capabilities: cvolume cswitch cswitch-joined
  Capture channels: Front Left - Front Right
  Limits: Capture 0 - 65536
  Front Left: Capture 0 [0%] [off] #switched off
  Front Right: Capture 0 [0%] [off]

如您所见,捕获设备当前已关闭。要打开它,请键入alsamixer。

如您在第一张图片中所看到的,它正在显示我们的播放设备。按F4键切换到“捕获设备”。

在第二张图片中,突出显示的部分显示捕获设备已静音。要取消静音,请按空格键

如上图所示,突出显示的部分确认捕获设备未静音。

2)未选择当前麦克风作为捕获设备

在这种情况下,可以通过键入alsamixer并选择声卡来设置麦克风。在这里,您可以选择默认的麦克风设备。

如图所示,突出显示部分是您必须选择声卡的地方。

第二张图片显示了声卡的屏幕选择

3)没有Internet连接:语音到文本的转换需要有效的Internet连接。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表