您的回答可能为时已晚。但是,将来可能会有其他人在寻找这个。
我还没有找到使用 Speech_recognition 自动检测语言的方法。 Google API 确实以备用语言数组的形式支持多种语言,它会尝试使用您指定的不同语言来提供翻译。
我克服检测语言问题的方法是我只有两种语言。我可以有更多我只有两个我需要的。我使用唤醒词和命令。所以我可以说(例如)“ok computer translate from Spanish”并且命令解析器确定意图是从西班牙语翻译。我为此使用了 snips,但您可以只进行字符串标记化。无论如何,那时,因为我知道意图是“来自西班牙语”,所以我明确地将语言代码设置为“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
这可能不是最优雅的解决方案。以后我可能会重写很多次。但是,它可以很好地满足我目前的需求。
我希望这有助于回答您的问题。