HTML
import speech_recognition as sr # Create a recognizer object r = sr.Recognizer() # Define the audio file path audio_file = "path/to/audio/file.wav" # Load the audio file with sr.AudioFile(audio_file) as source: # Read the audio data from the file audio_data = r.record(source) # Convert speech to text text = r.recognize_google(audio_data) # Print the converted text print(text)
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.