1.作者について
Gao Zhixiang、男性、西安工科大学電子情報学部、2021年大学院生
研究の方向性:マシンビジョンと人工知能
Eメール:[email protected]
Liu Shuaibo、男性、西安工科大学電子情報学部、2021年大学院生、Zhang Hongwei人工知能研究グループ
研究の方向性:マシンビジョンと人工知能
Eメール:[email protected]
2.BaiduAPIに基づく北京語の認識
2.1音声認識
音声認識とは、音声信号を対応するテキスト情報に変換することです。このシステムは、主に特徴抽出、音響モデル、言語モデル、辞書、デコードの4つの部分で構成されています。さらに、特徴をより効果的に抽出するために、必要になることがよくあります。音声信号はフィルタリングされ、フレーム化され、その他の音声データの前処理作業が行われ、分析される音声信号は元の信号から適切に抽出されます。
一般的なプロセス:
2.2BaiduAPI呼び出しメソッド
Baiduインテリジェント開発プラットフォームでの音声技術などのアプリケーションの確立を通じて、相対的な技術権限機能が取得されます。
作成が完了すると、Baiduからアプリケーションのリストが表示されます。ここでAppID、APIキー、およびシークレットキーを使用してAPI呼び出しを行うことができます。
3.3。実験
3.1実験の準備
この実験では、識別にBaidu APIを使用するため、baidu-aipモジュールをインストールする必要があります
。まず、コマンドラインを開き、pipinstallbaidu-aipと入力します。
上記のように、インストールは成功しています。
このプロジェクトはpyqt5を使用してインターフェースを作成するため、pyqt5モジュールもインストールする必要があります。
コマンドラインを開き、pipinstallpyqt5と入力してインストールします。
次に、Baidu AIの公式Webサイトにアクセスしてアプリケーションを作成し、AppID、APIKey、SecretKeyを取得する必要があります。
3.2実験結果
ここでは、対応する番号を直接入力し、Enterキーの後に記録を開始し、Baidu検索インターフェイスをポップアップすると、直接検索できます。つまり、実験は成功です。
4.実験コード
import wave
import requests
import time
import base64
from pyaudio import PyAudio, paInt16
import webbrowser
framerate = 16000 # 采样率
num_samples = 2000 # 采样点
channels = 1 # 声道
sampwidth = 2 # 采样宽度2bytes
FILEPATH = 'speech.wav'
base_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s"
APIKey = "********" # 填写自己的APIKey
SecretKey = "**********" # 填写自己的SecretKey
HOST = base_url % (APIKey, SecretKey)
def getToken(host):
res = requests.post(host)
return res.json()['access_token']
def save_wave_file(filepath, data):
wf = wave.open(filepath, '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=channels,
rate=framerate, input=True, frames_per_buffer=num_samples)
my_buf = []
# count = 0
t = time.time()
print('正在录音...')
while time.time() < t + 4: # 秒
string_audio_data = stream.read(num_samples)
my_buf.append(string_audio_data)
print('录音结束.')
save_wave_file(FILEPATH, my_buf)
stream.close()
def get_audio(file):
with open(file, 'rb') as f:
data = f.read()
return data
def speech2text(speech_data, token, dev_pid=1537):
FORMAT = 'wav'
RATE = '16000'
CHANNEL = 1
CUID = '*******'
SPEECH = base64.b64encode(speech_data).decode('utf-8')
data = {
'format': FORMAT,
'rate': RATE,
'channel': CHANNEL,
'cuid': CUID,
'len': len(speech_data),
'speech': SPEECH,
'token': token,
'dev_pid': dev_pid
}
url = 'https://vop.baidu.com/server_api'
headers = {
'Content-Type': 'application/json'}
# r=requests.post(url,data=json.dumps(data),headers=headers)
print('正在识别...')
r = requests.post(url, json=data, headers=headers)
Result = r.json()
if 'result' in Result:
return Result['result'][0]
else:
return Result
def openbrowser(text):
maps = {
'百度': ['百度', 'baidu'],
'腾讯': ['腾讯', 'tengxun'],
'网易': ['网易', 'wangyi']
}
if text in maps['百度']:
webbrowser.open_new_tab('https://www.baidu.com')
elif text in maps['腾讯']:
webbrowser.open_new_tab('https://www.qq.com')
elif text in maps['网易']:
webbrowser.open_new_tab('https://www.163.com/')
else:
webbrowser.open_new_tab('https://www.baidu.com/s?wd=%s' % text)
if __name__ == '__main__':
flag = 'y'
while flag.lower() == 'y':
print('请输入数字选择语言:')
devpid = input('1536:普通话(简单英文),1537:普通话(有标点),1737:英语,1637:粤语,1837:四川话\n')
my_record()
TOKEN = getToken(HOST)
speech = get_audio(FILEPATH)
result = speech2text(speech, TOKEN, int(devpid))
print(result)
if type(result) == str:
openbrowser(result.strip(','))
flag = input('Continue?(y/n):')