0. 太長不看系列,直接使用
在1.2官網注冊后拿到APISecret和APIKey,直接復制文章2.4demo代碼,確定音頻為wav格式,采樣率為16K,在命令行執行
python single_sentence_recognition.py -client_secret=你的client_secret -client_id=你的client_id -file_path=test.wav
識別結果
使用中有任何問題,歡迎留言提問。
1. Python調用標貝科技語音識別接口,實現語音轉文字
1.1 環境准備:
Python 3
1.2 獲取權限
標貝科技 https://ai.data-baker.com/#/index
填寫邀請碼fwwqgs,每日免費調用量還可以翻倍
1.2.1 登錄
點擊產品地址進行登錄,支持短信、密碼、微信三種方式登錄。
1.2.2 創建新應用
登錄后進入【首頁概覽】,各位開發者可以進行創建多個應用。包括一句話識別、長語音識別、錄音文件識別;在線合成、離線合成、長文本合成。
1.2.3 選擇服務
進入【已創建的應用】,左側選擇您需調用的AI技術服務,右側展示對應服務頁面概覽(您可查詢用量、管理套餐、購買服務量、自主獲取授權、預警管理)。
1.2.4 獲取Key&Secret
通過服務 / 授權管理,獲取對應參數,進行開發配置(獲取訪問令牌token)
拿到Key和Secret就可以正式使用啦!
2. 代碼實現
2.1 獲取access_token
在拿到Key和Secret后,我們還需要調用授權接口獲取access_token,這個access_token有效時長是24小時。
# 獲取access_token用於鑒權 def get_access_token(client_secret, client_id): grant_type = "client_credentials" url = "https://openapi.data-baker.com/oauth/2.0/token?grant_type={}&client_secret={}&client_id={}"\ .format(grant_type, client_secret, client_id) try: response = requests.post(url) response.raise_for_status() except Exception as e: print(e) return else: access_token = json.loads(response.text).get('access_token') return access_token
2.2 獲取識別文本
拿到access_token后,調用語音識別接口,就可以獲得識別后文本
# 獲取識別后文本 def get_text(file, headers): url = "https://asr.data-baker.com/asr/api?" response = requests.post(url, data=file, headers=headers) code = json.loads(response.text).get("code") text = json.loads(response.text).get("text") if code != 20000: print(response.text) return text
2.3 配置接口參數
client_secret和client_id:在文章1.2的官網獲取,必填
file_path:文件保存路徑,必填
audio_format:音頻格式,默認wav,根據文件可以自己選填
sample_rate:采樣率,默認16000,根據文件可以自己選填
add_pct:是否在靜音處添加標點,默認true
# 獲取命令行輸入參數 def get_args(): parser = argparse.ArgumentParser(description='ASR') parser.add_argument('-client_secret', type=str, required=True) parser.add_argument('-client_id', type=str, required=True) parser.add_argument('-file_path', type=str, required=True) parser.add_argument('--audio_format', type=str, default='wav') parser.add_argument('--sample_rate', type=str, default='16000') parser.add_argument('--add_pct', type=str, default='true') args = parser.parse_args() return args
2.4 完整demo
#!/usr/bin/env python # coding: utf-8 import requests import json import argparse # 獲取access_token用於鑒權 def get_access_token(client_secret, client_id): grant_type = "client_credentials" url = "https://openapi.data-baker.com/oauth/2.0/token?grant_type={}&client_secret={}&client_id={}"\ .format(grant_type, client_secret, client_id) try: response = requests.post(url) response.raise_for_status() except Exception as e: print(e) return else: access_token = json.loads(response.text).get('access_token') return access_token # 獲取識別后文本 def get_text(file, headers): url = "https://asr.data-baker.com/asr/api?" response = requests.post(url, data=file, headers=headers) code = json.loads(response.text).get("code") text = json.loads(response.text).get("text") if code != 20000: print(response.text) return text # 獲取命令行輸入參數 def get_args(): parser = argparse.ArgumentParser(description='ASR') parser.add_argument('-client_secret', type=str, required=True) parser.add_argument('-client_id', type=str, required=True) parser.add_argument('-file_path', type=str, required=True) parser.add_argument('--audio_format', type=str, default='wav') parser.add_argument('--sample_rate', type=str, default='16000') parser.add_argument('--add_pct', type=str, default='true') args = parser.parse_args() return args if __name__ == '__main__': args = get_args() # 獲取access_token client_secret = args.client_secret client_id = args.client_id access_token = get_access_token(client_secret, client_id) # 讀取音頻文件 with open(args.file_path, 'rb') as f: file = f.read() # 填寫Header信息 audio_format = args.audio_format sample_rate = args.sample_rate add_pct = args.add_pct headers = {'access_token': access_token, 'audio_format': audio_format, 'sample_rate': sample_rate, 'add_pct': add_pct} text = get_text(file, headers) print(text)
2.5 執行
復制所有代碼,確定音頻為wav格式,采樣率為16K,在命令行執行
python single_sentence_recognition.py -client_secret=你的client_secret -client_id=你的client_id -file_path=test.wav
結果