2

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)

Bojack
  • 111
  • 1
  • 1
  • 8

3 Answers3

4

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 ".

Bojack
  • 111
  • 1
  • 1
  • 8
  • 1
    Hello @Vincent. How to see the text output from the script. I am getting only: Exception: Process finished with exit code 0 – Drago Jan 25 '20 at 17:31
1

Your original code is close; what might be happening is your source variable could have the write scope of the with … as source: block. By ending the with block; you're also unsetting the variables created for that block. If this is the issue, you could:

  1. Create your variables at the script scope (i.e. not within any conditional blocks, such as after r = sr.Recognizer()), and only assign it value within the with block
import speech_recognition as sr
r = sr.Recognizer()
audio = False

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))
  1. Perform all your processing while the audio file is in-scope
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))
  1. As you've done in the accepted solution above; remove the with block and flatten your code structure.
import speech_recognition as sr
r = sr.Recognizer()
audio = r.record(sr.AudioFile("hello_world.wav"))

try:
    s = r.recognize_google(audio)
    print("Text: "+s)
except Exception as e:
    print("Exception: "+str(e))
crampus
  • 11
  • 2
1

Instead of audio = r.record(source) make use of audio = r.listen(source) it worked for me..

here is the link from where I got it. link

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 04 '22 at 15:59