# -*- coding: utf-8 -*-
#from pyaudio import PyAudio, paInt16
import numpy as np
from datetime import datetime
import wave
import time
import urllib, urllib2, pycurl
import base64
import json
import os
import sys
import RPi.GPIO as GPIO
import time
# BOARD編號(hào)方式,基于插座引腳編號(hào)
GPIO.setmode(GPIO.BOARD)
# 輸出模式
GPIO.setup(11, GPIO.OUT)
reload(sys)
sys.setdefaultencoding( "utf-8" )
save_count = 0
save_buffer = []
t = 0
sum = 0
time_flag = 0
flag_num = 0
filename = '2.wav'
duihua = '1'
def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html
def get_token():
apiKey = "Ll0c53MSac6GBOtpg22ZSGAU"
secretKey = "44c8af396038a24e34936227d4a19dc2"
auth_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + apiKey + "&client_secret=" + secretKey;
res = urllib2.urlopen(auth_url)
json_data = res.read()
return json.loads(json_data)['access_token']
def dump_res(buf):
global duihua
print "字符串類型"
print (buf)
a = eval(buf)
print type(a)
if a['err_msg']=='success.':
#print a['result'][0]#終于搞定了,在這里可以輸出,返回的語(yǔ)句
duihua = a['result'][0]
print duihua
def use_cloud(token):
fp = wave.open(filename, 'rb')
nf = fp.getnframes()
f_len = nf * 2
audio_data = fp.readframes(nf)
cuid = "7519663" #產(chǎn)品id
srv_url = 'http://vop.baidu.com/server_api' + '?cuid=' + cuid + '&token=' + token
http_header = [
'Content-Type: audio/pcm; rate=8000',
'Content-Length: %d' % f_len
]
c = pycurl.Curl()
c.setopt(pycurl.URL, str(srv_url)) #curl doesn't support unicode
#c.setopt(c.RETURNTRANSFER, 1)
c.setopt(c.HTTPHEADER, http_header) #must be list, not dict
c.setopt(c.POST, 1)
c.setopt(c.CONNECTTIMEOUT, 30)
c.setopt(c.TIMEOUT, 30)
c.setopt(c.WRITEFUNCTION, dump_res)
c.setopt(c.POSTFIELDS, audio_data)
c.setopt(c.POSTFIELDSIZE, f_len)
c.perform() #pycurl.perform() has no return val
# 將data中的數(shù)據(jù)保存到名為filename的WAV文件中
def save_wave_file(filename, data):
wf = wave.open(filename, 'wb')
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(SAMPLING_RATE)
wf.writeframes("".join(data))
wf.close()
NUM_SAMPLES = 2000 # pyAudio內(nèi)部緩存的塊的大小
SAMPLING_RATE = 8000 # 取樣頻率
LEVEL = 1500 # 聲音保存的閾值
COUNT_NUM = 20 # NUM_SAMPLES個(gè)取樣之內(nèi)出現(xiàn)COUNT_NUM個(gè)大于LEVEL的取樣則記錄聲音
SAVE_LENGTH = 8 # 聲音記錄的最小長(zhǎng)度:SAVE_LENGTH * NUM_SAMPLES 個(gè)取樣
token = get_token()
key = '05ba411481c8cfa61b91124ef7389767'
api = 'http://www.tuling123.com/openapi/api?key=' + key + '&info='
while(True):
print "kaishi"
os.system('arecord -D "plughw:1,0" -f S16_LE -d 3 -r 8000 /home/pi/yuyinduihua/2.wav')
use_cloud(token)
print "輸入內(nèi)容"
print duihua
############
#語(yǔ)音識(shí)別進(jìn)行開門的設(shè)置#
############
if(cmp(duihua,'開門,')==0):
print "識(shí)別開門"
GPIO.output(11, GPIO.LOW)
info = duihua
duihua = ""
request = api + info
response = getHtml(request)
dic_json = json.loads(response)
#print '機(jī)器人: '.decode('utf-8') + dic_json['text']
#huida = ' '.decode('utf-8') + dic_json['text']
a = dic_json['text']
print type(a)
unicodestring = a
# 將Unicode轉(zhuǎn)化為普通Python字符串:"encode"
utf8string = unicodestring.encode("utf-8")
print type(utf8string)
print str(a)
url = "http://tsn.baidu.com/text2audio?tex="+dic_json['text']+"&lan=zh&per=0&pit=1&spd=7&cuid=7519663&ctp=1&tok=24.ece6cfa6b5821f481deceef114da892e.2592000.1467287744.282335-7519663"
os.system('mpg123 "%s"'%(url))
print "wait..1s"
time.sleep(1)