技术基础参考:https://www.jb51.net/article/168328.htm
技术分析:1.优点:实现简单,利用各种API快速实现语音助手。2.缺点,由于图灵机器人需要付费和实名认证造成了一些麻烦,所以改进了一下这个图灵机器人语音助手编写了小思语音助手。
小思机器人:https://www.ownthink.com/docs/bot/
百度语音识别:http://ai.baidu.com/docs#/ASR-Online-Python-SDK/top](http://ai.baidu.com/docs#/ASR-Online-Python-SDK/top
# -*- coding: utf-8 -*-#
# -------------------------------
# Name:SpeechRobot
# Author:Nieson
# Date:2019/7/19 16:31
#Date:2019/11/15改进
# 用python3实现自己的语音对话机器人
# -------------------------------
from aipimport AipSpeech
import requests
import json
import speech_recognitionas sr
import win32com.client
# 初始化语音
speaker = win32com.client.Dispatch("SAPI.SpVoice")
# 1、语音生成音频文件,录音并以当前时间戳保存到voices文件中
# Use SpeechRecognition to record 使用语音识别录制
def my_record(rate=16000):
r = sr.Recognizer()
with sr.Microphone(sample_rate=rate)as source:
print("请说话……………………")
audio = r.listen(source)
with open("myvoices.wav", "wb")as f:
f.write(audio.get_wav_data())
# 2、音频文件转文字:采用百度的语音识别python-SDK
# 导入我们需要的模块名,然后将音频文件发送给出去,返回文字。
# 百度语音识别API配置参数
APP_ID ='17761131'
API_KEY ='SYUAKbKfYAA7ytHgq25pgDfy'
SECRET_KEY ='aQCSHyZTI3BW93dGaVQGrm8kdP1Mfevt'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
path ='myvoices.wav'
# 将语音转文本STT
def listen():
# 读取录音文件
with open(path, 'rb')as fp:
voices = fp.read()
try:
# 参数dev_pid:1536普通话(支持简单的英文识别)、1537普通话(纯中文识别)、1737英语、1637粤语、1837四川话、1936普通话远场
result = client.asr(voices, 'wav', 16000, {'dev_pid':1537, })
# result = CLIENT.asr(get_file_content(path), 'wav', 16000, {'lan': 'zh', })
# print(result)
# print(result['result'][0])
# print(result)
result_text = result["result"][0]
print("蔗糖王金飞: " + result_text)
#print(result_text)
return result_text
except KeyError:
print("###重新输入###")
speaker.Speak("嗨金飞,你在吗...")
# 3、与机器人对话:调用的是小思
# 小思机器人回复
def Turing(text_words=""):
result_text = listen()
sess = requests.get('https://api.ownthink.com/bot?spoken={result_text2}'.format(result_text2= result_text))
answer = sess.text
answer = json.loads(answer)
data = answer['data']['info']['text']
print("小思: " + data)
data2 ="嗨金飞"
return data2+data
###################
#############
# 语音合成,输出机器人的回答
while True:
my_record()
result_text = listen()
response = Turing(result_text)
speaker.Speak(response)