首页 > 解决方案 > 如何在 Google Colab 中进行文本到语音的转换?

问题描述

我知道像 Google Text to Speech 这样的库。但是,这在 Colab 中不起作用。我最近在 Colab 中遇到了一个复杂的笔记本https://colab.research.google.com/github/tugstugi/pytorch-dc-tts/blob/master/notebooks/EnglishTTS.ipynb#scrollTo=jLU2p4Gq_12d我们可以在其中转换文本讲话。但是,有没有一种简单的方法可以在 Google Colab 中使用 Google Text to Speech 或其他库?

这样我就提供了一个"My name is XYZ"字符串 - 并在 Colab 笔记本中说出来。(这发生在我提供的链接中,但非常复杂)。

PS 如果可能的话,我希望像 GTTS 一样自动播放音频。在这个笔记本中,我们需要点击语音输出的播放按钮。

标签: pythongoogle-cloud-platformtext-to-speechgoogle-colaboratorygtts

解决方案


我终于解决了这个问题。一种简单的方法是将 Google Text to Speech 与 IPython 的 Audio 方法结合使用。以下代码片段可以在几行内为您完成这项工作!您还可以查看我在这里创建的 Colab 笔记本https://colab.research.google.com/drive/1wMg9ZV2WH2ugAC-6iZLUkEH3V6XxI3H-演示了这一点。

from gtts import gTTS #Import Google Text to Speech
from IPython.display import Audio #Import Audio method from IPython's Display Class
tts = gTTS('hello joyjit') #Provide the string to convert to speech
tts.save('1.wav') #save the string converted to speech as a .wav file
sound_file = '1.wav'
Audio(sound_file, autoplay=True) 

#Autoplay = True will play the sound automatically
#If you would not like to play the sound automatically, simply pass Autoplay = False.

推荐阅读