In our previous blog Python Essentials for Voice Control: Part 1 we have discussed Speech Recognition , which is the first step towards Voice Control. In this blog we are going to enhance our skills to develop a Personal Voice Assistant.
Let us move ahead now..
Communication needs a medium for representing thoughts which can be in any form like Verbal or Non - Verbal. We can express thoughts by speaking or writing. We can communicate with computers through speaking using Speech Recognition till now. But it is like one way communication , where the computer can listen to us but to make it in action it needs to express itself using Text , Speech or Actions.
Artificial Intelligence working towards putting responses about feelings and expression but not getting 100% accuracy till now.
Now, we will express thoughts using Text , where we will convert Text into Speech in Real Time.
Text to Speech Conversion makes us understand the response of the computer in the form of Text. pyttsx3 library of Python will help us to achieve the Goal.
pip install pyttsx3
Install pyttsx3 library using CMD.
It is a class of pyttsx3 library. It contains several methods which helps us to convert text into speech. It initialises an Engine instance to convert Text to Speech.
init(driverName=None , debug=False)
Parameter driverName contains the specific name of the driver which we want to use to create Engine instances for Text to Speech Conversion. By default it will use the driver which is available in the Operating system.
Parameter debug used to give debug output enabled or not. It is of Boolean Type and by default it is False.
Drivers : By default available Drivers Operating System based are :
sapi5 : SAPI5 on Windows
nsss : NSSpeechSynthesizer on Mac OS X
espeak : eSpeak on every other platform
import pyttsx3
engine = pyttsx3.init()
Like this we can initialize the Engine for Text to Speech Conversion.
It is a function of class init(). Using it, we can get the current value of a Property to modify accordingly. In this case Property is an attribute or Characteristic of an Object.
For Example: Voice Attributes can be defined like Voice of Male or Female, Speed of Voice, Volume of Voice etc.
getProperty(name)
Parameter name is defined as the String ID or name of Property which we want to modify.
It is a function of class init(). Using it, we can assign the values to the Properties of Objects. Attribute selection is done by getProperty() function while values assigning to the attribute is done by setProperty().
setProperty(name, value)
voices = engine.getProperty('voices')
engine.setProperty('voice',voices[1].id)
engine.setProperty('rate', 125 )
engine.setProperty('volume',1.0)
Code tells us that we have selected the voices as the property to modify using getProperty() function.
We assign the values to the attributes of the voices Property using setProperty() function:
voice : voice id , ( 0 for Male Voice ) and (1 for Female Voice).
rate : Speed of speaking (int type)
volume : Level of Volume (float type)
It is a function of class init(). It is used to operate a computer to speak something as output to the user. It occurs just after a Preoperative is finished. It continues in a queue.
say(text , name=None)
It is a function of class init() to manage Engine calls. It is used to run an event loop until all commands queued up until a method call completes in which we use this function. It Blocks all other next steps in the waiting area till the method call completes its operations.
def speak(text):
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice',voices[1].id)
engine.setProperty('rate', 125 )
engine.setProperty('volume',1.0)
engine.say(text)
engine.runAndWait()
It is used to register a callback for notification on the given topic.
connect(topic, cb)
Valid Topics of callback for a method are conditions according to which keyword of topic should be occur:
Started-Utterance :
Method should be fired when the engine begins speaking an utterance.
onStartUtterance(name)
Started-Word :
Method should be fired when the engine begins speaking a word.
onStartWord(name, location, length)
Finished-Utterance:
Method should be fired when the engine ends speaking an utterance.
onFinishUtterance(name, completed)
Error:
Method should be fired when the engine throws an error.
onError(name, exception)
def onStart(name):
print 'starting', name
def onWord(name, location, length):
print 'word', name, location, length
def onEnd(name, completed):
print 'finishing', name, completed
engine = pyttsx3.init()
engine.connect('started-utterance', onStart)
engine.connect('started-word', onWord)
engine.connect('finished-utterance', onEnd)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()
It is used to unregister a notification callback.
disconnect(token)
Python is healthy in almost every aspect of development in Technology. We can create wonderful applications in a small amount of codes. It gives us interesting implementations as well as industrial programs for projects.
Our thoughts can directly sync with the programs to develop something amazing. It is wonderful to make computers speak to us.
Using pyttsx3 we can develop many projects for the community. We can contribute to the luxury living of people and utilise time as well as resources in a healthier way.
We can make a computer to read a book for us using pyttsx3, which is like Amazon Audible, Audio Book Service.
5 Factors Influencing Consumer Behavior
READ MOREElasticity of Demand and its Types
READ MOREAn Overview of Descriptive Analysis
READ MOREWhat is PESTLE Analysis? Everything you need to know about it
READ MOREWhat is Managerial Economics? Definition, Types, Nature, Principles, and Scope
READ MORE5 Factors Affecting the Price Elasticity of Demand (PED)
READ MORE6 Major Branches of Artificial Intelligence (AI)
READ MOREScope of Managerial Economics
READ MOREDijkstra’s Algorithm: The Shortest Path Algorithm
READ MOREDifferent Types of Research Methods
READ MORE
Latest Comments