用pyinstaller制作的exe不起作用

时间:2019-07-10 00:17:22

标签: python speech-recognition pyinstaller

我制作了一个简单的语音识别程序,并尝试在Pyinstaller的帮助下将exe制作出来,现在,当我在机器上运行该exe时,它可以正常工作并打印来自麦克风的识别音频,但是当我在另一个上运行它时我在 * win10 Lenovo IdeaPad 330 * 上运行计算机时,即使我已在Windows上允许使用麦克风并在使用该程序时,该程序仍会运行,但不会打印识别的音频并继续循环播放mic任务栏上的小图标也会弹出。 现在总结是我的机器上制作的exe在其他机器上不起作用,为什么? 而且我认为线程可能是重复的,因为我的问题与模块特别相关 有解决办法吗?

这是代码

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()

1 个答案:

答案 0 :(得分:1)

当没有麦克风时,speech_recognition.Microphone()将引发OSError异常,因此您需要抓住它。我建议您创建一个函数以返回source(如果有麦克风),然后在另一个函数上使用它来读取命令。像这样:

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)

最后,运行pyinstaller -F script.py生成可执行文件。