# 使用 Google Speech API 进行语音识别

> 原文： [https://pythonspot.com/speech-recognition-using-google-speech-api/](https://pythonspot.com/speech-recognition-using-google-speech-api/)

Google 具有出色的语音识别 API。 该 API 将语音文本（麦克风）转换为书面文本（Python 字符串），即语音到文本。 您只需在麦克风中讲话即可，Google API 会将其翻译成书面文字。 该 API 对于英语具有出色的效果。

Google 还创建了 JavaScript Web Speech API，因此您可以根据需要在 JavaScript 中识别语音，以下是此链接： [https://www.google.com/intl/zh-CN/chrome/demos/speech.html](https://www.google.com/intl/en/chrome/demos/speech.html "Google Web Speech ") 。 要在网络上使用它，您将需要 Google Chrome 25 版或更高版本。

## 安装

Google Speech API v2 is limited to 50 queries per day. Make sure you have a good microphone.
Are you are looking for [text to speech](https://pythonspot.com/speech-engines-with-python-tutorial/) instead?

这是 Ubuntu Linux 的安装指南。 但这可能在其他平台上也很好。 您将需要安装一些软件包：PyAudio，PortAudio 和 SpeechRecognition。 PyAudio 0.2.9 是必需的，您可能需要手动进行编译。

```py
git clone http://people.csail.mit.edu/hubert/git/pyaudio.git
cd pyaudio
sudo python setup.py install
sudo apt-get installl libportaudio-dev
sudo apt-get install python-dev
sudo apt-get install libportaudio0 libportaudio2 libportaudiocpp0 portaudio19-dev
sudo pip3 install SpeechRecognition

```

## 程序

This program will record audio from your microphone, send it to the speech API and return a Python string.

使用语音识别模块记录音频，该模块将包括在程序顶部。 其次，我们将录制的语音发送到 Google 语音识别 API，然后该 API 返回输出。
**r.recognize_google（audio）**返回一个字符串。

```py

#!/usr/bin/env python3
# Requires PyAudio and PySpeech.

import speech_recognition as sr

# Record Audio
r = sr.Recognizer()
with sr.Microphone() as source:
    print("Say something!")
    audio = r.listen(source)

# Speech recognition using Google Speech Recognition
try:
    # for testing purposes, we're just using the default API key
    # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
    # instead of `r.recognize_google(audio)`
    print("You said: " + 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))

```

您可能会喜欢：[个人助理 Jarvis（语音识别和文本到语音）](https://pythonspot.com/personal-assistant-jarvis-in-python/)或[语音引擎](https://pythonspot.com/speech-engines-with-python-tutorial/)