python - 如何修复我的麦克风无法正确接收输入的异常错误?
问题描述
我正在以 siri 和谷歌助手的形式构建一个弱 Ai 平台,但是,每次我运行我的代码时,它总是返回“对不起,我没有抓住那个”——这个异常而不是通过我的麦克风接收输入。我是按照 YouTube 教程来构建它,因为我对 python 还很陌生,因此我不知道问题是什么,因为它似乎对教程中的人来说工作正常。有人可以指导我解决这个问题吗?谢谢
import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import os
import smtplib
import pythoncom
print("Initializing Karren")
MASTER = "Nadeem"
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
def speak(text):
engine.say(text)
engine.runAndWait()
def wishMe():
hour = int(datetime.datetime.now().hour)
if hour>=0 and hour <12:
speak("Good Morning" + MASTER)
elif hour>=12 and hour<18:
speak("Good Afternoon" + MASTER)
else:
speak("Good Evening" + MASTER)
speak("How may I assist you?")
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = r.listen(source)
try :
print("Recognizing...")
query = r.recognize_google(audio, language ='en-uk')
print(f"user said: {query}\n")
except Exception as e:
print("Sorry i didn't catch that...")
speak("Initializing Karren...")
wishMe()
query = takeCommand()
#Logic
if query:
if 'wikipedia' in query.lower():
speak('Searching wikipedia...')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences =2)
print(results)
speak(results)
if 'open youtube' in query.lower():
webbrowser.open("youtube.com")
解决方案
好吧,这是更新的代码@iSavageSkull
import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import os
import smtplib
import pythoncom
print("Initializing Karren")
MASTER = "Nadeem"
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
def speak(text):
engine.say(text)
engine.runAndWait()
def wishMe():
hour = int(datetime.datetime.now().hour)
if hour>=0 and hour <12:
speak("Good Morning" + MASTER)
elif hour>=12 and hour<18:
speak("Good Afternoon" + MASTER)
else:
speak("Good Evening" + MASTER)
speak("How may I assist you?")
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = r.listen(source)
try :
print("Recognizing...")
query = r.recognize_google(audio, language ='en-uk')
print(f"user said: {query}\n")
except Exception as e:
print("Sorry i didn't catch that...")
speak("Initializing Karren...")
wishMe()
query = takeCommand()
#Logic
if query:
if 'wikipedia' in query.lower():
speak('Searching wikipedia...')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences =2)
print(results)
speak(results)
if 'open youtube' in query.lower():
webbrowser.open("youtube.com")
您在这里所做的错误:
在 下
def takeCommand()
,print(f"user said: {query}\n")
缩进错误,应该正确缩进。同样
def takeCommand()
的下,
except Exception as e:
print("Sorry i didn't catch that...")
这里又是缩进错误,这就是为什么它总是不断打印错误,即使代码产生了合法的输出
运行我更新的你的代码,程序运行良好!!!
推荐阅读
- angular - 如何定义和调用子路由
- javascript - React.js - 自动完成:如何在单击其他区域时隐藏建议 Lst
- angular - 如何使用角度和套接字 io 显示来自 feathersjs 服务器的实时数据
- react-native - 将数据显示为简单的折线图 - 反应原生
- python - 在 Python/Django textarea 字段中检测换行符
- c++ - 如何获取列表中有子节点的树的节点总和?
- canvas - 在three.js / Autodesk 3D Viewer(Autodesk Forge Viewer)中调整对象的大小
- barcode-scanner - 如何在 Zebra 设备上模拟条码扫描
- python - 编码另一个列表中存在的列表元素的最有效方法
- npm - NPM 检测 package.json/package-lock.json 中的预发布依赖项?