【深度讲解】手把手教你python制作萝莉音智能对话语音机器人,附全部源码!速速学起来!!_python菜鸟-CSDN博客
【深度讲解】手把手教你python制作萝莉音智能对话语音机器人,附全部源码!速速学起来!!
别着急,先看演示
记得三连加关,我太惨了,都没多少人关注我,呜呜!(水印名就是我b站用户名)
个人b站暂时主要讲了qq机器人制作,感兴趣点开看看:川川菜鸟b站主页
前言一定要看,很重要!!!
为了让大家真正学会,我用分模块步骤的方式讲解,这样也能让大家不仅在娱乐的同时,还能学到知识。东西有点多,你大可不必着急复制粘贴,你只需要看看我的讲解即可,当然,如果你能按照我的步骤亲自执行每一部分代码,那样你会更加学到知识,最下面可以直接下载完整的源码文件!!!别说你搞不出来了!!
第一步实现普通智能对话
代码如下:
# coding=gbk
"""
作者:川川
时间:2021/8/21
"""
import requests
print('请输入你想说的:')
while True:
a=input()
url='https://api.ownthink.com/bot?appid=9ffcb5785ad9617bf4e64178ac64f7b1&spoken=%s'%a
te=requests.get(url).json()
data=te['data']['info']['text']
print(data)
对话效果(还是比较人工智能)
第二步文字转萝莉音
1-到百度ai开放平台,链接为:https://ai.baidu.com/
,点击控制台,扫码登录进去 2-申请百度语音技术api,步骤如下: 然后配置如下: 点击创建即可。 然后到管理用用去查看:(我圈出来的后面要用) 开始撸代码:
# coding=gbk
"""
作者:川川
时间:2021/8/21
"""
# pip install baidu-aip
from aip import AipSpeech
""" 你的 APPID AK SK """
APP_ID = '上面说的那个APP_ID复制粘贴到这里'
API_KEY = '上面说的那个API_KEY复制粘贴到这里'
SECRET_KEY = '上面说的那个SECRET_KEY复制粘贴到这里'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
result = client.synthesis('你好,川川帅哥', 'zh', 1, {
'vol': 5, # 音量
'spd': 3, # 语速
'pit': 9, # 语调
'per': 4, # 0:女 1:男 3:逍遥 4:小萝莉
})
# 识别正确返回语音二进制 错误则返回dict 参照下面错误码
if not isinstance(result, dict):
with open('auido.mp3', 'wb') as f:
f.write(result)
运行后,不出意外,你会看到生成了一个mp3音频,你也可以手动点开播放以下看看。
第三步播放音频
运行就会播放该音频,这个演示可以看完整视频,但是该播放方式只能播放一次,还不能循环播放,当然这是基础部分。
# coding=gbk
"""
作者:川川
时间:2021/8/21
"""
from playsound import playsound
playsound('auido.mp3')
为了解决这个循环问题,我在网上找到了另外的办法来补救,我再创建一个play.py文件内容如下:
from platform import system
from abc import ABC, abstractmethod
operating_system = system()
if operating_system == 'Windows':
from ctypes import c_buffer, windll
from random import random
from time import sleep
from sys import getfilesystemencoding
elif operating_system == 'Darwin':
from AppKit import NSSound
from Foundation import NSURL
from time import sleep
elif operating_system == 'Linux':
# pathname2url escapes non-URL-safe characters
import os
try:
from urllib.request import pathname2url
except ImportError:
# python 2
from urllib import pathname2url
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst
class PlaysoundException(Exception):
pass
class playsoundBase(ABC):
def __init__(self):
pass
@abstractmethod
def play(self, sound, block):
raise NotImplemented
@abstractmethod
def stop(self):
raise NotImplemented
class playsoundWin(playsoundBase):
alias = ''
def winCommand(self, *command):
buf = c_buffer(255)
command = ' '.join(command).encode(getfilesystemencoding())
errorCode = int(windll.winmm.mciSendStringA(command, buf, 254, 0))
if errorCode:
errorBuffer = c_buffer(255)
windll.winmm.mciGetErrorStringA(errorCode, errorBuffer, 254)
exceptionMessage = (
'\n Error ' + str(errorCode) + ' for command:\n'
+ command.decode() + '\n ' + errorBuffer.value.decode())
raise PlaysoundException(exceptionMessage)
return buf.value
def play(self, sound, block=True):
self.alias = 'playsound_' + str(random())
self.winCommand('open "' + sound + '" alias', self.alias)
self.winCommand('set', self.alias, 'time format milliseconds')
durationInMS = self.winCommand('status', self.alias, 'length')
self.winCommand('play', self.alias, 'from 0 to', durationInMS.decode())
if block:
sleep(float(durationInMS) / 1000.0)
def stop(self):
self.winCommand('stop', self.alias)
def close(self):
self.winCommand('close', self.alias)
class playsoundOSX(playsoundBase):
def play(self, sound, block=True):
if '://' not in sound:
if not sound.startswith('/'):
from os import getcwd
sound = getcwd() + '/' + sound
sound = 'file://' + sound
url = NSURL.URLWithString_(sound)
nssound = NSSound.alloc().initWithContentsOfURL_byReference_(url, True)
if not nssound:
raise IOError('Unable to load sound named: ' + sound)
nssound.play()
if block:
sleep(nssound.duration())
def stop(self):
raise NotImplemented
class playsoundNix(playsoundBase):
def play(self, sound, block=True):
if not block:
raise NotImplementedError(
"block=False cannot be used on this platform yet")
Gst.init(None)
playbin = Gst.ElementFactory.make('playbin', 'playbin')
if sound.startswith(('http://', 'https://')):
playbin.props.uri = sound
else:
playbin.props.uri = 'file://' + pathname2url(
os.path.abspath(sound))
set_result = playbin.set_state(Gst.State.PLAYING)
if set_result != Gst.StateChangeReturn.ASYNC:
raise PlaysoundException(
"playbin.set_state returned " + repr(set_result))
# FIXME: use some other bus method than poll() with block=False
# https://lazka.github.io/pgi-docs/#Gst-1.0/classes/Bus.html
bus = playbin.get_bus()
bus.poll(Gst.MessageType.EOS, Gst.CLOCK_TIME_NONE)
playbin.set_state(Gst.State.NULL)
def stop(self):
raise NotImplemented
operating_system = 'Windows'
if operating_system == 'Windows':
playsound = playsoundWin
elif operating_system == 'Darwin':
playsound = playsoundOSX
elif operating_system == 'Linux':
playsound = playsoundNix
del operating_system
第四步综合上述代码
# coding=gbk
"""
作者:川川
时间:2021/8/22
"""
from play import playsound
from aip import AipSpeech
import requests
""" 你的 APPID AK SK """
APP_ID = '上面讲过的id'
API_KEY = '上面讲过API_KEY'
SECRET_KEY = '上面讲过SECRET_KEY'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
print('请输入你想说的:')
while True:
a=input()
url='https://api.ownthink.com/bot?appid=9ffcb5785ad9617bf4e64178ac64f7b1&spoken=%s'%a
te=requests.get(url).json()
data=te['data']['info']['text']
print(data)
result = client.synthesis(data, 'zh', 1, {
'vol': 8, # 音量
'spd': 5, # 语速
'pit': 9, # 语调
'per': 4, # 0:女 1:男 3:逍遥 4:小萝莉
})
# 识别正确返回语音二进制 错误则返回dict 参照下面错误码
if not isinstance(result, dict):
with open('auido.mp3', 'wb+') as f:
f.write(result)
p = playsound()
voice_path = r"auido.mp3"
p.play(voice_path) # 播放
p.close() # 停止
完整代码下载地址
上述步骤仅仅是讲解,如果小白不太懂,可以只需在百度ai平台申请后,将你的id和key在我的代码中换上就可以成功运行。
https://github.com/89461561511656/bot
<p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><strong style="margin: 0px; padding: 0px; word-break: break-all; overflow-wrap: break-word; overflow: hidden;"><span class="md-line md-end-block" style="margin: 0px; padding: 0px; box-sizing: border-box; display: block; font-size: 20px; word-break: break-all; overflow-wrap: break-word; overflow: hidden;">一、项目简介</span><span class="md-line md-end-block" style="margin: 0px; padding: 0px; word-break: break-all; overflow-wrap: break-word; overflow: hidden; box-sizing: border-box; display: block; font-size: 20px; color: #ff0000;">本课程演示的<span style="margin: 0px; padding: 0px; word-break: break-all; overflow-wrap: break-word; overflow: hidden;">是一套</span><span style="margin: 0px; padding: 0px; word-break: break-all; overflow-wrap: break-word; overflow: hidden; color: #ffff00; background-color: #ff0000;"><strong style="margin: 0px; padding: 0px; font-size: 16px; word-break: break-all; overflow-wrap: break-word; overflow: hidden;"><span class="md-line md-end-block" style="margin: 0px; padding: 0px; box-sizing: border-box; font-size: 20px; word-break: break-all; overflow-wrap: break-word; overflow: hidden;">基于JavaWeb实现的
学生信息管理系统</span></strong></span><strong style="margin: 0px; padding: 0px; font-size: 16px; word-break: break-all; overflow-wrap: break-word; overflow: hidden;"><span class="md-line md-end-block" style="margin: 0px; padding: 0px; word-break: break-all; overflow-wrap: break-word; overflow: hidden; box-sizing: border-box; font-size: 20px; display: inline !important;"><span style="margin: 0px; padding: 0px; word-break: break-all; overflow-wrap: break-word; overflow: hidden; color: #333333;">,</span>主要针对计算机相关专业的正在做毕设的
学生与需要项目实战练习的Java
学习者。</span></strong></span><span class="md-line md-end-block" style="margin: 0px; padding: 0px; word-break: break-all; overflow-wrap: break-word; overflow: hidden; box-sizing: border-box; display: block; font-size: 20px; color: #ff0000;"><strong style="margin: 0px; padding: 0px; font-size: 16px; word-break: break-all; overflow-wrap: break-word; overflow: hidden;"><span class="md-line md-end-block" style="margin: 0px; padding: 0px; word-break: break-all; overflow-wrap: break-word; overflow: hidden; box-sizing: border-box; font-size: 20px; display: inline !important;"><br style="margin: 0px; padding: 0px;" /></span></strong><span style="margin: 0px; padding: 0px; word-break: break-all; overflow-wrap: break-word; overflow: hidden; font-size: 18px;"><strong style="margin: 0px; padding: 0px; font-size: 16px; word-break: break-all; overflow-wrap: break-word; overflow: hidden;"><span class="md-line md-end-block" style="margin: 0px; padding: 0px; word-break: break-all; overflow-wrap: break-word; overflow: hidden; box-sizing: border-box; display: inline !important;"><strong style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; box-sizing: border-box; font-size: 18px;"><strong style="margin: 0px; padding: 0px; box-sizing: border-box;">课程包含</strong></span></strong></span></strong><strong style="margin: 0px; padding: 0px; font-size: 16px; word-break: break-all; overflow-wrap: break-word; overflow: hidden;"><span class="md-line md-end-block" style="margin: 0px; padding: 0px; word-break: break-all; overflow-wrap: break-word; overflow: hidden; box-sizing: border-box; display: inline !important;">:</span></strong></span></span></strong></p>
<p class="md-end-block md-p md-focus" style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0.8em 0px; padding: 0px; font-size: 16px; background-color: #ffffff; box-sizing: border-box; line-height: inherit; orphans: 4; white-space: pre-wrap; position: relative; color: #333333;"><span style="margin: 0px; padding: 0px; color: #ff0000;"><strong style="margin: 0px; padding: 0px; word-break: break-all; overflow-wrap: break-word; overflow: hidden;"><span style="margin: 0px; padding: 0px; box-sizing: border-box; font-size: 18px;">1. <span style="margin: 0px; padding: 0px; box-sizing: border-box; background-color: #ff0000; color: #ffff00;">项目
源码、项目文档、数据库脚本、软件工具</span>等所有资料</span></strong></span></p>
<p class="md-end-block md-p" style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0.8em 0px; padding: 0px; font-size: 16px; background-color: #ffffff; box-sizing: border-box; line-height: inherit; orphans: 4; white-space: pre-wrap; position: relative; color: #333333;"><span style="margin: 0px; padding: 0px; color: #ff0000;"><strong style="margin: 0px; padding: 0px; word-break: break-all; overflow-wrap: break-word; overflow: hidden;"><span style="margin: 0px; padding: 0px; box-sizing: border-box; font-size: 18px;">2. 带你从零开始部署运行本套系统</span></strong></span></p>
<p class="md-end-block md-p" style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0.8em 0px; padding: 0px; font-size: 16px; background-color: #ffffff; box-sizing: border-box; line-height: inherit; orphans: 4; white-space: pre-wrap; position: relative; color: #333333;"><span style="margin: 0px; padding: 0px; color: #ff0000;"><strong style="margin: 0px; padding: 0px; word-break: break-all; overflow-wrap: break-word; overflow: hidden;"><span style="margin: 0px; padding: 0px; box-sizing: border-box; font-size: 18px;">3. 该项目
附带的
源码资料可作为毕设使用</span></strong></span></p>
<p class="md-end-block md-p" style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0.8em 0px; padding: 0px; font-size: 16px; background-color: #ffffff; box-sizing: border-box; line-height: inherit; orphans: 4; white-space: pre-wrap; position: relative; color: #333333;"><span style="margin: 0px; padding: 0px; color: #ff0000;"><strong style="margin: 0px; padding: 0px; word-break: break-all; overflow-wrap: break-word; overflow: hidden;"><span style="margin: 0px; padding: 0px; box-sizing: border-box; font-size: 18px;">4. 提供技术答疑</span></strong></span></p>
<p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><strong style="margin: 0px; padding: 0px; word-break: break-all; overflow-wrap: break-word; overflow: hidden;"><span style="margin: 0px; padding: 0px; box-sizing: border-box; display: block; font-size: 20px; word-break: break-all; overflow-wrap: break-word; overflow: hidden;"><br style="margin: 0px; padding: 0px;" />二、技术实现</span></strong></p>
<p class="md-end-block md-p md-focus" style="box-sizing: border-box; line-height: inherit; orphans: 4; margin: 0.8em 0px; white-space: pre-wrap; position: relative; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 18px;"><span class="md-plain" style="box-sizing: border-box;">后台框架:Servlet、JSP、JDBC、DbUtils</span></p>
<p class="md-end-block md-p" style="box-sizing: border-box; line-height: inherit; orphans: 4; margin: 0.8em 0px; white-space: pre-wrap; position: relative; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 18px;"><span class="md-plain" style="box-sizing: border-box;">UI界面:EasyUI、jQuery、Ajax</span></p>
<p class="md-end-block md-p" style="box-sizing: border-box; line-height: inherit; orphans: 4; margin: 0.8em 0px; white-space: pre-wrap; position: relative; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 18px;"><span class="md-plain" style="box-sizing: border-box;">数据库:MySQL</span></p>
<p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><strong style="margin: 0px; padding: 0px; word-break: break-all; overflow-wrap: break-word; overflow: hidden;"><span style="margin: 0px; padding: 0px; word-break: break-all; overflow-wrap: break-word; overflow: hidden; color: #494429; font-size: 18px;"> </span></strong></p>
<p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 20px;"><strong style="margin: 0px; padding: 0px; word-break: break-all; overflow-wrap: break-word; overflow: hidden;">三、系统功能</strong></span></p>
<p class="md-end-block md-p" style="box-sizing: border-box; line-height: inherit; orphans: 4; margin: 0.8em 0px; position: relative;"><span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="font-size: 18px; white-space: pre-wrap;">该系统共包含三种角色:
学生、
教师和管理员,功能分别如下:</span></span></p>
<p class="md-end-block md-p" style="box-sizing: border-box; line-height: inherit; orphans: 4; margin: 0.8em 0px; position: relative;"><span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="font-size: 18px; white-space: pre-wrap;">1.
学生模块 个人信息管理、同
学通讯录、成绩查询、修改密码</span></span></p>
<p class="md-end-block md-p" style="box-sizing: border-box; line-height: inherit; orphans: 4; margin: 0.8em 0px; position: relative;"><span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="font-size: 18px; white-space: pre-wrap;">2.
教师模块 个人信息管理、
教师通讯录、成绩登记、成绩统计、成绩导出、修改密码</span></span></p>
<p class="md-end-block md-p" style="box-sizing: border-box; line-height: inherit; orphans: 4; margin: 0.8em 0px; position: relative;"><span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="font-size: 18px; white-space: pre-wrap;">3. 系统管理员 基础信息管理、
学生信息管理、
教师信息管理、考试管理、后台管理</span></span></p>
<p class="md-end-block md-p md-focus" style="box-sizing: border-box; line-height: inherit; orphans: 4; margin: 0.8em 0px; white-space: pre-wrap; position: relative; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 18px;"><span class="md-plain" style="box-sizing: border-box;">该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。</span></p>
<p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"> </p>
<p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><strong style="margin: 0px; padding: 0px; font-size: 20px; word-break: break-all; overflow-wrap: break-word; overflow: hidden;">四、项目截图</strong></p>
<p><strong><span style="font-size: 18px;">1)系统登陆界面</span></strong></p>
<p><strong><span style="font-size: 18px;"><img src="https://img-bss.csdnimg.cn/202108010757348673.jpg" alt="" /><br /></span></strong></p>
<p><strong><span style="font-size: 18px;"><strong><span style="font-size: 18px;">2)
学生模块</span></strong></span></strong></p>
<p><strong><span style="font-size: 18px;"><img src="https://img-bss.csdn.net/202002241015575966.png" alt="" /></span></strong></p>
<p><strong><span style="font-size: 18px;"><strong><span style="font-size: 18px;">3)
教师模块</span></strong></span></strong></p>
<p><strong><span style="font-size: 18px;"><img src="https://img-bss.csdn.net/202002241016127898.png" alt="" /></span></strong></p>
<p><strong><span style="font-size: 18px;"><strong><span style="font-size: 18px;">4)系统管理员</span></strong></span></strong></p>
<p><strong><span style="font-size: 18px;"><img src="https://img-bss.csdn.net/202002241016281177.png" alt="" /></span></strong></p>
<p><strong><span style="font-size: 18px;"><img src="https://img-bss.csdn.net/202002241016369884.png" alt="" /></span></strong></p>
<p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px;"> </p>
<p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px;"> </p>
<p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #ff0000;"><strong style="margin: 0px; padding: 0px; word-break: break-all; overflow-wrap: break-word; overflow: hidden;"><span style="margin: 0px; padding: 0px; word-break: break-all; overflow-wrap: break-word; overflow: hidden; font-size: 18px;">更多Java毕设项目请关注
【毕设系列课程
】</span></strong></span><a style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; box-sizing: border-box; color: #007bff; outline: none; cursor: pointer; margin: 0px; padding: 0px; font-size: 16px; text-decoration-line: none !important;" href="https://edu.csdn.net/lecturer/2104">https://edu.csdn.net/lecturer/2104</a></p>
<p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px;"> </p>
<p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px;"> </p>
<p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px;"><img src="https://img-bss.csdnimg.cn/202102270503111075.jpg" alt="" /></p>
<p> </p>