让Python语音识别更快

4

我一直在使用Python的Google语音识别。以下是我的代码:

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非常陌生,所以如果我的问题有一个简单的答案,那么请谅解。

我也遇到了wit.ai的同样问题。 - Dadu Khan
2个回答

6
你可以使用其他语音识别程序,例如,你可以创建一个IBM账户并使用他们的Watson语音转文本服务。如果可能,请尝试使用他们的websocket接口,因为这样它可以在你说话时主动转录你所说的内容。
以下是一个不使用websocket的示例:
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语音转文本 祝好运。一旦语音识别正常工作,它就非常有用和有益!

嗨,O Harris,我是IBM Watson语音转文本用户,想知道如何提高模型的准确性以识别Python中的单词。请参考我的帖子https://dev59.com/DsDqa4cB1Zd3GeqPUAOP?noredirect=1#comment118030539_66769294。请仅参考我使用Watson模型的Python代码部分。 - user12384956

-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))

这真的更快吗? - Redgar Tech

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接