python調用百度語音(語音辨識-鬥地主語音記牌器)

來源:互聯網
上載者:User

標籤:main   strong   ack   ==   擷取檔案   tar   ref   file   nat   

一、概述

本篇簡要介紹百度語音語音辨識的基本使用(其實是鬥地主時想弄個記牌器又沒money,抓包什麼的又不會,只好搞語音辨識的了)

 

二、建立應用

 

開啟百度語音官網,產品與使用->語音辨識->立即使用->建立應用

出現如下頁面

依照提示依次填寫,最終結果

(ps:我就想弄個記牌的,就起了個計數器的名)

點右方的 ‘查看key’ 記下App ID,API Key,Secret Key。接下來要用到

 

 

需要安裝模組pip install baidu-aippip install pyaudio

 

語音辨識代碼

from aip import AipSpeech""" 你的 APPID AK SK """APP_ID = ‘你記下的APP_ID‘API_KEY = ‘你記下的API_KEY‘SECRET_KEY = ‘你記下的SECRET_KEY‘client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)# 讀取檔案def get_file_content(filePath):    with open(filePath, ‘rb‘) as fp:        return fp.read()# 識別本地檔案li=client.asr(get_file_content(‘01.pcm‘), ‘pcm‘, 8000, {    ‘lan‘: ‘zh‘,})print(li)# 從URL擷取檔案識別# client.asr(‘‘, ‘pcm‘, 16000, {#     ‘url‘: ‘http://121.40.195.233/res/16k_test.pcm‘,#     ‘callback‘: ‘http://xxx.com/receive‘,# })

python錄音代碼

import wavefrom pyaudio import PyAudio,paInt16framerate=8000NUM_SAMPLES=2000channels=1sampwidth=2TIME=2def save_wave_file(filename,data):    ‘‘‘save the date to the wavfile‘‘‘    wf=wave.open(filename,‘wb‘)    wf.setnchannels(channels)    wf.setsampwidth(sampwidth)    wf.setframerate(framerate)    wf.writeframes(b"".join(data))    wf.close()def my_record():    pa=PyAudio()    stream=pa.open(format = paInt16,channels=1,                   rate=framerate,input=True,                   frames_per_buffer=NUM_SAMPLES)    my_buf=[]    count=0    while count<TIME*5:#控制錄音時間        string_audio_data = stream.read(NUM_SAMPLES)        my_buf.append(string_audio_data)        count+=1        print(‘.‘)    save_wave_file(‘01.pcm‘,my_buf)    stream.close()chunk=2014def play():    wf=wave.open(r"01.pcm",‘rb‘)    p=PyAudio()    stream=p.open(format=p.get_format_from_width(wf.getsampwidth()),channels=    wf.getnchannels(),rate=wf.getframerate(),output=True)    while True:        data=wf.readframes(chunk)        if data=="":break        stream.write(data)    stream.close()    p.terminate()if __name__ == ‘__main__‘:    my_record()    print(‘Over!‘)    play()

 

效果如:

 

協助文檔:

百度語音協助文檔or手冊

 

 

 

 

三、後記

本代碼未完全實現,有興趣可自行整理,玩鬥地主的時候聲音可能要大點,因為識別有時候會報3001錯誤,音頻品質過差,不過被打可別找我

python調用百度語音(語音辨識-鬥地主語音記牌器)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.