首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复麦克风不能正确接收输入的异常错误?

如何修复麦克风不能正确接收输入的异常错误?
EN

Stack Overflow用户
提问于 2020-04-08 03:34:31
回答 1查看 75关注 0票数 0

我正在构建一个弱的Ai平台,形式是siri和谷歌助手,然而,每次我运行我的代码时,它总是返回“对不起,我没有捕捉到它”-异常而不是通过我的麦克风接收输入。我正在遵循YouTube教程来构建这个平台,因为我是python的新手,因此我不知道问题是什么,因为它似乎对tutorial.Can中的人很好,有人能带我解决这个问题吗?谢谢。

代码语言:javascript
复制
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")
EN

回答 1

Stack Overflow用户

发布于 2021-04-12 14:31:16

下面是更新后的代码@iSavageSkull

代码语言:javascript
复制
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")

您在此处执行的错误:

  1. def takeCommand()下,print(f"user said: {query}\n")缩进错误,应正确缩进。

  1. 下的同一个def takeCommand()

代码语言:javascript
复制
    except Exception as e:
        print("Sorry i didn't catch that...")

在这里,它又是一个缩进错误,这就是为什么它总是保持打印错误,即使代码产生了合法的输出

运行我更新的代码,程序运行正常!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61088027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档