基于Python實(shí)現(xiàn)語(yǔ)音識(shí)別和語(yǔ)音轉(zhuǎn)文字
前言
嗨嗨,大家好呀 ~ 今天給你們分享一個(gè)有趣的東西 ~
是一個(gè)語(yǔ)音識(shí)別跟語(yǔ)音轉(zhuǎn)文字的小工具
感興趣的朋友可以繼續(xù)往下滑咯

直接使用
在1.2官網(wǎng)注冊(cè)后拿到APISecret和APIKey,
直接復(fù)制文章2.4demo代碼,
保存為online_tts.py,
在命令行執(zhí)行
python online_tts.py -client_secret=你的client_secret -client_id=你的client_id -file_save_path=test.wav --text=今天天氣不錯(cuò)
獲取權(quán)限
Python調(diào)用標(biāo)貝科技語(yǔ)音合成接口,實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音
1.環(huán)境準(zhǔn)備
Python 3
2.獲取權(quán)限
標(biāo)貝科技 https://ai.data-baker.com/#/?source=qwer12
1.2.1 登錄
點(diǎn)擊產(chǎn)品地址進(jìn)行登錄,支持短信、密碼、微信三種方式登錄。

1.2.2 創(chuàng)建新應(yīng)用
登錄后進(jìn)入【首頁(yè)概覽】,
各位開(kāi)發(fā)者可以進(jìn)行創(chuàng)建多個(gè)應(yīng)用。
包括一句話識(shí)別、長(zhǎng)語(yǔ)音識(shí)別、錄音文件識(shí)別;
在線合成、離線合成、長(zhǎng)文本合成。

1.2.3 選擇服務(wù)
進(jìn)入【已創(chuàng)建的應(yīng)用】,左側(cè)選擇您需調(diào)用的AI技術(shù)服務(wù),右側(cè)展示對(duì)應(yīng)服務(wù)頁(yè)面概覽(您可查詢用量、管理套餐、購(gòu)買服務(wù)量、自主獲取授權(quán)、預(yù)警管理)。

1.2.4 獲取Key&Secret
通過(guò)服務(wù) / 授權(quán)管理,獲取對(duì)應(yīng)參數(shù),
進(jìn)行開(kāi)發(fā)配置

拿到Key和Secret就可以正式使用啦!
代碼實(shí)現(xiàn)
1.獲取access_token
在拿到Key和Secret后,
我們還需要調(diào)用授權(quán)接口獲取access_token,
這個(gè)access_token有效時(shí)長(zhǎng)是24小時(shí)。
需要源碼、教程,或者是自己有關(guān)python不懂的問(wèn)題,都可以來(lái)這里哦 https://jq.qq.com/?_wv=1027&k=xJU8WKpY 這里還有學(xué)習(xí)資料與免費(fèi)課程領(lǐng)取
# 獲取access_token用于鑒權(quán)
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)
response = requests.post(url)
access_token = json.loads(response.text).get('access_token')
return access_token
2.獲取轉(zhuǎn)換后音頻
拿到access_token后,
調(diào)用語(yǔ)音合成接口,
就可以獲得生成的音頻
# 獲取轉(zhuǎn)換后音頻
def get_audio(data):
url = "https://openapi.data-baker.com/tts?access_token={}&domain={}&language={}&voice_name={}&text={}&audiotype={}".format(data['access_domain'], data['domain'], data['language'], data['voice_name'], data['text'], data['audiotype'])
response = requests.post(url)
content_type = response.headers['Content-Type']
if 'audio' not in content_type:
raise Exception(response.text)
return response.content
3.配置接口參數(shù)
client_secret和client_id:在文章1.2的官網(wǎng)獲取,必填
file_save_path:文件保存路徑,必填
text:需要轉(zhuǎn)換的文本內(nèi)容
audiotype:音頻類型,默認(rèn)16K采樣率wav格式
domain:所屬領(lǐng)域,默認(rèn)1
language:合成后文本語(yǔ)言,默認(rèn)中文“zh"
voice_name:發(fā)音人選擇,默認(rèn)“Lingling",
# 獲取命令行輸入?yún)?shù)
def get_args():
text = '歡迎使用標(biāo)貝開(kāi)發(fā)平臺(tái)。'
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_save_path', type=str, required=True)
parser.add_argument('--text', type=str, default=text)
parser.add_argument('--audiotype', type=str, default='6')
parser.add_argument('--domain', type=str, default='1')
parser.add_argument('--language', type=str, default='zh')
parser.add_argument('--voice_name', type=str, default='Lingling')
args = parser.parse_args()
return args

4.完整demo
#!/usr/bin/env python
# coding: utf-8
import requests
import json
import argparse
# 獲取access_token用于鑒權(quán)
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)
response = requests.post(url)
access_token = json.loads(response.text).get('access_token')
return access_token
# 獲取轉(zhuǎn)換后音頻
def get_audio(data):
url = "https://openapi.data-baker.com/tts?access_token={}&domain={}&language={}&voice_name={}&text={}&audiotype={}".format(data['access_domain'], data['domain'], data['language'], data['voice_name'], data['text'], data['audiotype'])
response = requests.post(url)
content_type = response.headers['Content-Type']
if 'audio' not in content_type:
raise Exception(response.text)
return response.content
# 獲取命令行輸入?yún)?shù)
def get_args():
text = '歡迎使用標(biāo)貝開(kāi)發(fā)平臺(tái)。'
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_save_path', type=str, required=True)
parser.add_argument('--text', type=str, default=text)
parser.add_argument('--audiotype', type=str, default='6')
parser.add_argument('--domain', type=str, default='1')
parser.add_argument('--language', type=str, default='zh')
parser.add_argument('--voice_name', type=str, default='Lingling')
args = parser.parse_args()
return args
if __name__ == '__main__':
try:
args = get_args()
# 獲取access_token
client_secret = args.client_secret
client_id = args.client_id
access_token = get_access_token(client_secret, client_id)
# 讀取參數(shù)
audiotype = args.audiotype
domain = args.domain
language = args.language
voice_name = args.voice_name
text = args.text
data = {'access_domain': access_token, 'audiotype': audiotype, 'domain': domain, 'language': language,
'voice_name': voice_name, 'text': text}
content = get_audio(data)
#保存音頻文件
with open('test.wav', 'wb') as audio:
audio.write(content)
print("task finished successfully")
except Exception as e:
print(e)5.執(zhí)行
復(fù)制所有代碼,保存為online_tts.py,在命令行執(zhí)行
python online_tts.py -client_secret=你的client_secret -client_id=你的client_id -file_save_path=test.wav --text=今天天氣不錯(cuò)
到此這篇關(guān)于基于Python實(shí)現(xiàn)語(yǔ)音識(shí)別和語(yǔ)音轉(zhuǎn)文字的文章就介紹到這了,更多相關(guān)Python語(yǔ)音識(shí)別 語(yǔ)音轉(zhuǎn)文字內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python實(shí)現(xiàn)語(yǔ)音識(shí)別和語(yǔ)音合成功能
- python之語(yǔ)音識(shí)別speech模塊
- python3實(shí)現(xiàn)語(yǔ)音轉(zhuǎn)文字(語(yǔ)音識(shí)別)和文字轉(zhuǎn)語(yǔ)音(語(yǔ)音合成)
- python語(yǔ)音識(shí)別的轉(zhuǎn)換方法
- 基于Python創(chuàng)建語(yǔ)音識(shí)別控制系統(tǒng)
- python語(yǔ)音識(shí)別whisper的使用
- Linux下利用python實(shí)現(xiàn)語(yǔ)音識(shí)別詳細(xì)教程
- 基于Python實(shí)現(xiàn)語(yǔ)音識(shí)別功能
- Python實(shí)現(xiàn)語(yǔ)音識(shí)別vosk的示例代碼
相關(guān)文章
Python try except異常捕獲機(jī)制原理解析
這篇文章主要介紹了Python try except異常捕獲機(jī)制原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
針對(duì)Pandas的總結(jié)以及數(shù)據(jù)讀取_pd.read_csv()的使用詳解
這篇文章主要針對(duì)Pandas總結(jié)以及數(shù)據(jù)讀取_pd.read_csv()的使用詳解做出了實(shí)例,講解非常全面,值得收藏,需要的朋友可以參考下2023-03-03
Python中的數(shù)據(jù)可視化matplotlib與繪圖庫(kù)模塊
這篇文章介紹了Python中的數(shù)據(jù)可視化matplotlib與繪圖庫(kù)模塊,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
Ubuntu16安裝Python3.9的實(shí)現(xiàn)步驟
這篇文章主要介紹了Ubuntu16安裝Python3.9的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
打包FlaskAdmin程序時(shí)關(guān)于static路徑問(wèn)題的解決
近期寫了個(gè)基于Flask-admin的數(shù)據(jù)庫(kù)管理程序,通過(guò)pyinstaller打包,給別人用,經(jīng)過(guò)幾次嘗試,打包的數(shù)據(jù)一直找不到static里面的樣式文件,查閱資料后,最總把問(wèn)題搞定了。寫下處理流程,供后來(lái)人參考2021-09-09
Python實(shí)現(xiàn)將Word表格嵌入到Excel中
把Word中的表格轉(zhuǎn)到Excel中,順便做一個(gè)調(diào)整。這個(gè)需求在實(shí)際工作中,很多人還是經(jīng)常碰到的!本文就將介紹如何利用Python實(shí)現(xiàn)這一功能,需要的朋友可以了解一下2021-12-12

