百度AI攻略:語音識別

1.功能描述:python

將60秒內的語音快速識別爲文字,適用於手機語音輸入、語音搜索、人機對話等語音交互場景json

2.平臺接入api

具體接入方式比較簡單,能夠參考個人另外一個帖子,這裏就不重複了:app

http://ai.baidu.com/forum/topic/show/943327post

3.調用攻略(Python3)及評測測試

3.1首先認證受權:ui

在開始調用任何API以前須要先進行認證受權,具體的說明請參考:編碼

http://ai.baidu.com/docs#/Auth/topurl

具體Python3代碼以下:code

# -*- coding: utf-8 -*-

#!/usr/bin/env python

import urllib

import base64

import json

#client_id 爲官網獲取的AK, client_secret 爲官網獲取的SK

client_id =【百度雲應用的AK】

client_secret =【百度雲應用的SK】

#獲取token

def get_token():

host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret

request = urllib.request.Request(host)

request.add_header('Content-Type', 'application/json; charset=UTF-8')

response = urllib.request.urlopen(request)

token_content = response.read()

#print (token_content)

if token_content:

token_info = json.loads(token_content)

token_key = token_info['access_token']

return token_key

3.2百度語音識別分析接口調用:

詳細說明請參考:https://ai.baidu.com/docs#/ASR-API-PRO/top

說明的比較清晰,這裏就不重複了。

你們須要注意的是:

格式支持:pcm(不壓縮)、wav(不壓縮,pcm編碼)、amr(壓縮格式)、m4a(AAC編碼);固定16k 採樣率;

系統支持語言種類 普通話

 

Python3調用代碼以下:

# 只支持 pcm/wav/amr 格式,極速版額外支持m4a 格式

# 輸入參數須要識別的文件

def asr(AUDIO_FILE):

    # 文件格式

    FORMAT = AUDIO_FILE[-3:];  # 文件後綴只支持 pcm/wav/amr 格式,極速版額外支持m4a 格式

    print(FORMAT)

    CUID = '123456PYTHON';

    # 採樣率

    RATE = 16000;  # 固定值   

    # 普通版

    DEV_PID = 1537;  # 1537 表示識別普通話,使用輸入法模型。1536表示識別普通話,使用搜索模型。根據文檔填寫PID,選擇語言及識別模型

    ASR_URL = 'http://vop.baidu.com/server_api'

 

    token = get_token()

    speech_data = []

    with open(AUDIO_FILE, 'rb') as speech_file:

        speech_data = speech_file.read()

    length = len(speech_data)

    if length == 0:

        print('file %s length read 0 bytes' % AUDIO_FILE)

    params = {'cuid': CUID, 'token': token, 'dev_pid': DEV_PID}

    #測試自訓練平臺須要打開如下信息

    #params = {'cuid': CUID, 'token': token, 'dev_pid': DEV_PID, 'lm_id' : LM_ID}

    params_query = urlencode(params);

    headers = {

        'Content-Type': 'audio/' + FORMAT + '; rate=' + str(RATE),

        'Content-Length': length

    }

    url = ASR_URL + "?" + params_query

    print("url is", url);

    print("header is", headers)

    # print post_data

    req = Request(ASR_URL + "?" + params_query, speech_data, headers)

    try:

        begin = timer()

        f = urlopen(req)

        result_str = f.read()

        print("Request time cost %f" % (timer() - begin))

    except  URLError as err:

        print('asr http response http code : ' + str(err.code))

        result_str = err.read()

    result_str = str(result_str, 'utf-8')

    print(result_str)

    with open("result.txt", "w") as of:

        of.write(result_str)

asr('../voc/16k.wav')

4.功能評測和建議

測試下來,總體識別效果不錯。百度語音識別的很準確,速度也很快,用起來很是的方便。能夠應用於

語音輸入

擺脫按鍵操做,經過語音識別直接輸入文字,快速返回識別結果,可應用於遊戲文字輸入、社交聊天、語音指令等多個場景,提升輸入效率及體驗

語音搜索

搜索內容直接以語音的方式輸入,響應速度更快,適用於音樂、電影、小說等內容搜索場景,讓搜索內容輸入更加便捷,高效

人機對話

經過極速API接口,將語音識別爲文字,毫秒級響應,可用於聊天機器人、故事機等近場語音識別環境,讓人機對話更加流暢天然

相關文章
相關標籤/搜索