Python开发学习之Python用三十行代码,打造一款简单的人工语音对话
1876年,亚历山大·格雷厄姆·贝尔(Alexander Graham Bell)发明了一种电报机,可以通过电线传输音频。托马斯·爱迪生(Thomas Edison)于1877年发明了留声机,这是第一台记录声音并播放声音的机器。
最早的语音识别软件之一是由Bells Labs在1952年编写的,只能识别数字。1985年,IBM发布了使用“隐马尔可夫模型”的软件,该软件可识别1000多个单词。
几年前,一个replace(“?”,“”)代码价值一个亿
如今,在Python中Tensorflow,Keras,Librosa,Kaldi和语音转文本API等多种工具使语音计算变得更加容易。
今天,我使用gtts和speech_recognition,教大家如何通过三十行代码,打造一款简单的人工语音对话。思路就是将语音变成文本,然后文本变成语音。
gtts
gtts是将文字转化为语音,但是需要在虚拟网络下使用。这个因为要接谷歌服务器。
具体gtts的官方文档:
下面,让我们看一段简单的的代码
from gtts import gTTS
def speak(audioString):
print(audioString)
tts = gTTS(text=audioString,)
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,)
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的谷歌地图
文章来源:网络 版权归原作者所有
上文内容不用于商业目的,如涉及知识产权问题,请权利人联系小编,我们将立即处理
相关文章
- Python学习--21 电子邮件
- 机器学习笔记 - 使用python代码实现易于理解的反向传播
- Python语言学习:Python语言学习之正则表达式常用函数之re.search方法【输出仅一个匹配结果(内容+位置)】、re.findall方法【输出所有匹配结果(内容)】案例集合之详细攻略
- Python语言学习:Python语言学习之函数(定义&调用函数/常用内置函数如filter&map/内嵌函数/闭包/匿名函数如lambda&map)的简介、案例应用之详细攻略
- Python语言学习:python语言代码调试—异常处理之详细攻略
- Python语言学习:利用python获取当前/上级/上上级目录路径(获取路径下的最后叶目录的文件名、合并两个不同路径下图片文件名等目录/路径案例、正确加载图片路径)之详细攻略
- Python编程语言学习:python中与数字相关的函数(取整等)、案例应用之详细攻略
- Python语言学习:利用python语言实现调用内部命令(python调用Shell脚本)—命令提示符cmd的几种方法
- Python语言学习之数值、小数、空格那些事:python和数值、小数、空格的使用方法之详细攻略
- Python语言学习之文件格式后缀那些事:python和常见各种文件格式后缀介绍、使用方法之详细攻略
- Python可视化数据分析09、MySQL读写
- python:网络安全攻击与防御的工具(附零基础学习资料)
- 【python代码】:能在手机上敲 Python 代码几款App
- 【阶段四】Python深度学习02篇:深度学习基础知识:神经网络可调超参数:优化器
- 【阶段三】Python机器学习30篇:机器学习项目实战:智能推荐系统的基本原理与计算相似度的常用方法
- Python基于深度学习算法实现图书推荐系统项目实战
- 【机器学习】精确率(precision)、召回率(recall)以及F1分数的概念讲解加python代码实现
- Python学习70:collections
- C++调用C++项目中的Python脚本中的函数和类。,在,工程,python
- Python 学习之中的一个:在Mac OS X下基于Sublime Text搭建开发平台包括numpy,scipy
- python基础===一行 Python 代码实现并行(转)
- 学习笔记(05):Python爬虫开发-数据解析