网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Python实现人工语音对话的方法

本篇内容介绍了“Python实现人工语音对话的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

创新互联公司是一家专业提供象州企业网站建设,专注与成都做网站、网站设计、外贸营销网站建设HTML5建站、小程序制作等业务。10年已为象州众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。

gtts

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

具体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包

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

Python实现人工语音对话的方法

下面就是 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)

下面是我乱说的英语

Python实现人工语音对话的方法

对话

上面,我们实现了用麦克风记录下你的话,并且得到了对应的文本,那么下一步就是字符串的文本操作了,比如说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()

 Python实现人工语音对话的方法

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

Python实现人工语音对话的方法

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

Python实现人工语音对话的方法

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

Python实现人工语音对话的方法

本项目对应的Github

https://github.com/MaoliRUNsen/Simple-intelligent-voice-dialogue

“Python实现人工语音对话的方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


网站标题:Python实现人工语音对话的方法
当前地址:http://bjjierui.cn/article/jpecgh.html

其他资讯