首页 > 解决方案 > 键盘键按住以运行并释放以暂停 python 中的语音识别器

问题描述

我想按住键(例如 KeyA)启动并释放键暂停

所以基本上当我拿着 KeyA 时,我应该能够说“你好”,并且 Speech_recognizer 将运行,当我释放 KeyA 时,它应该暂停 Speech_recognizer 但仍然能够说出答案。speak() 是文字转语音

就像firestick电视遥控器上的alexa一样,我们必须按住alexa按钮然后说话然后释放按钮

这是我的代码

def takeCommand():
# speech recognizer

r = sr.Recognizer()
with sr.Microphone() as source:
    print("Hearing...")
    # r.pause_threshold = 1
    r.chunk_size = 2048
    r.adjust_for_ambient_noise(source, duration=0.5)
    r.energy_threshold = 300
    audio = r.listen(source)

try:
    print("Recognizing....")
    query = r.recognize_google(audio, language='en-in')
    print(f"You said: {query}\n")

except Exception as e:
    print(e)  # to remove error from console remove this

    print("Say that again please...")
    return "None"
return query
pass

while True:

        query = takeCommand().lower()

        if 'hello' in query:
            speak("Hello, how are you?")

标签: pythonkeyboardspeech-recognitionkeyboard-events

解决方案


推荐阅读