语音识别返回意外错误

2024-04-26 05:42:19 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我得到如下错误:

     Traceback (most recent call last):
      File "C:\Users\pc\AppData\Local\Programs\Python\Python38-32\lib\site-packages\speech_recognition\__init__.py", line 108, in get_pyaudio
        import pyaudio
    ModuleNotFoundError: No module named 'pyaudio'
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "C:\Users\pc\Desktop\lerconn.py", line 3, in <module>
        with spr.Microphone() as mic:
      File "C:\Users\pc\AppData\Local\Programs\Python\Python38-32\lib\site-packages\speech_recognition\__init__.py", line 79, in __init__
        self.pyaudio_module = self.get_pyaudio()
      File "C:\Users\pc\AppData\Local\Programs\Python\Python38-32\lib\site-packages\speech_recognition\__init__.py", line 110, in get_pyaudio
        raise AttributeError("Could not find PyAudio; check installation")
    AttributeError: Could not find PyAudio; check installation

使用基本语音/声音识别系统:

    import speech_recognition as spr
    with spr.Microphone() as mic:
        try:
            audin = rec.recognize_google(rec.listen(mic), language="tr-TR")
            print(audin)
        except spr.UnknownValueError:
            # response= rand(notexisterrors)
             response = "I don't currently know the word or the phrase."
            
        except spr.RequestError:
            # response = rand(reqerrors)
              response = "Some weird problems with your system."
        else:
            # response = rand(unknownerrors)
             response = "Unknown error, I don't get it."
        print(response)

我试着用PyAudio查看错误,但也不起作用

我使用: Python 3.8.5-文本编辑器:记事本、记事本++和空闲-操作系统:Windows 10 Pro 64位

随便问什么。提前谢谢


Tags: inpygetinitresponselocallinespeech
1条回答
网友
1楼 · 发布于 2024-04-26 05:42:19

我在运行仅安装了SpeechRecognition包的示例时收到了ModuleNotFoundError: No module named 'pyaudio' 消息。如果你用pipwinpyaudio来补充,它似乎是有效的

通过运行以下命令安装:

pip install SpeechRecognition 
pip install pipwin   # Windows
pipwin install pyaudio

然后我遇到了一个小问题,就是找不到正确的麦克风索引,但这对我来说很有用:

import speech_recognition as spr

rec = spr.Recognizer()

# Show all available microphones in system
for index, name in enumerate(spr.Microphone.list_microphone_names()):
   print("Microphone with name \"{1}\" found for `Microphone(device_index={0})`".format(index, name))

# set correct mic
i = 0

response = "It worked!"

with spr.Microphone(device_index=i) as mic: 
   audio = rec.listen(mic, phrase_time_limit=5) # 5 s timer
try:
   
   audin = rec.recognize_google(audio, language="en-US")
   print(audin) # Show result
except spr.UnknownValueError:
   response = "I don't currently know the word or the phrase "
   
except spr.RequestError:
   response = "Some weird prolems with your system."
except Exception as e:
   response = "Error:" + str(e)
   response = "There is some unknown error, dude. I don't get it. Sorry."
else:
   print(response)

相关问题 更多 >