使用单词激活python中的语音识别

问题描述

目前,我正在从事Virtual Assistant项目 其中,它接受语音命令并执行。

现在,我将他命名为'MARK'

现在,我已为该代码提供了访问我的麦克风的权限

这是代码.....

import speech_recognition as sr

def takeCommand():
    # It is the recognizer
    r=sr.Recognizer()
    # Kept the microphone as source
    with sr.Microphone() as source: # It should keep listening but only when I say 'Mark'
        print("Listening...")
        r.pause_threshold=1
        audio = r.listen(source)    
    try:
        print("Reconizing...")
        query = r.recognize_google(audio,language="en-in")
        print(f"You said : {query}\n")
    except Exception as error:
        # print(error)
        print('Say that again please')

在此功能的此处,他听到了一切,然后将其转换为文本

但是我不希望他一直在听我讲话,我的意思是,他应该听,但是只有在我说“ MARK”时才回答。

为此,我更改了这样的代码。...

def listen_to_navdeep():
    # It is the recognizer
    r=sr.Recognizer()
    # Kept the microphone as source
    with sr.Microphone() as source: # It should keep listening but only when I say 'Mark'
        r.pause_threshold=1
        audio = r.listen(source)    
    
    try:
        query = r.recognize_google(audio,language="en-in")
        query=query.lower()
        if "mark" in query:
            return query
            break
        else:
            pass
    except:
        pass

# Now I am outside the function....

while True:
    command=listen_to_navdeep
    # Some process runs over here.....

现在,即使我这样做了,也会有一些时光倒流

所以我想做的是,他应该积极地聆听声音,但仍然应该做出响应(也不要打印出没有任何语音的异常)或将其转换为文本并运行一些过程,实际上我告诉“马克”的那一刻,他应该开始录制声音,然后将其处理为文本并执行分配的任何操作(请不要担心它将要执行的任务)

一个解释我的问题的好例子是“ Alexa”的工作方式。 Alexa会继续聆听家里或周围的一切,但没有反应。每当我说“ Alexa”时,她只会做出反应。这就是我想要的....在我的AI助手中。

另一件事是,我不希望答案与我的代码相似,已经足够了,请告诉我在上述情况下如何实现我想做的事情。

提前感谢您的帮助!

- Navdeep

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)