Search code examples
pythonspeech-recognitiongoogle-speech-apispeech-recognition-api

Speech to text in python with a WAV file


I try to convert a speech in a WAV file but I'm stuck here. A lot of tutorial give the same code but it doesn't work for me. Here it is:

import speech_recognition as sr
r = sr.Recognizer()

with sr.AudioFile("hello_world.wav") as source:
    audio = r.record(source)
try:
    s = r.recognize_google(audio)
    print("Text: "+s)
except Exception as e:
    print("Exception: "+str(e))

The "hello_world.wav" file is in the same repertory than the code. I don't have any error. The console:

C:\Users\python.exe "D:/voice_recognition.py"
Exception:

Process finished with exit code 0

Help? :)

(Sorry if my English is bad)


Solution

  • Okay I actually made it work. I post the code that work for me if someone have the same problem:

    import speech_recognition as sr
    r = sr.Recognizer()
    
    hellow=sr.AudioFile('hello_world.wav')
    with hellow as source:
        audio = r.record(source)
    try:
        s = r.recognize_google(audio)
        print("Text: "+s)
    except Exception as e:
        print("Exception: "+str(e))
    

    Maybe it was because I used ' instead of ".