Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed Google Speech to Text API in Python program?

I have a project in which I have created a chat program between a client and host, and I have to embed Speech to Text in it. Is there any way by which I can embed Google Speech to Text API in my program ??

like image 710
user2963317 Avatar asked Nov 07 '13 05:11

user2963317


People also ask

How do you implement Speech-to-Text in Python?

Translation of Speech to Text: First, we need to import the library and then initialize it using init() function. This function may take 2 arguments. After initialization, we will make the program speak the text using say() function. This method may also take 2 arguments.

How do I install Google speech recognition in Python?

Google-Speech-API − It can be installed by using the command pip install google-api-python-client. Pyaudio − It can be installed by using pip install Pyaudio command. SpeechRecognition − This package can be installed by using pip install SpeechRecognition.


2 Answers

There is a package in PyPI called Speech Recognition which looks like it will do this. The live (i.e. via microphone) API looks fantastically simple.

# NOTE: this requires PyAudio because it uses the Microphone class
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:                # use the default microphone as the audio source
    audio = r.listen(source)                   # listen for the first phrase and extract it into audio data

try:
    print("You said " + r.recognize(audio))    # recognize speech using Google Speech Recognition
except LookupError:                            # speech is unintelligible
    print("Could not understand audio")

It also has capabilities for transcribing WAV files, running as a background process, providing confidence values for the transcription, etc.

like image 173
Jamie Bull Avatar answered Oct 21 '22 14:10

Jamie Bull


This is probably what you do not want to use in your case, but for other's who may need this for a one-off project, I hacked together a simple python client a while ago that uses the APIs built into Chrome for voice search:

https://github.com/korylprince/python-google-transcribe

For it to work, you must have 16000Hz encoded FLACs, and they have to be fairly short.

Also, like the comments mention, the API is unofficial, so who knows when it will stop working.

like image 30
korylprince Avatar answered Oct 21 '22 14:10

korylprince