【问题标题】:How to activate Speech Recognition with a word in python如何在python中使用单词激活语音识别
【发布时间】:2020-08-18 05:34:17
【问题描述】:

目前我正在开发一个虚拟助手项目 其中,它接受语音命令并执行它。

现在,我将他命名为“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)    
    while True:
        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.....

现在即使我这样做了,也会有一些时间上的不幸

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

另一个解释我的问题的好例子是“Alexa”是如何工作的。 Alexa 一直在听家里或周围的一切,但没有反应。每当我说“Alexa”时,她才会做出反应。这就是我想要的……在我的 AI 助手中。

请告诉我如何实现它....

期待帮助....

【问题讨论】:

    标签: python speech-recognition


    【解决方案1】:

    你可以做一个说话功能让他说话,你必须导入 pyttsx3 模块。 通过在终端中运行此命令来安装它。

    pip install pyttsx3
    

    speak 功能有点像这样

    speak("Thank Me Later For this and upvote")
    

    【讨论】:

    • 嗨梅西,感谢您的及时回复,但该答案不能回答我的问题另一件事是pyttsx3模块中没有任何说话功能......
    【解决方案2】:

    您可以尝试执行以下操作:

    wake_word = "Mark"
    
    while True:
        print("Listening")
        text = take_command()
    
        if text.count(wake_word) > 0:
            talk("I am ready")
            text = take_command()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-29
      • 1970-01-01
      相关资源
      最近更新 更多