国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

基于python實現百度語音識別和圖靈對話

瀏覽:3日期:2022-07-06 15:35:17

圖例如下

基于python實現百度語音識別和圖靈對話

https://github.com/Dongvdong/python_Smartvoice

上電后,只要周圍聲音超過 2000,開始錄音5S 錄音上傳百度識別,并返回結果文字輸出 繼續等待,周圍聲音是否超過2000,沒有就等待。 點用電腦API語音交互

代碼如下

# -*- coding: utf-8 -*-# 樹莓派from pyaudio import PyAudio, paInt16import numpy as npfrom datetime import datetimeimport waveimport timeimport requests#導入requests庫import urllib, urllib.request, pycurlimport base64import jsonimport osimport sysfrom imp import reload # 調用電腦API生成語音交互import speechimport win32apiimport osimport sysimport timeimport win32con reload(sys) #sys.setdefaultencoding( 'utf-8' )#一些全局變量save_count = 0save_buffer = []t = 0sum = 0time_flag = 0flag_num = 0filename = ’’duihua = ’1’def getHtml(url): html= requests.get(url) # html.encoding = ’utf-8’#防止中文亂碼 return html.textdef get_token(): apiKey = 'AxXDYEN27Ks9XHocsGmCEdPm' secretKey = '61cd52759f4d704d91c155a22ff7183d' auth_url = 'https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + apiKey + '&client_secret=' + secretKey; res = requests.get(auth_url) #res.encoding = ’utf-8’#防止中文亂碼 #print (res.text) return json.loads(res.text)[’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])#終于搞定了,在這里可以輸出,返回的語句 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 = '9120612' #產品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中的數據保存到名為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(b''.join(data)) wf.close()NUM_SAMPLES = 2000 # pyAudio內部緩存的塊的大小SAMPLING_RATE = 8000 # 取樣頻率LEVEL = 1500 # 聲音保存的閾值COUNT_NUM = 20 # NUM_SAMPLES個取樣之內出現COUNT_NUM個大于LEVEL的取樣則記錄聲音SAVE_LENGTH = 8 # 聲音記錄的最小長度:SAVE_LENGTH * NUM_SAMPLES 個取樣exception_on_overflow=False# 開啟聲音輸入pyaudio對象pa = PyAudio()stream = pa.open(format=paInt16, channels=1, rate=SAMPLING_RATE, input=True,frames_per_buffer=NUM_SAMPLES)token = get_token()#獲取tokenkey = ’35ff2856b55e4a7f9eeb86e3437e23fe’api = ’http://www.tuling123.com/openapi/api?key=’ + key + ’&info=’while(True): # 讀入NUM_SAMPLES個取樣 string_audio_data = stream.read(NUM_SAMPLES,False); # 將讀入的數據轉換為數組 audio_data = np.fromstring(string_audio_data, dtype=np.short) # 計算大于LEVEL的取樣的個數 large_sample_count = np.sum( audio_data > LEVEL ) temp = np.max(audio_data) if temp > 2000 and t == 0: t = 1#開啟錄音 print ('---------主人我在聽你說!(5S)----------') begin = time.time() # print (temp) if t: #print (np.max(audio_data)) if np.max(audio_data)<1000: sum += 1 # print (sum) end = time.time() if end-begin>5: time_flag = 1 # print ('五秒到了,準備結束') # 如果個數大于COUNT_NUM,則至少保存SAVE_LENGTH個塊 if large_sample_count > COUNT_NUM: save_count = SAVE_LENGTH else: save_count -= 1 if save_count < 0: save_count = 0 if save_count > 0: # 將要保存的數據存放到save_buffer中 save_buffer.append(string_audio_data ) else: # 將save_buffer中的數據寫入WAV文件,WAV文件的文件名是保存的時刻 #if time_flag: if len(save_buffer) > 0 or time_flag:#filename = datetime.now().strftime('%Y-%m-%d_%H_%M_%S') + '.wav'#原本是用時間做名字filename = str(flag_num)+'.wav'flag_num += 1save_wave_file(filename, save_buffer)save_buffer = []t = 0sum =0time_flag = 0 # print (filename, '保存成功正在進行語音識別')use_cloud(token) # print (duihua)info = duihuaduihua = ''request = api + str(info)response = getHtml(request) # print ( '-----1-----')dic_json = json.loads(response) a = dic_json[’text’] unicodestring = a# 將Unicode轉化為普通Python字符串:'encode'utf8string = unicodestring.encode('utf-8') print ('科塔娜:'+str(a)) # 電腦說話speech.say(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=25.41bf315625c68b3e947c49b90788532d.315360000.1798261651.282335-9120612'os.system(’mpg123 '%s'’%(url))

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: 百度 Python
相關文章:
主站蜘蛛池模板: 欧美一级视频高清片 | 91精品国产91久久久久青草 | 午夜一区二区福利视频在线 | 欧美成人午夜 | 亚洲欧洲日产国产 最新 | 深夜福利国产 | 最新黄网 | 一级毛片在线免费观看 | 殴美毛片| 亚州毛片| 欧美一级aⅴ毛片 | 欧美亚洲在线观看 | 黑人边吃奶边扎下面激情视频 | 亚洲 欧美 精品 | 美女张开双腿让男人桶 | 99久久精品国产国产毛片 | 国产高清美女一级a毛片久久 | 美女张开腿给男人捅 | 爱逼综合网| 成人a在线观看 | 97操碰| 91伊人久久| 一级做a爱片特黄在线观看 一级做a爱片特黄在线观看免费看 | 一级毛片免费 | 精品综合久久久久久蜜月 | 亚洲国产精品久久久久久 | 国内黄色一级精品 | 成人毛片一区二区三区 | 国产精品v欧美精品v日本精 | 欧美日本韩国一区 | 很黄的网站在线观看 | 国产精品美女久久福利网站 | 亚州国产 | 国产精品久久国产三级国不卡顿 | 免费特级毛片 | 99视频在线观看免费 | 国产成人免费在线 | 日韩a无吗一区二区三区 | 国产欧美一区二区成人影院 | 日本一区二区三区在线 视频 | 亚洲欧美日韩精品久久亚洲区色播 |