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

问题描述 投票:0回答:2

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

我的代码:

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

希望你理解。

python-3.x api audio voice speech
2个回答
6
投票

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

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

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

 said = r.recognize_google(audio, language="es")

我的语音记录课程

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

监听循环 - 我从 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

这可能不是最优雅的解决方案。我将来可能会重写很多次。不过,它非常适合我目前的需要。

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


-1
投票

非常感谢@WinkDoubleguns,这还不算太晚。

这对我很有帮助。 ;)

© www.soinside.com 2019 - 2024. All rights reserved.