We can make the computer speak with Python.
Given a text string, it will speak the written words in the English language.
This process is called Text To Speech (TTS).
Related Course: The Complete Machine Learning Course with Python
Text to speech
Pyttsx text to speech
Pytsx is a cross-platform text-to-speech wrapper.
It uses different speech engines based on your operating system:
sapi5 - SAPI5 on Windows XP, Windows Vista, and (untested) Windows 7
espeak - eSpeak on any distro / platform that can host the shared library (e.g., Ubuntu / Fedora Linux)
Install with pip (using pyenv, pipenv or virtualenv):
sudo pip install pyttsx
Then run the example code:
import pyttsx |
gTTS text to speech
gTTS is a module and command line utility to save spoken text to mp3.
It uses the Google Text to Speech (TTS) API.
Listen to the voice sample below:
Related Course: The Complete Machine Learning Course with Python
This module supports many languages and sounds very natural.
Install
Install with the python package tool (pip):
sudo pip install gTTS
Examplefrom gtts import gTTS
import os
tts = gTTS(text='Good morning', lang='en')
tts.save("good.mp3")
os.system("mpg321 good.mp3")
If you want to test it on the command line use:
gtts-cli.py "Hello" -l 'en' -o hello.mp3
iOS TTS and speech recognition
TTS in Pythonista for iOS:import speech
speech.say('Hola mundo', 'es_ES')
To record sound:import sound
r = sound.Recorder('audio.m4a')
r.record(3) # seconds
To recognize it as text:text = speech.recognize('audio.m4a', 'en')[0][0] # sent to Apple servers
Microsoft speech engine
If you use Microsoft Windows 10, it has a speech engine included.
Install the module win32com, then you can use this code:import win32com.client as wincl
speak = wincl.Dispatch("SAPI.SpVoice")
speak.Speak("Hello World")
IBM Watson TTS
IBM has created an tts API, which is free for a limited amount of queries.
You can listen to sample data on the Watson TTS page.
You can use the tts-watson module to interact.
pip install tts-watson
After registrating on the IBM watson site (generate keys there),
we can write our code:
from tts_watson.TtsWatson import TtsWatson
ttsWatson = TtsWatson('watson_user', 'watson_password', 'en-US_AllisonVoice')
ttsWatson.play("Hello World")
Alternatively you can use curl to directly fetch from the API.
Thank you so much. I tried several other translate commands and nothing worked till i landed on your page. Your tutorial worked perfectly. Many Thanks again!!
I think in your gTTS example you meant:
...
os.system("mpg123 good.mp3")
Sorry, I didn't realize there was a clone called mpg321. :)
how to speak in other language? let say Chinese. Any ideas?
IGNORE, I have found some thoughts
It keeps saying: "ImportError: No module named pyttsx". Any thoughts?
The module pyttsx is not installed on your system.
Install it with pip or from https://pypi.python.org/pypi/pyttsx
Thanks because the "pip install" command has not worked so far.
You can install it manually or using pip/easy_install.
setuptools method
You can install it with setuptools
<div class="terminal">
git clone https://github.com/RapidWar...
cd pyttsx
sudo python setup.py install
</div>
pip method
Do you have pip installed on the computer? You can install it with get-pip.py
https://pip.pypa.io/en/stable/installing/#do-i-need-to-install-pip
This may help:
https://pyttsx.readthedocs.io/en/latest/install.html
try
pip install --upgrade pip
than
pip3 install pyttsx3
that should do the job
How to do voice modulation in that output audio
using another python module, this one is only for speech recognition