# Python 中的个人助理（Jarvis）

> 原文： [https://pythonspot.com/personal-assistant-jarvis-in-python/](https://pythonspot.com/personal-assistant-jarvis-in-python/)

我认为在 [**Python**](https://pythonspot.com)中创建**个人助理**很酷。 如果您喜欢电影，可能已经听说过 A.I. Jarvis。 钢铁侠电影中的角色 在本教程中，我们将创建 [**机器人**。](https://pythonspot.com/robotics)

我要拥有的功能是：

*   [**识别语音（语音识别）**](https://pythonspot.com/speech-recognition-using-google-speech-api/)
*   [**口头回答（文本到语音）**](https://pythonspot.com/speech-engines-with-python-tutorial/)
*   回答简单的命令

For this tutorial you will need (Ubuntu) Linux, [**Python**](https://pythonspot.com) and a working microphone.

#### 视频

This is what you’ll create (watch the whole video, demo at the end):

&lt;iframe allow="autoplay; encrypted-media" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube-nocookie.com/embed/ErGAhUa_rlA?rel=0" width="560"&gt;&lt;/iframe&gt;

### 识别语音

Speech recognition can by done using the Python SpeechRecognition module. We make use of the [Google Speech API](https://pythonspot.com/speech-recognition-using-google-speech-api/ "Google Speech Recognition in Python") because of it’s great quality.

### 以语音回答（文字转语音）

Various [**APIs and programs are available for text to speech applications**](https://pythonspot.com/speech-engines-with-python-tutorial/ "TTS"). Espeak and pyttsx work out of the box but sound very robotic. We decided to go with the Google Text To Speech API, gTTS.

```py

sudo pip install gTTS

```

使用它很简单：

```py

from gtts import gTTS
import os
tts = gTTS(text='Hello World', lang='en')
tts.save("hello.mp3")
os.system("mpg321 hello.mp3")

```

[&lt;picture&gt;&lt;source srcset="/wp-content/uploads/2015/07/gtts.png.webp" type="image/webp"&gt; &lt;source srcset="/wp-content/uploads/2015/07/gtts.png" type="image/jpeg"&gt; ![gtts](img/538e4461fb4b4cc380ce7b29759028f8.jpg)&lt;/picture&gt; ](/wp-content/uploads/2015/07/gtts.png)

### 完成程序

The program below will answer spoken questions.

```py

#!/usr/bin/env python3
# Requires PyAudio and PySpeech.

import speech_recognition as sr
from time import ctime
import time
import os
from gtts import gTTS

def speak(audioString):
    print(audioString)
    tts = gTTS(text=audioString, lang='en')
    tts.save("audio.mp3")
    os.system("mpg321 audio.mp3")

def recordAudio():
    # Record Audio
    r = sr.Recognizer()
    with sr.Microphone() as source:
    print("Say something!")
    audio = r.listen(source)

    # Speech recognition using Google Speech Recognition
    data = ""
    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 data

def 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 + "/&amp;")

# initialization
time.sleep(2)
speak("Hi Frank, what can I do for you?")
while 1:
    data = recordAudio()
    jarvis(data)

```

### 相关文章：

*   [**机器人技术，计算机视觉和超赞的内容**](https://pythonspot.com/robotics)

*   [**语音引擎（TTS）**](https://pythonspot.com/speech-engines-with-python-tutorial/)

*   [**语音识别**](https://pythonspot.com/speech-recognition-using-google-speech-api/)