4

我一直在使用 Google Speech Recognition for Python。这是我的代码:

import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
   print("Say something!")
   audio = r.listen(source)
   print(r.recognize_google(audio))

虽然识别非常准确,但需要大约 4-5 秒才能吐出识别出的文字。由于我正在创建语音助手,因此我想修改上面的代码以使语音识别更快。

有什么办法可以将这个数字降低到大约 1-2 秒?如果可能的话,我会尝试像 Siri 和 Ok Google 等服务一样快速识别。

我对python很陌生,所以如果我的问题有一个简单的答案,我深表歉意。

4

2 回答 2

6

您可以使用另一个语音识别程序。例如,您可以在 IBM 建立一个帐户以使用他们的 Watson Speech To Text。如果可能的话,尝试使用他们的 websocket 接口,因为它会在你还在说话的时候主动转录你在说什么。

一个示例(不使用 websockets)将是:

import speech_recognition as sr

# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
    print("Adjusting for background noise. One second")
    r.adjust_for_ambient_noise(source)
    print("Say something!")
    audio = r.listen(source)

IBM_USERNAME = "INSERT IBM SPEECH TO TEXT USERNAME HERE"  # IBM Speech to Text usernames are strings of the form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
IBM_PASSWORD = "INSERT IBM SPEECH TO TEXT PASSWORD HERE"  # IBM Speech to Text passwords are mixed-case alphanumeric strings
try:
    print("IBM Speech to Text thinks you said " + r.recognize_ibm(audio, username=IBM_USERNAME, password=IBM_PASSWORD))
except sr.UnknownValueError:
    print("IBM Speech to Text could not understand audio")
except sr.RequestError as e:
    print("Could not request results from IBM Speech to Text service; {0}".format(e))

你也可以尝试使用pocketsphinx,但就个人而言,我没有特别好的经验。它是离线的(一个加号),但对我来说,并不是特别准确。您可能可以调整一些检测设置并消除一些背景噪音。我相信还有一个训练选项可以将其修改为您的声音,但它看起来并不简单。

一些有用的链接:

语音识别

麦克风识别示例

IBM Watson 语音转文本

祝你好运。一旦语音识别正常工作,它就非常有用和有益!

于 2018-11-14T11:58:35.083 回答
-1

使用正确的输入通道和调整以获得最佳效果:

def speech_to_text():

    required=-1
    for index, name in enumerate(sr.Microphone.list_microphone_names()):
        if "pulse" in name:
            required= index
    r = sr.Recognizer()
    with sr.Microphone(device_index=required) as source:
        r.adjust_for_ambient_noise(source)
        print("Say something!")
        audio = r.listen(source, phrase_time_limit=4)
    try:
        input = r.recognize_google(audio)
        print("You said: " + input)
        return str(input)
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))
于 2020-03-20T14:37:13.797 回答