python speech_使用Google Speech API进行Python语音识别

﹏ヽ暗。殇╰゛Y 2022-12-07 13:22 463阅读 0赞

c82fdb62f167878ca910def290c433df.png

python speech

In this tutorial you will learn about python speech recognition. There are plenty of options available for this. But Google Speech API is best among all of them.

在本教程中,您将学习python语音识别。 有很多可用的选项。 但是,Google Speech API是其中最好的。

So in this article we are going to see how we can implement Google Speech API in Python.

因此,在本文中,我们将了解如何在Python中实现Google Speech API。

所需的安装文件 (Required Installation Files)

1. Install PyAudio

1.安装PyAudio

PyAudio provides Python bindings for Port Audio, the cross-platform audio I/O library. With PyAudio, you can easily use Python to play and record audio on a variety of platforms. To install PyAudio open terminal or command prompt and type this command and hit enter.

PyAudio为跨平台音频I / O库Port Audio提供Python绑定。 使用PyAudio ,您可以轻松地使用Python在各种平台上播放和录制音频。 要安装PyAudio,请打开终端或命令提示符,然后键入此命令,然后按Enter。

For linux:

对于Linux:

sudo apt-get install python-pyaudio python3-pyaudio**

须藤apt-get install python-pyaudio python3-pyaudio

For windows:

对于Windows:

pip install PyAudio

pip安装PyAudio

This command will install PyAudio for both Python 2 and Python 3.

此命令将为Python 2和Python 3安装PyAudio。

Also Read: Python Text to Speech Example

另请阅读: Python文本到语音示例

2. Install Python Speech Recognition Module

2.安装Python语音识别模块

It is a Library for performing speech recognition, with support for several engines and APIs, online and offline. To install it open terminal or command prompt, type the command mentioned below and hit enter.

它是一个用于执行语音识别的库,支持在线和离线的多个引擎和API。 要安装它,请打开终端或命令提示符,键入以下提到的命令,然后按Enter。

pip install SpeechRecognition

点安装语音识别

Python语音识别程序 (Python Speech Recognition Program)

  1. import speech_recognition as sr
  2. r = sr.Recognizer()
  3. with sr.Microphone() as source:
  4. print("Say something!")
  5. audio = r.listen(source)
  6. try:
  7. text = r.recognize_google(audio)
  8. print("You said: " + text)
  9. except sr.UnknownValueError:
  10. print("Google Speech Recognition could not understand audio")
  11. except sr.RequestError as e:
  12. print("Could not request results from Google Speech Recognition service" + format(e))

Output

输出量

Say something!

说些什么!

You said: hello how are you

你说:你好

I have said “hello are you”. That’s why it is printing the same. You can speak anything you want and this program will record your audio and convert it into text and print it on the screen.

我说过“你好”。 这就是为什么要打印相同的原因。 您可以说任何话,该程序将录制您的音频并将其转换为文本并在屏幕上打印。

Isn’t it awesome.

是不是很棒

Now let’s understand the program.

现在让我们了解程序。

In first line we’re just importing the package speech_recognition as sr. Then we are making a object of class Recognizer to use the methods listen() and recognize_google(). Class Microphone is used to use the default microphone as the audio source.

在第一行中,我们只是将包Speech_recognition导入为sr。 然后,我们使Recognizer类的对象使用方法listen()ognize_google()。 Class Microphone用于将默认麦克风用作音频源。

listen() method will listen for the first phrase and extract it into audio data.

listen()方法将侦听第一个短语并将其提取到音频数据中。

Then we’ll pass that audio in recognize_google() method and this method will return the text generated from that audio.

然后,我们将该音频传递到accept_google ()方法中,该方法将返回从该音频生成的文本。

The reason to put recognize_google method into try – except statements is that if any error occur while recognizing it then we can print that error using except.

尝试identify_google方法放入except语句的原因是,如果在识别它时发生任何错误,则可以使用except打印该错误。

Note: Internet connection will be needed while running this program.

注意:运行该程序时需要Internet连接。

Comment down below if you have any queries related to python speech to text tutorial.

如果您对python语音文本教程有任何疑问,请在下面注释掉。

翻译自: https://www.thecrazyprogrammer.com/2018/08/python-speech-recognition.html

python speech

发表评论

表情:
评论列表 (有 0 条评论,463人围观)

还没有评论,来说两句吧...

相关阅读