|
|
|
|
import time
|
|
|
|
|
import urllib
|
|
|
|
|
import wave
|
|
|
|
|
import urllib.parse
|
|
|
|
|
import requests
|
|
|
|
|
from pyaudio import PyAudio, paInt16
|
|
|
|
|
from aip import AipSpeech
|
|
|
|
|
import pyttsx3
|
|
|
|
|
from AI聊天机器人 import login_window,chat_window
|
|
|
|
|
|
|
|
|
|
framerate = 16000 # 采样率
|
|
|
|
|
num_samples = 2000 # 采样点
|
|
|
|
|
channels = 1 # 声道
|
|
|
|
|
sampwidth = 2 # 采样宽度2bytes
|
|
|
|
|
FILEPATH = 'D:/Tool/test/pythonProject6/myvoices.wav' #该文件目录要存在
|
|
|
|
|
|
|
|
|
|
#用于接收用户的语音输入, 并生成wav音频文件(wav、pcm、mp3的区别可详情百度)
|
|
|
|
|
class Speak():
|
|
|
|
|
|
|
|
|
|
#将音频数据保存到wav文件之中
|
|
|
|
|
def save_wave_file(self, filepath, data):
|
|
|
|
|
wf = wave.open(filepath, 'wb')
|
|
|
|
|
wf.setnchannels(channels)
|
|
|
|
|
wf.setsampwidth(sampwidth)
|
|
|
|
|
wf.setframerate(framerate)
|
|
|
|
|
wf.writeframes(b''.join(data))
|
|
|
|
|
wf.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 进行语音录制工作
|
|
|
|
|
def my_record(self):
|
|
|
|
|
pa = PyAudio()
|
|
|
|
|
# 打开一个新的音频stream
|
|
|
|
|
stream = pa.open(format=paInt16, channels=channels,
|
|
|
|
|
rate=framerate, input=True, frames_per_buffer=num_samples)
|
|
|
|
|
my_buf = [] # 存放录音数据
|
|
|
|
|
|
|
|
|
|
t = time.time()
|
|
|
|
|
print('正在讲话...')
|
|
|
|
|
|
|
|
|
|
while time.time() < t + 5: # 设置录音时间(秒)
|
|
|
|
|
# 循环read,每次read 2000frames
|
|
|
|
|
string_audio_data = stream.read(num_samples)
|
|
|
|
|
my_buf.append(string_audio_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print('讲话结束')
|
|
|
|
|
self.save_wave_file(FILEPATH, my_buf) #保存下录音数据
|
|
|
|
|
stream.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
APP_ID = '73927317'
|
|
|
|
|
API_KEY = '3jGcj5fLma64CtB3tTEuLcei'
|
|
|
|
|
SECRET_KEY = 'qm8gPCF7DSKpqatx5ZQ8e4OvNLmgdYcG' # 此处填写自己的密钥
|
|
|
|
|
|
|
|
|
|
"""调用接口, 调用BaiDu AI 接口进行录音、语音识别"""
|
|
|
|
|
client = AipSpeech('73927317', '3jGcj5fLma64CtB3tTEuLcei', 'qm8gPCF7DSKpqatx5ZQ8e4OvNLmgdYcG')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RobotSay():
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
# 初始化语音
|
|
|
|
|
self.engine = pyttsx3.init() # 初始化语音库
|
|
|
|
|
|
|
|
|
|
# 设置语速
|
|
|
|
|
self.rate = self.engine.getProperty('rate')
|
|
|
|
|
self.engine.setProperty('rate', self.rate - 50)
|
|
|
|
|
|
|
|
|
|
def say(self, msg):
|
|
|
|
|
# 输出语音
|
|
|
|
|
self.engine.say(msg) # 合成语音
|
|
|
|
|
self.engine.runAndWait()
|
|
|
|
|
|
|
|
|
|
class ReadWav():
|
|
|
|
|
# 读取文件
|
|
|
|
|
def get_file_content(self, filePath):
|
|
|
|
|
with open(filePath, 'rb') as fp:
|
|
|
|
|
return fp.read()
|
|
|
|
|
|
|
|
|
|
def predict(self):
|
|
|
|
|
# 调用百度AI的接口, 识别本地文件
|
|
|
|
|
return client.asr(self.get_file_content('D:/Tool/test/pythonProject6/myvoices.wav'), 'wav', 16000, {
|
|
|
|
|
'dev_pid': 1537,
|
|
|
|
|
})
|
|
|
|
|
def talkWithRobot(msg):
|
|
|
|
|
url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg={}'.format(urllib.parse.quote(msg))
|
|
|
|
|
html = requests.get(url)
|
|
|
|
|
return html.json()["content"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
robotSay = RobotSay()
|
|
|
|
|
speak = Speak()
|
|
|
|
|
readTalk = ReadWav()
|
|
|
|
|
while True:
|
|
|
|
|
|
|
|
|
|
speak.my_record() #录音
|
|
|
|
|
|
|
|
|
|
text = readTalk.predict()['result'][0] #调用百度AI接口, 将录音转化为文本信息
|
|
|
|
|
|
|
|
|
|
print("本人说:", text) #输出文本信息
|
|
|
|
|
response_dialogue = talkWithRobot(text) #调用青云客机器人回答文本信息并返回
|
|
|
|
|
print("青云客说:", response_dialogue) #输出回答文本信息
|
|
|
|
|
|
|
|
|
|
robotSay.say(response_dialogue) #播放回答信息
|
|
|
|
|
|
|
|
|
|
readWav = ReadWav() # 实例化方法
|
|
|
|
|
print(readWav.predict())
|