python-3.x 语音识别不接受输入

thtygnil  于 2023-01-10  发布在  Python
关注(0)|答案(3)|浏览(103)

我正在使用下面的代码,但它没有接受任何输入。它只是打印侦听,没有发生任何事情,不接受输入,程序也没有终止。

import speech_recognition as sr

r1=sr.Recognizer()
r2=sr.Recognizer()
r3=sr.Recognizer()

with sr.Microphone() as source:
    print('Listening')
    audio = r3.listen(source)
    print(r2.recognize_google(audio))
hl0ma9xz

hl0ma9xz1#

我认为您的麦克风没有检测到。请尝试打印所有输出设备,我肯定您会收到一条消息,说没有找到输出设备。

dohp0rv5

dohp0rv52#

试试这个:

with sr.Recognizer(device_index=1) as source:

作为替代,

with sr.Microphone() as source:

如果尚未安装Pyaudio,请确保已使用命令pip install PyAudio安装Pyaudio

ws51t4hk

ws51t4hk3#

我做过一个类似的项目。你可能想试试这个。

def takeCommand():
    r = sr.Recognizer()

    with sr.Microphone() as source:

        print("Listening...")
        r.pause_threshold = 1
        audio = r.listen(source)

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

相关问题