Reputation: 43
I had made a simple speech recognition program and tried to make exe out it with the help of Pyinstaller , now when i run that exe on my machine it works fine and prints the recognized audio from mic but when I run this on another machine as I ran it upon *win10 Lenovo IdeaPad 330 *, the program runs but does not print the recognized audio and keeps on looping, even though I have given permission for mic on windows and when program makes use of mic the small icon in the taskbar too pop-ups . Now summary is that exe made on my machine does not work on others and why? And I think a thread could be possible duplicate as my problem like specifically related to modules Any solution?
here is the code
import speech_recognition
recognizer = speech_recognition.Recognizer()
def listen():
with speech_recognition.Microphone() as source:
print('i m hearing !')
recognizer.adjust_for_ambient_noise(source)
try:
audio = recognizer.listen(
source=source, timeout=5, phrase_time_limit=4)
except speech_recognition.WaitTimeoutError:
pass
try:
print(recognizer.recognize_google(audio))
return recognizer.recognize_google(audio)
except speech_recognition.UnknownValueError:
pass
except Exception as e:
print(e)
if __name__ == '__main__':
while True:
user=str(listen())
if user in ['exit','close','goodbye']:
print('okay goodbye!')
exit()
Upvotes: 1
Views: 951
Reputation: 6031
When there is no Mic the speech_recognition.Microphone()
would raise and OSError
exception, so you need to catch it. I suggest you create a function to return the source
if there is a mic and then use it on another function to read the commands. Something like this:
import speech_recognition
recognizer = speech_recognition.Recognizer()
def get_mic():
try:
source = speech_recognition.Microphone()
return source
except OSError:
return None
def listen(source):
with source as src:
print('i m hearing !')
recognizer.adjust_for_ambient_noise(src)
try:
audio = recognizer.listen(
source=src, timeout=5, phrase_time_limit=4)
except speech_recognition.WaitTimeoutError:
print("speech_recognition.WaitTimeoutError")
return
try:
result = recognizer.recognize_google(audio)
return str(result)
except speech_recognition.UnknownValueError:
print("speech_recognition.UnknownValueError")
return
except Exception as e:
print("Other Exception:", e)
if __name__ == '__main__':
source = get_mic()
if not source:
print("No Mic Device Found!")
exit()
while True:
user = listen(source)
if user in ['exit', 'close', 'goodbye']:
print('okay goodbye!')
exit()
else:
print(user)
And finally, run pyinstaller -F script.py
to generate your executable.
Upvotes: 1