Note - Double Click to Copy Code Contact Us!

Speech to Text

Tech Doubility
Speech to Text



HTML
  1. import speech_recognition as sr
  2.  
  3. # Create a recognizer object
  4. r = sr.Recognizer()
  5.  
  6. # Define the audio file path
  7. audio_file = "path/to/audio/file.wav"
  8.  
  9. # Load the audio file
  10. with sr.AudioFile(audio_file) as source:
  11. # Read the audio data from the file
  12. audio_data = r.record(source)
  13.  
  14. # Convert speech to text
  15. text = r.recognize_google(audio_data)
  16.  
  17. # Print the converted text
  18. print(text)
  19.  

Make sure you have the SpeechRecognition library installed before running this code. You can install it using pip:

pip install SpeechRecognition

In the code above, you first create a recognizer object from the SpeechRecognition library. Then you specify the path to your audio file. The code uses the AudioFile class to load the audio data from the file. After that, it passes the audio data to the recognize_google function, which uses Google's speech recognition service to convert the speech to text. Finally, the converted text is printed.

You can replace "path/to/audio/file.wav" with the actual path to your audio file. The audio file should be in WAV format for this example, but the SpeechRecognition library supports various audio formats.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.