簡體   English   中英

用pyinstaller制作的exe不起作用

[英]Exe made with pyinstaller does not works

我制作了一個簡單的語音識別程序,並嘗試在Pyinstaller的幫助下將exe制作出來,現在當我在計算機上運行該exe時,它可以正常工作並打印來自麥克風的識別音頻,但是當我在另一台計算機上運行該exe時,在* win10 Lenovo IdeaPad 330 *上運行它,即使我在Windows上允許使用麥克風,並且當程序使用麥克風時,該程序也會運行,但不會打印識別的音頻並繼續循環播放,即使任務欄上的小圖標也彈出-UPS 。 現在總結是我的機器上制作的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()

如果沒有麥克風,則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生成可執行文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM