Day 14 Azure cognitive service: Text-to-Speech- Azure 念给你听

Azure cognitive service: Text-to-Speech- Azure 念给你听

Text-to-Speech 文字转换语音是 Azure Speech 语音服务中其中一个项目。在语音服务之中,除了提供文字和语音的互相转换以外,还有一些更进阶的服务,如语音翻译、从话语中理解发话者的意图、辨识发话者身份,等等。在这次要执行的 Text-to-Speech 是为了把之前用 OCR 辨识到的韩文,直接转换成语音,让使用者可以不但可以从翻译知道意思,也可以学习发音。

申请 Azure Speech

  1. 进入https://portal.azure.com/#home
  2. 点选建立资源
  3. 搜寻并选择 speech 语音
  4. 自己命名。
  5. 找到可以选择定价层 Free F0 的区域,并选择 Free F0 。
  6. 给予标签。
  7. 检阅 + 建立。

安装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、翻译和文字转换语音的工具了,所以下一篇我们来组装韩文翻译机器人。(谜之声:搜集完武器,要打第二个中头目了~)


<<:  Day14. Module & #extend #prepend #include - Ruby 继承 part1

>>:  D-16. Rspec 从零开始写测试(二) factory_bot_rails && Largest Number At Least Twice of Others

Day21-路由守卫(Navigation Guards)

Navigation Guards有点像是生命周期的概念,变更路由前後时可以先执行一些动作,分别有「...

建立表与表之间的关联(Day28)

文章同时发布於: https://kevinyay945.com/smart-home-tutori...

爬虫怎麽爬 从零开始的爬虫自学 DAY24 python档案读写open( )

前言 各位早安,书接上回我们将程序码改得更方便阅读,还加上抓取连结的功能,今天我们要来介绍明天实作储...

[Day2] Android - Kotlin笔记: lazy

来介绍一下lazy 以下以TextView为例 我们一般init(初始化一个元件): private...

Scanners API-总金额篇

这个API可以取得总金额的排名, 步骤如下: (1)汇入需要的库 import shioaji as...