python语音转文字

Here you will get python text to speech example.

在这里,您将获得python文本语音转换示例。

As we know, some people have difficulty reading large amounts of text due to dyslexia and other learning disabilities. Some people have basic literary levels. They often get frustrated trying to browse the internet because so much of it is in text form or on other hand some people prefer to listen or watch a news article (or something like this) instead of reading. So to solve all these problems a concept comes into mind that is ”text to speech”.

众所周知,由于阅读障碍和其他学习障碍,有些人难以阅读大量文字。 有些人具有基本的文学水平。 他们经常尝试浏览Internet时感到沮丧,因为其中很多都是文本形式的,或者另一方面,某些人更喜欢听或看新闻文章(或类似的东西)而不是阅读。 因此,要解决所有这些问题,就会想到一个概念,即“文本到语音”。

So in this tutorial we are going to learn that how to convert text to speech in Python. Here we’ll show you two best and easiest ways to convert your text into speech

因此,在本教程中,我们将学习如何在Python中将文本转换为语音。 在这里,我们将向您展示将文本转换为语音的两种最简便的方法

  1. Text to speech without internet connection (using pyttsx3)

    文本到语音无需互联网连接(使用pyttsx3)

  2. Text to speech having internet connection (using gTTS)

    具有互联网连接的文本到语音(使用gTTS)

Python文字转语音示例 (Python Text to Speech Example)

方法1:使用pyttsx3 (Method 1: Using pyttsx3)

Pyttsx3 is an offline cross-platform Test-to-Speech library which is compatible with both Python 3 and Python 2 and supports multiple TTS engines

Pyttsx3是一个离线跨平台的“测试到语音转换”库,与Python 3和Python 2兼容,并支持多个TTS引擎

To use pyttsx3, first we have to download and install it. In order to install it open your command prompt or terminal and type this command.

要使用pyttsx3,首先我们必须下载并安装它。 为了安装它,请打开命令提示符或终端,然后键入此命令。

pip install pyttsx3

点安装pyttsx3

If you’re using windows operating system then you also have to install “pypiwin32” to make it work. To install pypiwin32 again type this command and hit enter in command prompt.

如果您使用的是Windows操作系统,则还必须安装“ pypiwin32 ”以使其正常运行。 要再次安装pypiwin32 ,请键入此命令,然后在命令提示符下按Enter。

python  -m  pip install pypiwin32

python -m pip安装pypiwin32

Make sure you’ve internet connection while running both of the command. It is one time process, after you’ve installed pyttsx3 now to use it, the program will be as shown below.

同时运行两个命令时,请确保您已连接互联网。 这是一次过程,现在安装pyttsx3以使用它后,程序将如下所示。

import pyttsx3
engine = pyttsx3.init()
engine.say("hello crazy programmer")
engine.setProperty('rate',120)
engine.setProperty('volume', 0.9)
engine.runAndWait()

In this program, in first Line we’re initializing pyttsx3 for use then we’re passing the text in method say(). After it we’re setting some properties like volume and rate of the voice. Here we’re passing 120 as rate, which means it will speak 120 words per minute and last line of above program will be produce an audio saying “hello crazy  programmer”.

在此程序中,在第一行中,我们将初始化pyttsx3以供使用,然后在方法say()中传递文本 之后,我们要设置一些属性,例如声音的音量和速率。 在这里,我们以120的速率传递,这意味着它将每分钟讲120个单词,并且上述程序的最后一行将产生一个音频,说“你好疯狂的程序员”。

We can also modify the voice like we can change it into female voice (by default its male), age and language. For more information please visit http://pyttsx3.readthedocs.io/en/latest/engine.html

我们还可以修改语音,就像可以将其更改为女性语音(默认为男性),年龄和语言一样。 有关更多信息,请访问http://pyttsx3.readthedocs.io/en/latest/engine.html

方法2:使用gTTS(Google文本到语音) (Method 2: Using gTTS (Google Text to Speech))

Google Text to Speech is one of the best TTS API out there, because it will generate audio as approximately similar to human voice while other APIs generate audio like a metallic voice or robotic voice. But there is also a disadvantage of gTTS, it will need an internet connection to convert the text into an audio. So it can be slow then other offline APIs.

Google Text to Speech是目前最好的TTS API之一,因为它会产生与人类语音大致相似的音频,而其他API会产生诸如金属语音或机器人语音之类的音频。 但是gTTS的另一个缺点是,它需要互联网连接才能将文本转换为音频。 因此,它可能比其他脱机API慢。

To install gTTS API open your command prompt or terminal and type this command:

要安装gTTS API,请打开命令提示符或终端,然后输入以下命令:

pip  install gTTS

点安装gTTS

Program for conversion will be as shown below.

转换程序如下所示。

from gtts import gTTS
tts = gTTS(text="Hello crazy programmer", lang='en')
tts.save("audio.mp3")

Unlike other APIs it will generate an audio and will save into the same directory where your program stored.

与其他API不同,它将生成音频并将其保存到程序存储的同一目录中。

To play this audio we’ll need another tool to play audio on command line.

要播放此音频,我们需要另一个工具在命令行上播放音频。

If you’re using Linux (eg. Ubuntu) then mpg321 will be best command line player.

如果您使用的是Linux(例如Ubuntu),则mpg321将是最佳的命令行播放器。

To install it open terminal and type this command-

要安装它,请打开终端并输入以下命令:

sudo apt-get install mpg321

sudo apt安装mpg321

Now we can use this command to play any audio on command line:

现在我们可以使用此命令在命令行上播放任何音频:

mpg321 audio.mp3 -quiet

mpg321 audio.mp3-安静

To run this command in python program, add these two lines into above program

要在python程序中运行此命令,请将这两行添加到上述程序中

import os     #will be on the top
os.system('mpg321 audio.mp3 -quiet')

On other hand, for windows, we doesn’t have to install any new software or API to play the mp3 file. All we have to do is open command prompt and enter the name of your file it will play that file using your default media player. So to run this command in python add these two lines in above program.

另一方面,对于Windows,我们无需安装任何新软件或API即可播放mp3文件。 我们要做的就是打开命令提示符,然后输入文件名,它将使用默认的媒体播放器播放该文件。 因此,要在python中运行此命令,请在上述程序中添加这两行。

import os       #will be on the top
os.system("audio.mp3")

For more information on gTTS please visit https://pypi.org/project/gTTS/

有关gTTS的更多信息,请访问https://pypi.org/project/gTTS/

Comment below if you have queries regarding python text to speech conversion.

如果您有关于python文本到语音转换的查询,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2018/05/python-text-to-speech.html

python语音转文字

python语音转文字_Python文字转语音示例相关推荐

  1. python语音播报天气预报_python实现智能语音天气预报

    python编写的语音天气预报 本系统主要包括四个函数: 1.获取天气数据 1.输入要查询天气的城市 2.利用urllib模块向中华万年历天气api接口请求天气数据 3.利用gzip解压获取到的数据, ...

  2. python 文字转语音 带情感_Python文字转换语音,让你的文字会「说话」抠脚大汉秒变撒娇萌妹...

    APP 也有文字转换为语音的功能,虽然听起来很别扭,但是基本能解决长辈们看不清文字或者眼睛疲劳,通过文字转换为语音来获取信息. 我们用 Python 能否实现文字转语音呢,可以的,百度有个语音接口,可 ...

  3. python 文字转语音 带情感_python文字转语音实现过程解析

    这篇文章主要介绍了python文字转语音实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 使用百度接口 接口地址https://ai.ba ...

  4. python发微信语音没声音_Python文字转换语音,抠脚大汉秒变撒娇萌妹

    APP 也有文字转换为语音的功能,虽然听起来很别扭,但是基本能解决长辈们看不清文字或者眼睛疲劳,通过文字转换为语音来获取信息. 我们用 Python 能否实现文字转语音呢,可以的,百度有个语音接口,可 ...

  5. python离线语音转文本_Python实现自动化语音转文本,彻底解放你的双手

    当对一个或多个人的谈话进行记录时,采用一种高度准确和自动化的方式将口语提取为文本非常有用.转换成文字后,便可以将其用于进一步分析或用作其他功能. 我们将使用称为AssemblyAI(https://w ...

  6. python录音文件降噪_Python谱减法语音降噪实例

    代码中用到了nextpow2,其中n = nextpow2(x) 表示最接近x的2的n次幂. #!/usr/bin/env python import numpy as np import wave ...

  7. python编写格斗游戏_Python文字小游戏:搏击比赛

    基础语法课时的练手作业 import time import random while True: player_victory = 0 enemy_victory = 0 for i in rang ...

  8. python实时数据流设计_Python读取实时数据流示例

    1.#coding:utf-8chose=[('foo',1,2),('bar','hello'),('foo',3,4)]defdo_foo(x,y):print('foo',x,y)defdo_b ...

  9. python 批量下载图片_Python 批量下载图片示例

    使用Python find函数和urllib下载图片. A:#!/usr/bin/env python import time import urllib i = 0 url = ['']*10 na ...

最新文章

  1. mongodb和python交互
  2. python3 异步 asyncio get_event_loop new_event_loop 使用
  3. 对大量转载贴识别算法的研究
  4. 关于gcc -o 的使用问题
  5. java遍历日期_Java遍历起止日期中间的所有日期操作
  6. python json是什么_python的json用法
  7. 面向对象编程(六):数据封装
  8. 第一章 批判性思维概念
  9. 只要还在路上前行着的,那都是一个个闪闪发光惹人爱的人啊
  10. 东莞软件开发公司有哪些【怎么样】
  11. Android手势密码解锁
  12. 盒模型——快递比喻法
  13. 如何培养自己的编程兴趣
  14. SQL “varchar转换为numeric时出错” 的小插曲
  15. 电脑回收站清空了怎么恢复回来
  16. ERP术语 英文对照(部分)(参考)
  17. 陌生人社交的一路羁绊——垃圾用户
  18. 如何优雅判断属性值为空
  19. 中国电信发军医疗器械领域
  20. mysql导入dataworks_使用DataWorks数据集成从MySQL导入数据到GDB

热门文章

  1. 易中天品汉代风云人物08:韩信成败之谜
  2. 2019.03.16【NOIP提高组】模拟 B 组 电费结算(electric)
  3. springboot毕设项目养老平台的设计与实现u8sua(java+VUE+Mybatis+Maven+Mysql)
  4. 试解PTA《20211122-函数基础练习》,涉及 高精度加法 和 10进制转换成2-16进制问题,有需要的同学来看看吧;
  5. 解决xp系统进不了安全模式的问题
  6. C++基础—缺省参数
  7. 百度前端笔试题 css3画三角形
  8. 解决您未被授权查看该页; HTTP 错误 401.1 - 未经授权:访问由于凭据无效被拒绝
  9. 80后的千万富翁曝光
  10. DateDiff函数的使用