如何使用 Python 中的语音识别自动检测语言

2 api audio voice speech python-3.x

我正在开发一个应用程序,我想自动检测语言然后打印它。

我的代码:

with sr.Microphone() as source:
audio = r.listen(source)
try:
    # Auto detect the language
    print("You said: " + r.recognize_google(audio))
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")
Run Code Online (Sandbox Code Playgroud)

希望你明白。

Win*_*uns 5

这对您的回答可能为时已晚。然而,其他人可能会在未来寻找这个。

我还没有找到使用语音识别自动检测语言的方法。Google API 确实以备用语言数组的形式支持多种语言,它将尝试您指定的不同语言以提供翻译。

我克服检测语言问题的方法是我只有两种语言。我可以有更多我只有两个我需要的。我使用唤醒词和命令。所以我可以说(例如)“从西班牙语翻译好的计算机”,命令解析器确定意图是从西班牙语翻译。我为此使用了 snips,但您可以只进行字符串标记化。无论如何,在那一点上,因为我知道意图是“来自西班牙语”,所以我明确地将语言代码设置为“es”,如下所示:

 said = r.recognize_google(audio, language="es")
Run Code Online (Sandbox Code Playgroud)

我的 SpeechRecord 类

import speech_recognition as sr

class SpeechRec:
    #https://techwithtim.net/tutorials/voice-assistant/wake-keyword/
    def record(self, lang='en'):
        r = sr.Recognizer()
        with sr.Microphone() as source:
            audio = r.listen(source)
            said = ""

            try:
                #can I detect the language?
                if (lang == 'en') :
                    said = r.recognize_google(audio, language='en-US')
                elif (lang == 'es') :
                    said = r.recognize_google(audio, language="es") 

                print(said)
            except Exception as e:
                if (str(e) != ""):
                    print("Exception: " + str(e))

        return said.lower()
Run Code Online (Sandbox Code Playgroud)

监听循环 - 我从 Flask 事件中调用它,但它在独立应用程序中的工作方式相同

WAKE = "computer"
while True:
    text = SpeechRec().record()
    language = "en"

    if text.count(WAKE) > 0:
        text = SpeechRec().record()

        #here I have a call to determine the intent via Snips - I've removed that and just
        #placed a text comparison for simplicity.  Also, to note, using Snips I can reduce
        #the wake word and command to remove two of these 
        #"text = SpeechRec().record()" lines
        if (text == 'translate from spanish'):
            text = SpeechRec().record('es')
        else:
            text = SpeechRec().record()

        #at this point I do the translation and print the value from the translation
Run Code Online (Sandbox Code Playgroud)

这可能不是最优雅的解决方案。将来我可能会多次重写。但是,它可以很好地满足我目前的需求。

我希望这有助于回答您的问题。