python logo


Category: uncategorized

save dictionary python

python string methods

python find in array

python pause for time

Speech engines with python tutorial

Text To Speech (TTS) Text To Speech (TTS)

A computer system used to create artificial speech is called a speech synthesizer, and can be implemented in software or hardware products.

A text-to-speech (TTS) system converts normal language text into speech. How can we use speech synthesis in Python?

Related courses:

Pyttsx


Pyttsx is a cross-platform speech (Mac OSX, Windows, and Linux) library. You can set voice metadata such as age, gender, id, language and name. Thee speech engine comes with a large amount of voices.

Text to speech sample:
 

Install with:


sudo pip install pyttsx

Create the code speech1.py


import pyttsx
engine = pyttsx.init()
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

And execute it with python.

Espeak


eSpeak is a compact open source software speech synthesizer for English and other languages, for Linux and Windows.

Text to speech sample:
 

We can install using:


sudo apt-get install espeak

Create the code speech2.py:


import os
os.system("espeak 'The quick brown fox'")

It is very easy to use, but like pyttsx it sounds very robotic.

gTTS


The gtts module no longer works.

I found a script on Github that uses the Google speech engine. The script comes with many options and does not speak, instead it saves to an mp3. We added a command to play the mp3 automatically:


os.system("mpg321 out.mp3 -quiet")

Run with:


python gtts.py -s 'Python programming example'

The voice is extremely natural. The only disadvantage is that you need to be connected with the Internet when running this script.

Links


You might like:

string methods python

Methods can be applied to Python strings, where a string is somet text between quotes. An example of using the string method replace, which replaces the a sub-string in a string:


s = "Hello World"
s = s.replace("World","Python")
print(s)

Related Course:
Python Programming Bootcamp: Go from zero to hero

Overview
Python includes these useful methods for string manipulation:



python math operators

Overview of Python IDEs and Code Editors

Install a Python IDE


An Integrated Desktop Environment (IDE) is a software application for programming. In addition to simple text editing they have all kind of features such as syntax highlighting, code completion, tabs, a class browser and many more.


Online Python Interpreters


The online interpreters may not work for everything but will work for most of the beginner tutorials. I recommend using a desktop IDE or the official Python interpreter.


Overview of IDEs (You only need one)

Method with description
capitalize(str)
Returns first letter capitalized string.
count(str)
Count the frequency of the word str. It can also be applied to words: s.count("Hello")
find(str)
Find the position of the first occurence.
index(str)
Find the position of the first occurence, raise exception if not found.
isdecimal()
Returns true if string is decimal.
isdigit()
Returns true if string contains digits only.
isnumeric()
Returns true if string contains numeric only.
lower()
Returns string in lowercase.
len(str)
Returns the length of str.
upper()
Returns string in uppercase.
replace(old, new)
Replaces the first argument with the second argument.

jarvis python code

I thought it would be cool to create a personal assistant in Python. If you are into movies you may have heard of Jarvis, an A.I. based character in the Iron Man films. In this tutorial we will create a robot.

In this tutorial you get started with coding your own Jarvis, the voice activated assistant in Iron Man. Jarvis is a voice assistant, similar to Apple’s Siri or Google Now. In this tutorial we use the power of the Python programming language and a text-to-speech service.

I’m going to be using Ubuntu Linux for this project but you should be able to use it in Windows or Mac as well. However, since there’s quite a bit of command line work required, I’d recommend doing this on a Linux machine.

The features I want to have are:

For this tutorial you will need (Ubuntu) Linux, Python and a working microphone.

Related course:

Video


This is what you’ll create (watch the whole video, demo at the end):

Recognize spoken voice


Speech recognition can by done using the Python SpeechRecognition module. We make use of the Google Speech API because of it’s great quality.

Answer in spoken voice (Text To Speech)


Various APIs and programs are available for text to speech applications. Espeak and pyttsx work out of the box but sound very robotic. We decided to go with the Google Text To Speech API, gTTS.

IDE Author Platform Description Price Download
PyCharm Jetbrains Windows, Mac OS X, Linux/UNIX Python IDE. Features including: code completion, code inspections, on-the-fly error highlighting and quick-fixes € 89 / 1st year.($ 97.90) Download PyCharm 
Atom (+script plugin) GitHub Windows, Mac OS X, Linux/UNIX Python IDE. You need to download the script plugin after installing Atom. Free. Download Atom.
Pythonista omz:software Apple iOS (iPhone, iPad) Features include: Syntax highlighting, code completion, interactive prompt, standard and iOS modules. € 9. ($ 9.90) Download Pythonista.
Eclipse with PyDev Aleks Totic Windows, Mac OS X, Linux/UNIX Features include: Syntax highlighting, code refactoring, graphical debugging and more. Free Download 
Eric Python IDE Detlev Offenbach Windows, Linux/UNIX Features include: Syntax highlighting, autocompletion, class browser and more. Free Download 
Wing IDE Wingware Windows, Mac OS X, Linux/UNIX Features: Syntax highlighting, auto completion, refactoring, unit testing and version control. $45 to $245 per user / license. Download 
Komodo IDE Komodo Windows, Mac OS X, Linux/UNIX Features: Syntax highlighting, documentation browser, run code in-line, quick bookmarks and more. € 40 to € 223. ($99 to $295). Download
Repl.it Amjad Masad, Haya Odeh, Faris Masad and Max Shawabkeh. Web Python interpreter Free Run Online 
Ideone Ideone Web Python interpreter Free Run Online 
Codepad Steven Hazel Web Python interpreter Free Run Online 

sudo pip install gTTS

Using it is as simple as:


from gtts import gTTS
import os
tts = gTTS(text='Hello World', lang='en')
tts.save("hello.mp3")
os.system("mpg321 hello.mp3")

gtts

Complete program


The program below will answer spoken questions.


#!/usr/bin/env python3
# Requires PyAudio and PySpeech.

import speech_recognition as sr
from time import ctime
import time
import os
from gtts import gTTS

def speak(audioString):
print(audioString)
tts = gTTS(text=audioString, lang='en')
tts.save("audio.mp3")
os.system("mpg321 audio.mp3")

def recordAudio():
# Record Audio
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)

# Speech recognition using Google Speech Recognition
data = ""
try:
# Uses the default API key
# To use another API key: `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
data = r.recognize_google(audio)
print("You said: " + data)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))

return data

def jarvis(data):
if "how are you" in data:
speak("I am fine")

if "what time is it" in data:
speak(ctime())

if "where is" in data:
data = data.split(" ")
location = data[2]
speak("Hold on Frank, I will show you where " + location + " is.")
os.system("chromium-browser https://www.google.nl/maps/place/" + location + "/&")

# initialization
time.sleep(2)
speak("Hi Frank, what can I do for you?")
while 1:
data = recordAudio()
jarvis(data)


Related posts:


 

gui in python

To create a graphical interface (GUI) in Python you need to make use of a library or module.  There are at least three widely used modules for creating GUIs with Python:

While you can create a graphical user interface (GUI) with any of these modules, they do not have the same features.

Related course:

GUI toolkits


Tk provides basic widgets such as a button, menu, text and label. It is very limited compared to QT4 and WxPython but it is also the oldest module.  It runs on most versions of Mac OS, Linux and Windows.

TkMessage box a messagebox in Tk. GUIs made in Tk do not have a native look.

QT4 and QT5 are developed by the Qt company.  Graphical applications made with QT4 or QT5 run on Windows, Linux/X11 and Mac OS X.  It has tons of widgets including tabs, buttons, item views, table views, progressbars, input fields, calendar views and many more. Compared to Tk it has a lot more widgets available.

Graphical application using PyQt5

WxPython is a module that creates a native GUI look regardless of the operating system used. On Windows it will look as windows application while on Mac OS it will look as a Mac application. This can be a clear advantage compared to QT4 or Tk, depending on your purpose. WxWidgets has many widgets available such as buttons, menu, text but also more advanced widgets as a htmlview or tree control.

wxTabs A window created with wxPython. wxPython has native appearance on all operating systems.