get&quot“ oserror:无默认输入设备可用”在Python音频程序中
我正在研究一个项目,以使用Pycharm中的Python 3.10创建语音助手。当程序启动时,会出现错误:
Traceback (most recent call last):
File "D:\alexa.py", line 56, in <module>
query = takecommand().lower()
File "D:\alexa.py", line 37, in takecommand
with sr.Microphone() as source:
File "C:\Users\(redacted)\PycharmProjects\pythonProject8\venv\lib\site-
packages\speech_recognition\__init__.py", line 86, in __init__
device_info = audio.get_device_info_by_index(device_index) if device_index is not None else
audio.get_default_input_device_info()
File "C:\Users\(redacted)\PycharmProjects\pythonProject8\venv\lib\site-packages\pyaudio.py",
line 949, in get_default_input_device_info
device_index = pa.get_default_input_device()
OSError: No Default Input Device Available
Process finished with exit code 1
代码:
import pyttsx3
import datetime
import speech_recognition as sr
import wikipedia
import webbrowser
import os.path
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice',voices[1].id)
def speak(audio):
engine.say(audio)
def wishme():
hour = int(datetime.datetime.now().hour)
if hour>=0 and hour<12:
speak('Доброе утро')
elif hour>12 and hour<18:
speak('Добрый день')
else:
speak('Добрый вечер')
speak(' Я Алекса, ваш голосовой помощник. Теперь многие вещи проще делать говоря со мной ')
def takecommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print('Слушаю...')
r.pause_threshold = 2
audio = r.listen(source)
try:
print('Распознование...')
query = r.recognize_google(audio,language = 'en-in')
print(f'User said: {query}\n')
except Exception as e :
print('Повторите это снова пожалуйста...')
return 'Ничего'
return query
if __name__ == '__main__' :
wishme()
while True:
query = takecommand().lower()
if 'википедия' in query :
speak('Поиск в Википедии....')
query = query.replace('википедия','')
results = wikipedia.summary(query, sentences = 5)
print(results)
speak(results)
elif 'открой ютуб' in query :
webbrowser.open('youtube.com')
elif 'открой гугл' in query :
webbrowser.open('google.com')
elif 'открой вконтакте' in query:
webbrowser.open('vk.com')
elif 'открой почту' in query:
webbrowser.open('mail.ru')
elif 'время' in query :
strtime = datetime.datetime.now().strftime('%H:%M:%S')
speak(f'сейчас {strtime}')
elif 'пайчарм' in query :
codepath = 'C:\Program Files\JetBrains\PyCharm Community
Edition2021.3.2\bin\pycharm64'
os.startfile(codepath)
elif 'выход' in query:
speak('хорошо, пожалуйста позовите меня если вам понадобиться моя помощь')
quit()
我试图解决错误,但没有出现。只有对我来说,没有可用的默认设备,但是我仍然不明白如何修复它,因为不仅存在此错误。接下来我可以尝试什么?
I am working on a project to create a voice assistant using Python 3.10 in pycharm. When the program starts, an error appears:
Traceback (most recent call last):
File "D:\alexa.py", line 56, in <module>
query = takecommand().lower()
File "D:\alexa.py", line 37, in takecommand
with sr.Microphone() as source:
File "C:\Users\(redacted)\PycharmProjects\pythonProject8\venv\lib\site-
packages\speech_recognition\__init__.py", line 86, in __init__
device_info = audio.get_device_info_by_index(device_index) if device_index is not None else
audio.get_default_input_device_info()
File "C:\Users\(redacted)\PycharmProjects\pythonProject8\venv\lib\site-packages\pyaudio.py",
line 949, in get_default_input_device_info
device_index = pa.get_default_input_device()
OSError: No Default Input Device Available
Process finished with exit code 1
Code:
import pyttsx3
import datetime
import speech_recognition as sr
import wikipedia
import webbrowser
import os.path
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice',voices[1].id)
def speak(audio):
engine.say(audio)
def wishme():
hour = int(datetime.datetime.now().hour)
if hour>=0 and hour<12:
speak('Доброе утро')
elif hour>12 and hour<18:
speak('Добрый день')
else:
speak('Добрый вечер')
speak(' Я Алекса, ваш голосовой помощник. Теперь многие вещи проще делать говоря со мной ')
def takecommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print('Слушаю...')
r.pause_threshold = 2
audio = r.listen(source)
try:
print('Распознование...')
query = r.recognize_google(audio,language = 'en-in')
print(f'User said: {query}\n')
except Exception as e :
print('Повторите это снова пожалуйста...')
return 'Ничего'
return query
if __name__ == '__main__' :
wishme()
while True:
query = takecommand().lower()
if 'википедия' in query :
speak('Поиск в Википедии....')
query = query.replace('википедия','')
results = wikipedia.summary(query, sentences = 5)
print(results)
speak(results)
elif 'открой ютуб' in query :
webbrowser.open('youtube.com')
elif 'открой гугл' in query :
webbrowser.open('google.com')
elif 'открой вконтакте' in query:
webbrowser.open('vk.com')
elif 'открой почту' in query:
webbrowser.open('mail.ru')
elif 'время' in query :
strtime = datetime.datetime.now().strftime('%H:%M:%S')
speak(f'сейчас {strtime}')
elif 'пайчарм' in query :
codepath = 'C:\Program Files\JetBrains\PyCharm Community
Edition2021.3.2\bin\pycharm64'
os.startfile(codepath)
elif 'выход' in query:
speak('хорошо, пожалуйста позовите меня если вам понадобиться моя помощь')
quit()
I tried to fix the mistake, but nothing comes out. It's only clear to me that there is no default device available, but I still don't understand how to fix it, given that there is not only this error. What can I try next?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先查看此解决方案。而且(不确定这一点)检查是否已安装了音频设备(例如麦克风),然后检查该设备的驱动程序的安装正确。
First take a look at this solution. And also (not sure about this) check if you have an audio device (like a microphone) installed and then check that device's driver is installed correctly.