我正在开发一个需要 PyAudio 的 Python 项目,代码如下:
import pyttsx3
import speech_recognition as sr
import os
def take_commands():
r = sr.Recognizer()
with sr.Microphone() as source:
print('Listening')
r.pause_threshold = 0.7
audio = r.listen(source)
try:
print("Recognizing")
Query = r.recognize_google(audio)
print("the query is printed='", Query, "'")
except Exception as e:
print(e)
print("Say that again sir")
return "None"
import time
time.sleep(2)
return Query
def Speak(audio):
engine = pyttsx3.init()
engine.say(audio)
engine.runAndWait()
Speak("Do you want to shutdown your computer sir?")
while True:
command = take_commands()
if "no" in command:
Speak("Thank u sir I will not shut down the computer")
break
if "yes" in command:
# Shutting down
Speak("Shutting the computer")
os.system("shutdown /s /t 30")
break
Speak("Say that again sir")
一开始我无法在 Python 解释器中添加 PyAudio,所以我通过 pip install pyaudio
和 conda install pyaudio
安装了它。我遇到了错误,首先但最后我解决了它,最后我能够从 Python 解释器添加 PyAudio。但是,我仍然无法import pyaudio
,当我运行我的代码时出现以下错误:
Traceback (most recent call last):
File "C:\Users\moham\PycharmProjects\rich\venv\lib\site-packages\speech_recognition\__init__.py", line 107, 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\moham\OneDrive\الصور\Screenshots\ig-followers-master\shutdown.py", line 50, in <module>
command = take_commands()
File "C:\Users\moham\OneDrive\الصور\Screenshots\ig-followers-master\shutdown.py", line 17, in take_commands
with sr.Microphone() as source:
File "C:\Users\moham\PycharmProjects\rich\venv\lib\site-packages\speech_recognition\__init__.py", line 79, in __init__
self.pyaudio_module = self.get_pyaudio()
File "C:\Users\moham\PycharmProjects\rich\venv\lib\site-packages\speech_recognition\__init__.py", line 109, in get_pyaudio
raise AttributeError("Could not find PyAudio; check installation")
AttributeError: Could not find PyAudio; check installation
答案 0 :(得分:0)
我想我在使用 pyaudio 时必须从 https://www.lfd.uci.edu/~gohlke/pythonlibs/ 手动安装 pyaudio。检查您的python版本,下载并安装您需要的版本。
答案 1 :(得分:0)
在您的系统中,可能有多个版本的 Python 可用。 有两种方法可以解决此问题:
pip install pyaudio
py -3.8 -m pip install pyaudio
答案 2 :(得分:0)
试试这个:
首先使用命令 pip uninstall pyaudio 卸载 PyAudio
第二次使用 pip install PyAudio
重新安装它
抱歉,如果这不起作用。
如果有效,欢迎使用。
答案 3 :(得分:0)
windows下pyaudio的安装,试试这个:https://stackoverflow.com/a/55947519/10770878
我只能按照 Tomi Aarnio 在链接评论中的建议在 pipwin
的帮助下让 PyAudio 工作。