python - 如何在 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 一样自动播放音频。在这个笔记本中,我们需要点击语音输出的播放按钮。
解决方案
我终于解决了这个问题。一种简单的方法是将 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.
推荐阅读
- python - 为 Pyspark 数据框按多列重新分区
- regex - Grep 正则表达式重叠匹配
- php - 使用 PHP 将 HTML 表单数据作为表格发布
- sql - 基于条件比较两个子表的聚合返回记录
- java - 将协议缓冲区编码的消息从 Python 服务器发送到 Java 客户端
- xml - 创建一个按属性值排序的表
- c# - 类型的 DbContext 不能被池化
- c++ - 当 C++ 函数不从右值引用参数“移动”时,这是合理的方法吗?
- rust - 使用 into_serde 反序列化字符串会使应用程序恐慌
- oracle - 为什么使用来自不同应用程序的相同查询和相同 ODBC 连接获取不同数量的记录?