Text-to-Speech 文字轉換語音是 Azure Speech 語音服務中其中一個項目。在語音服務之中,除了提供文字和語音的互相轉換以外,還有一些更進階的服務,如語音翻譯、從話語中理解發話者的意圖、辨識發話者身份,等等。在這次要執行的 Text-to-Speech 是為了把之前用 OCR 辨識到的韓文,直接轉換成語音,讓使用者可以不但可以從翻譯知道意思,也可以學習發音。
python套件pip3.7 install azure-cognitiveservices-speech
一開始需要設定 speech config,輸入金鑰和當初申請時所選擇的區域代碼,會將字串傳給 Speech 轉換成語音,語音內容可以在本地端存成wav檔。
關於選擇區域的代碼,可以執行az account list-locations -o table,會列出所有區域相對應的代碼,在這邊我選擇的是美國東部 2 ,對應到的代碼就是eastus2。下面有列出對照表可供參考。
另外,這裡也需要設定發音的語系,關於語系設定的代碼,可以從此語言支援文件查詢。
from azure.cognitiveservices.speech import (
SpeechConfig,
SpeechSynthesizer,
)
# 輸入金鑰和區域,設定config
SPEECH_KEY = "你的金鑰"
speech_config = SpeechConfig(subscription=SPEECH_KEY, region="eastus2")
# 因為後續要針對韓文發音,所以設定成"ko-KR"
speech_config.speech_synthesis_language = "ko-KR"
# 設定轉換後存檔路徑
file_name = "your_path.wav"
audio_config = AudioOutputConfig(filename=file_name)
synthesizer = SpeechSynthesizer(
speech_config=speech_config, audio_config=audio_config
)
# 開始轉換成語音
synthesizer.speak_text_async(string)
這邊列出執行az account list-locations -o table之後,會出現的區域代碼表,方便大家查詢。
| DisplayName | Name | RegionalDisplayName |
|---|---|---|
| East US | eastus | (US) East US |
| East US 2 | eastus2 | (US) East US 2 |
| South Central US | southcentralus | (US) South Central US |
| West US 2 | westus2 | (US) West US 2 |
| West US 3 | westus3 | (US) West US 3 |
| Australia East | australiaeast | (Asia Pacific) Australia East |
| Southeast Asia | southeastasia | (Asia Pacific) Southeast Asia |
| North Europe | northeurope | (Europe) North Europe |
| Sweden Central | swedencentral | (Europe) Sweden Central |
| UK South | uksouth | (Europe) UK South |
| West Europe | westeurope | (Europe) West Europe |
| Central US | centralus | (US) Central US |
| North Central US | northcentralus | (US) North Central US |
| West US | westus | (US) West US |
| South Africa North | southafricanorth | (Africa) South Africa North |
| Central India | centralindia | (Asia Pacific) Central India |
| East Asia | eastasia | (Asia Pacific) East Asia |
| Japan East | japaneast | (Asia Pacific) Japan East |
| Jio India West | jioindiawest | (Asia Pacific) Jio India West |
| Korea Central | koreacentral | (Asia Pacific) Korea Central |
| Canada Central | canadacentral | (Canada) Canada Central |
| France Central | francecentral | (Europe) France Central |
| Germany West Central | germanywestcentral | (Europe) Germany West Central |
| Norway East | norwayeast | (Europe) Norway East |
| Switzerland North | switzerlandnorth | (Europe) Switzerland North |
| UAE North | uaenorth | (Middle East) UAE North |
| Brazil South | brazilsouth | (South America) Brazil South |
| Central US (Stage) | centralusstage | (US) Central US (Stage) |
| East US (Stage) | eastusstage | (US) East US (Stage) |
| East US 2 (Stage) | eastus2stage | (US) East US 2 (Stage) |
| North Central US (Stage) | northcentralusstage | (US) North Central US (Stage) |
| South Central US (Stage) | southcentralusstage | (US) South Central US (Stage) |
| West US (Stage) | westusstage | (US) West US (Stage) |
| West US 2 (Stage) | westus2stage | (US) West US 2 (Stage) |
| Asia | asia | Asia |
| Asia Pacific | asiapacific | Asia Pacific |
| Australia | australia | Australia |
| Brazil | brazil | Brazil |
| Canada | canada | Canada |
| Europe | europe | Europe |
| Global | global | Global |
| India | india | India |
| Japan | japan | Japan |
| United Kingdom | uk | United Kingdom |
| United States | unitedstates | United States |
| East Asia (Stage) | eastasiastage | (Asia Pacific) East Asia (Stage) |
| Southeast Asia (Stage) | southeastasiastage | (Asia Pacific) Southeast Asia (Stage) |
| Central US EUAP | centraluseuap | (US) Central US EUAP |
| East US 2 EUAP | eastus2euap | (US) East US 2 EUAP |
| West Central US | westcentralus | (US) West Central US |
| South Africa West | southafricawest | (Africa) South Africa West |
| Australia Central | australiacentral | (Asia Pacific) Australia Central |
| Australia Central 2 | australiacentral2 | (Asia Pacific) Australia Central 2 |
| Australia Southeast | australiasoutheast | (Asia Pacific) Australia Southeast |
| Japan West | japanwest | (Asia Pacific) Japan West |
| Jio India Central | jioindiacentral | (Asia Pacific) Jio India Central |
| Korea South | koreasouth | (Asia Pacific) Korea South |
| South India | southindia | (Asia Pacific) South India |
| West India | westindia | (Asia Pacific) West India |
| Canada East | canadaeast | (Canada) Canada East |
| France South | francesouth | (Europe) France South |
| Germany North | germanynorth | (Europe) Germany North |
| Norway West | norwaywest | (Europe) Norway West |
| Sweden South | swedensouth | (Europe) Sweden South |
| Switzerland West | switzerlandwest | (Europe) Switzerland West |
| UK West | ukwest | (Europe) UK West |
| UAE Central | uaecentral | (Middle East) UAE Central |
| Brazil Southeast | brazilsoutheast | (South America) Brazil Southeast |
現在已經具備 OCR、翻譯和文字轉換語音的工具了,所以下一篇我們來組裝韓文翻譯機器人。(謎之聲:搜集完武器,要打第二個中頭目了~)