In this article i want to show you an example of Python Speech Recognition With Google Speech, so Speech Recognition is a library for performing speech recognition, with support for several engines and APIs, online and offline.
Read More on Python GUI
1: PyQt5 GUI Development Tutorials
2: Pyside2 GUI Development Tutorials
3: wxPython GUI Development Tutorials
4: Kivy GUI Development Tutorials
What is Google Speech?
Google Speech library provides a simple and easy-to-use Python interface to different speech recognition engines and APIs, including Google Speech Recognition. you can use SpeechRecognition library in your Python programs to transcribe spoken audio into text. It supports multiple recognition engines, including Google Speech Recognition, which allows you to leverage Google’s powerful speech recognition capabilities in your applications.
This is a brief overview of what you can do with the SpeechRecognition library:
- Audio Input: Capture audio input from different sources such as microphones, audio files, or online streams.
- Speech Recognition: Use the library to recognize and transcribe spoken audio into text in real-time or from recorded audio files.
- Multiple Recognition Engines: Support for multiple recognition engines, including Google Speech Recognition, CMU Sphinx, and more, and it allows you to choose the one that best fits your needs.
- Language Support: Recognize speech in multiple languages and dialects, depending on the capabilities of the underlying recognition engine.
- Simple Interface: Provides a straightforward API for performing speech recognition tasks, making it easy to integrate into your Python applications.
Speech recognition engine/API support
- CMU Sphinx (works offline)
- Google Speech Recognition
- Google Cloud Speech API
- Wit.ai
- Microsoft Bing Voice Recognition
- Houndify API
- IBM Speech to Text
- Snowboy Hotword Detection (works offline)
How to Install Google Speech?
You can use pip for the installation of Google Speech.
1 |
pip install SpeechRecognition |
Also you need to install PyAudio
1 |
pip install pyaudio |
Google Requirements
- Python 2.6, 2.7, or 3.3+ (required)
- PyAudio 0.2.11+ (required only if you need to use microphone input, Microphone)
- PocketSphinx (required only if you need to use the Sphinx recognizer, recognizer_instance.recognize_sphinx)
- Google API Client Library for Python (required only if you need to use the Google Cloud Speech API, recognizer_instance.recognize_google_cloud)
- FLAC encoder (required only if the system is not x86-based Windows/Linux/OS X)
So now this is the complete code for Python Speech Recognition With Google Speech
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# Import the speech_recognition library as sr import speech_recognition as sr # Define the main function def main(): # Create a Recognizer instance r = sr.Recognizer() # Use the default microphone as the audio source with sr.Microphone() as source: # Adjust for ambient noise r.adjust_for_ambient_noise(source) # Prompt the user to say something print("Please say something") # Listen for audio input from the microphone audio = r.listen(source) # Print a message indicating that speech recognition is in progress print("Recognizing Now .... ") # Recognize speech using Google Speech Recognition try: # Print the recognized speech print("You have said \n" + r.recognize_google(audio)) # Print a success message print("Audio Recorded Successfully \n ") # Handle any exceptions that occur during speech recognition except Exception as e: # Print an error message print("Error : " + str(e)) # Write the audio data to a WAV file with open("recorded.wav", "wb") as f: f.write(audio.get_wav_data()) # Call the main function if the script is executed directly if __name__ == "__main__": main() |
This Python code utilizes the speech_recognition library to capture audio input from the default microphone, transcribe it into text using Google Speech Recognition, and save both the audio and the recognized text. It handles errors that may occur during the speech recognition process and provides instructions to install the necessary pyaudio module if it’s missing.
Run the the code and say something, this will be the result
FAQs:
Q: How do I use Google speech recognition in Python?
A: You can use Google speech recognition in Python using the speech_recognition library. This library provides a simple interface to different speech recognition engines and APIs, including Google Speech Recognition. You can install the library using pip install SpeechRecognition and after that use its recognize_google() function to perform speech recognition using Google’s service.
Q: How do I make Python auto speech recognition?
A: To make Python perform auto speech recognition, you can use speech_recognition library along with the pyaudio library for capturing audio input from the microphone. You can create a loop to continuously listen for audio input, perform speech recognition on the captured audio, and then take appropriate actions based on the recognized speech.
How to use Google TTS API in Python?
To use the Google Text-to-Speech (TTS) API in Python, you can use the gTTS library. This library provides a simple interface to the Google TTS API, and it allows you to convert text into speech. You can install the library using pip install gTTS and then use its gTTS() function to create speech from text.
Subscribe and Get Free Video Courses & Articles in your Email
Hello Parwiz,
Referring to your youtube video – PyQt5 Audio To Text Converter With Speech Recognition Library.
How do I input the audio (.wav file) once it is converted to an .exe application?
Can you pls help me with the necessary code?
E.g. Select .wav audio then transcribe it as text.
I am a Psychology student with no technical background but just interested in learning coding 🙂 I Find your videos very useful… Looking forward to your help.
Thank you
Rahul