首页 > 代码库 > 树莓派 百度语音 智能家居小助手
树莓派 百度语音 智能家居小助手
大体思路(程序写的有点乱,只是大体框架,还有好多坑留着慢慢填)
树莓派3通过USB声卡录音 ==>> 录音传给百度语音识别 ==>> 返回txt文本 ==>>
IF TXT=操作智能家居的语句 THEN 执行相应动作
ELSE 将txt文本提交tuling123 ==>> 返回txt ==>> 传给百度语音文字转语音==>> 树莓派播放
import uuid import base64 import json import urllib from urllib import request import urllib.parse import sys import wave from pyaudio import PyAudio,paInt16 from pypinyin import pinyin, lazy_pinyin import pypinyin import os channels = 1 framerate = 8000 NUM_SAMPLES = 2000 sampwidth = 2 TIME=2 apiKey = "SSWhZvvboPy5mbSy8ahOSFjP" secretKey = "c9b0195c91d435ca908ffc07dedbc4bd" asr_server = ‘http://vop.baidu.com/server_api‘ auth_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=SSWhZvvboPy5mbSy8ahOSFjP&client_secret=c9b0195c91d435ca908ffc07dedbc4bd" #将数据保存成wav格式文件 def save_wave_file(filename,data): wf = wave.open(filename,‘wb‘) wf.setnchannels(channels) wf.setsampwidth(sampwidth) wf.setframerate(framerate) wf.writeframes(b"".join(data)) wf.close() def get_token(): res = urllib.request.urlopen(auth_url) json_data = res.read().decode(‘utf-8‘) a = json.loads(json_data) token = a[‘access_token‘] return token def baidu_record(): os.system(‘arecord -D "plughw:1,0" -f S16_LE -d 3 -r 8000 ~/MyProject/temp.wav‘) print(‘done..‘) #将wav编码成base64格式,上传给百度语音识别,并取得识别的result, token = get_token() mac_address=uuid.UUID(int=uuid.getnode()).hex[-12:] #把wav文件以base64编码,再将需要上传的数据按照百度要求以json格式提交 speech_file = ‘temp.wav‘ with open(speech_file, ‘rb‘) as f: speech_data = f.read() speech_base64=base64.b64encode(speech_data).decode(‘utf-8‘) speech_length=len(speech_data) data_dict = {‘format‘:‘wav‘, ‘rate‘:8000, ‘channel‘:1, ‘cuid‘:mac_address, ‘token‘:token, ‘lan‘:‘zh‘, ‘speech‘:speech_base64, ‘len‘:speech_length} json_data = json.dumps(data_dict).encode(‘utf-8‘) json_length = len(json_data) request = urllib.request.Request(url=asr_server) request.add_header("Content-Type", "application/json") request.add_header("Content-Length", json_length) fs = urllib.request.urlopen(url=request, data=http://www.mamicode.com/json_data)"http://tsn.baidu.com/text2audio?tex="+ ‘主人,请吩咐‘+"&lan=zh&per=0&pit=1&spd=7&cuid=8466761&ctp=1&tok="+get_token() os.system(‘mplayer "%s"‘%(url)) cmd = baidu_record() a=lazy_pinyin(cmd) data = a[0:-1] print(data) if data=http://www.mamicode.com/= [‘da‘, ‘kai‘, ‘ke‘, ‘ting‘, ‘chuang‘, ‘lian‘]:"http://tsn.baidu.com/text2audio?tex="+‘客厅窗帘已打开‘+"&lan=zh&per=0&pit=1&spd=7&cuid=8466761&ctp=1&tok="+get_token() os.system(‘mplayer "%s"‘%(url)) continue if data=http://www.mamicode.com/= [‘da‘, ‘kai‘, ‘chu‘, ‘fang‘, ‘deng‘]:"http://tsn.baidu.com/text2audio?tex="+‘厨房灯已打开‘+"&lan=zh&per=0&pit=1&spd=7&cuid=8466761&ctp=1&tok="+get_token() os.system(‘mplayer "%s"‘%(url)) continue ### 开始图灵对话 #### key = ‘60db5011ff204107bf777a6ad1dead11‘ #key&api api = ‘http://www.tuling123.com/openapi/api?key=‘ + key + ‘&info=‘ tuling_request = api + urllib.parse.quote(cmd) response = urllib.request.urlopen(tuling_request).read().decode(‘utf-8‘) dic_json = json.loads(response) #百度text转语音 url = "http://tsn.baidu.com/text2audio?tex="+dic_json[‘text‘]+"&lan=zh&per=0&pit=1&spd=7&cuid=8466761&ctp=1&tok="+get_token() os.system(‘mplayer "%s"‘%(url)) #将中文转成拼音 #a=lazy_pinyin(cmd) #data = a[0:-1] #print(data) #拼音模糊匹配 # if opt == ‘dakai‘ or ‘duokan‘ or ‘dakan‘ or ‘daka‘: opt=‘dakai‘ # if opt == ‘guanbi‘ or ‘guabi‘ or ‘guanbin‘ or ‘daka‘: opt=‘guanbi‘ # if loc == ‘keting‘ or ‘ketin‘ or ‘kaitin‘ or ‘kantin‘: loc=‘keting‘ # if loc == ‘woshi‘ or ‘washi‘ or ‘wosi‘ or ‘wasi‘: loc=‘woshi‘ # if obj == ‘deng‘ or ‘dong‘ or ‘den‘ or ‘duo‘:obj=‘deng‘ ############################ # opt_list_open=[‘dakai‘,‘dakan‘,‘daka‘,‘daikai‘,‘daikan‘,‘daika‘,‘danka‘,‘dankai‘] # opt_list_close=[‘guanbi‘,‘guanshang‘,‘guanshan‘,‘guangbi‘] # loc_list_keting=[‘keting‘,‘kanting‘,‘kaiting‘] # obj_list_deng=[‘deng‘,‘den‘,‘dong‘] # if opt in opt_list_open: opt=‘dakai‘ # if opt in opt_list_close: opt=‘guanbi‘ # if loc in loc_list_keting: loc = ‘keting‘ # if obj in obj_list_deng: obj = ‘deng‘ # print(‘=== ====‘,opt+loc+obj) ##### #判断位置,并用http方式提交给各个节点的nodemcu无线模块数据,最后由nodemcu操作继电器或者红外遥控 # if loc==‘keting‘: # url = "http://192.168.1.106:8080/?"+opt+loc+obj # urllib.request.urlopen(url) # if loc==‘woshi‘: # url = "http://192.168.1.106:8080/?"+opt+loc+obj # urllib.request.urlopen(url)
本文出自 “七月” 博客,请务必保留此出处http://374721.blog.51cto.com/364721/1876878
树莓派 百度语音 智能家居小助手
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。