114.md 4.0 KB
Newer Older
W
init  
wizardforcel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
# 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):

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

### 识别语音

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

```

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

W
wizardforcel 已提交
49
### 完整程序
W
init  
wizardforcel 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

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

# 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/)