113.md 2.8 KB
Newer Older
W
init  
wizardforcel 已提交
1 2 3 4 5 6
# 使用 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 对于英语具有出色的效果。

W
wizardforcel 已提交
7
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 Chrome 25 版或更高版本。
W
init  
wizardforcel 已提交
8 9 10

## 安装

W
wizardforcel 已提交
11 12 13
Google Speech API v2 每天最多只能查询 50 个查询。确保您的麦克风良好。

您是否正在寻找[文字转语音](https://pythonspot.com/speech-engines-with-python-tutorial/)
W
init  
wizardforcel 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

这是 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

```

## 程序

W
wizardforcel 已提交
30
该程序将记录来自麦克风的音频,将其发送到语音 API 并返回 Python 字符串。
W
init  
wizardforcel 已提交
31 32

使用语音识别模块记录音频,该模块将包括在程序顶部。 其次,我们将录制的语音发送到 Google 语音识别 API,然后该 API 返回输出。
W
wizardforcel 已提交
33 34

`r.recognize_google(audio)`返回一个字符串。
W
init  
wizardforcel 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

```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/)