1876年,我们发明了电话,我们可以通过电线传输音频,隔年我们又发明了留声机,成功地实现了声音的记录和播放。1952年,贝尔实验室实现了一个可以识别数字的语音识别软件,1985年,IBM实现了一个可以识别1000个单词的软件。在声音的识别上,我们一直在前进。在人声识别的应用上,人工语音对话,智能应答,语音输入等功能已经相当常见,这都得益于各种第三方包的实现。如今,在Python中Tensorflow,Keras,Librosa,Kaldi和语音转文本API等多种第三方库和api使语音识别和操作变得更加容易。今天小编带来的就是使用gtts和speech_recognition实现的一个简单的人工语音对话。思路就是将语音转文本,经过一定的逻辑操作后,输出对应文本,然后再将文本转语音输出。话不多说,来看操作吧!

gtts

gtts是将文字转化为语音,但是需要在VPN下使用。这个因为要接谷歌服务器。

具体gtts的官方文档

下面,让我们看一段简单的的代码

from gtts import gTTS

def speak(audioString):
    print(audioString)
    tts = gTTS(text=audioString, lang='en')
    tts.save("audio.mp3")
    os.system("audio.mp3")
    
speak("Hi Runsen, what can I do for you?")

执行上面的代码,就可以生成一个mp3文件,播放就可以听到了Hi Runsen, what can I do for you?。这个MP3会自动弹出来的。

speech_recognition

speech_recognition用于执行语音识别的库,支持在线和离线的多个引擎和API。

speech_recognition具体官方文档

安装speech_recognition可以会出现错误,对此解决的方法是通过该网址安装对应的whl包

在官方文档中提供了具体的识别来自麦克风的语音输入的代码

下面就是 speech_recognition 用麦克风记录下你的话,这里我使用的是
recognize_google,speech_recognition 提供了很多的类似的接口。

import time
import speech_recognition as sr

# 录下来你讲的话
def recordAudio():
    # 用麦克风记录下你的话
    print("开始麦克风记录下你的话")
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
    data = ""
    try:
        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

if __name__ == '__main__':
    time.sleep(2)
    while True:
        data = recordAudio()
        print(data)

下面是我乱说的英语

对话

上面,我们实现了用麦克风记录下你的话,并且得到了对应的文本,那么下一步就是字符串的文本操作了,比如说how are you,那回答"I am fine”,然后将"I am fine”通过gtts是将文字转化为语音

# @Author:Runsen
# -*- coding: UTF-8 -*-
import speech_recognition as sr
from time import ctime
import time
import os
from gtts import gTTS


# 讲出来AI的话
def speak(audioString):
    print(audioString)
    tts = gTTS(text=audioString, lang='en')
    tts.save("audio.mp3")
    os.system("audio.mp3")


# 录下来你讲的话
def recordAudio():
    # 用麦克风记录下你的话
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)

    data = ""
    try:
        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


# 自带的对话技能(逻辑代码:rules)
def jarvis():
    while True:
        data = recordAudio()
        print(data)
        if "how are you" in data:
            speak("I am fine")
        if "time" in data:
            speak(ctime())
        if "where is" in data:
            data = data.split(" ")
            location = data[2]
            speak("Hold on Runsen, I will show you where " + location + " is.")
            # 打开谷歌地址
            os.system("open -a Safari https://www.google.com/maps/place/" + location + "/&")

        if "bye" in data:
            speak("bye bye")
            break


if __name__ == '__main__':
    # 初始化
    time.sleep(2)
    speak("Hi Runsen, what can I do for you?")

    # 跑起
    jarvis()

当我说how are you?会弹出I am fine的mp3

当我说where is Chiana?会弹出Hold on Runsen, I will show you where China is.的MP3

同样也会弹出China的谷歌地图

本项目对应的Github

小结

以上就是30行Python代码打造一款简单的人工语音对话的详细介绍,更多Python学习内容和人工语音对话的资料请关注W3Cschool其它相关文章!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。