原文: https://pythonspot.com/personal-assistant-jarvis-in-python/
我认为在 Python 中创建个人助理很酷。 如果您喜欢电影,可能已经听说过 A.I. Jarvis。 钢铁侠电影中的角色 在本教程中,我们将创建 机器人。
我要拥有的功能是:
- 识别语音(语音识别)
- 口头回答(文本到语音)
- 回答简单的命令
对于本教程,您将需要(Ubuntu)Linux,Python 和可正常使用的麦克风。
视频
这是您要创建的(观看整个视频,最后是演示):
https://www.youtube-nocookie.com/embed/ErGAhUa_rlA?rel=0
识别语音
可以使用 Python 语音识别模块完成语音识别。我们使用 Google Speech API,因为它的质量很高。
以语音回答(文字转语音)
各种 API 和程序可用于文本到语音的应用程序。Espeak 和 pyttsx 开箱即用,但听起来很机器人。我们决定使用 Google 文字到语音 API gTTS。
sudo pip install gTTS
使用它很简单:
from gtts import gTTSimport ostts = gTTS(text='Hello World', lang='en')tts.save("hello.mp3")os.system("mpg321 hello.mp3")

完整程序
下面的程序将回答口头问题。
#!/usr/bin/env python3# Requires PyAudio and PySpeech.import speech_recognition as srfrom time import ctimeimport timeimport osfrom gtts import gTTSdef speak(audioString):print(audioString)tts = gTTS(text=audioString, lang='en')tts.save("audio.mp3")os.system("mpg321 audio.mp3")def recordAudio():# Record Audior = sr.Recognizer()with sr.Microphone() as source:print("Say something!")audio = r.listen(source)# Speech recognition using Google Speech Recognitiondata = ""try:# Uses the default API key# To use another API key: `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`data = r.recognize_google(audio)print("You said: " + data)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))return datadef jarvis(data):if "how are you" in data:speak("I am fine")if "what time is it" in data:speak(ctime())if "where is" in data:data = data.split(" ")location = data[2]speak("Hold on Frank, I will show you where " + location + " is.")os.system("chromium-browser https://www.google.nl/maps/place/" + location + "/&")# initializationtime.sleep(2)speak("Hi Frank, what can I do for you?")while 1:data = recordAudio()jarvis(data)
