Sophie_LMR.zip (6745Bytes)
- BitVoicer - Speech Recognition (this is where the $5 comes in)
- Python 2.7 - Coding language
- Eclipse, Geany or your prefered interface for coding on Python
- Virtual COM Port
Optional:
- Ivona Voices - More natural sounding text-to-speech voices for Windows
Procedure:
First download Python (for this tutorial I am going to use version 2.7.3) which can be downloaded from https://www.python.org/download/releases/2.7.3
Install it into C:\Python27 for convinience sake.
Next thing you need to do is download and install eclipse which is an interface for writing your code via Python. You can download it from http://www.eclipse.org/downloads/
Install it into C:\eclipse
If you get this error while running it...
Go to the first answer in http://stackoverflow.com/questions/2030434/eclipse-no-java-jre-jdk-no-virtual-machine
www.stackoverflow.com is the best place to find help for any issues in python
Use this tutorial to install python into eclipse.
Alternatively use this tutorial to install python into Geany.
There are something called 'libraries' that you will need to access the different functionalities of python. This case we want a way for the computer to speak (text-to-speech). This is done using a library called pyttsx. Now lets go ahead and install this library. For those of you who already know how to install libraries just go ahead to the code.
Installing pyttsx (use the hyper link for documentation)
The first thing pyttsx needs is a pywin32 windows package which should be downloaded from here.
Next, install pip which is a command line installer for python (don't worry about it now). Download pip from here and paste it to your Python27 directory.
Now go to C: drive and while holding Shift key right click on the "Python27" folder. And choose 'Open command window here'
Next type 'python get-pip.py' and you should get something like this:
Now in C:\Python27 open command line as before, open command line for the folder "Scripts" and enter "pip install pyttsx"
If everything installed correctly, you should be able to make your computer speak. Let's now try this.
Create a new file called myVPA.py or anything that you prefer. Don't forget the '.py' at the end, because that is what makes Eclipse recoginize it as a python file.
Type this in it, turn your volume up and click on Run (accept any dialogue boxes that follow):
import pyttsx # text-to-speech library
engine = pyttsx.init() # initiates speech engine
engine.say('Hello World') # loads text into engine
engine.runAndWait() # THIS LINE IS IMPORTANT, runs the speech engine
If you hear your computer say "Hello World", then wohla we are a third of the way there.
Once you are done having fun making your computer (which I am going to call Sophie) say all sorts of stuff, let us move to actually being able to have a conversation with her.
This is where we will have to buy and setup BitVoicer, yeah you just have to trust me with your $5 on this. Until I can find an alternative to a free speech-to-text software, I'm just gonna suggest BitVoicer. You could alternatively use BitVoicer to command your arduino like I have shown in the video.
Once you finish installing BitVoicer, we need to use a Virtual Serial Port Emulator.
Why? Because BitVoicer was originally created to be used in conjuction with Arduino communicating over the Serial Port and we will have to make BitVoicer 'think' that it is still talking to an arduino over the serial port.
For now we can use this one: http://www.eltima.com/products/vspdxp/
Once you have installed this (trial version will do for now), open it and do the following.
Click on Add Pair (make sure you are not using there COM ports prior to this, you can check this by going to Device Manager and clicking on ports)
Next, like before we need to install another library to Python that lets it communicate with Serial/COM ports.
Type this: pip install pyserial
Lets setup BitVoicer now.
Go to File >> Preferences and put in the following values:
I think this is a good place to say that you could install additional languages or accents by going to File >> Additional Languages to better suit your voice. I changed mine to English - India just to make the speech recognition more accurate. Create a new Voice Schema (File >> New).
Then “Add New Sentence” twice and set it up EXACTLY like this:
If this picture is not legible, the command for “Who are you” is ‘who’ and “Do you like cookies” is ‘lik’ respectively.
Now we are ready to code on Python and get this thing working.
In your myVPA.py file write the following lines of code:
import pyttsx # text-to-speech library
import serial # serial communication library
while True: # creates a loop so that you can keep talking
ser = serial.Serial(‘COM8’,9600) # opens COM port 8 with baud rate of 9600
raw_data = ser.read(7) # reads the first 7 characters coming from the port
msg = str(raw_data[3:6]) # extracts 3rd to 6th characters from the array
# Note: The first three characters coming from BitVoicer are junk values
# for some reason I cannot understand, hence I just use values
# starting from the third character, you can increase the range of values
# as per your liking, but for the first time, just stick to this code
print msg # debugging line to check input
ser.close() # closes the serial port so as to reset the input commands
if(msg == ‘who’):
engine = pyttsx.init()
engine.say(‘Hi there. I am Sophie, your own Virtual Personal Assistant’)
engine.runAndWait()
if(msg == ‘lik’):
engine = pyttsx.init()
engine.say(‘Ofcourse I do! What do you think I am? A computer?’)
engine.runAndWait()
Now when you speak “Who are are?” clearly enough, your computer should respond with “Hi there. I am Sophie, your own Virtual Personal Assistant” or if you say “Do you like cookies?”, she should say "Ofcourse I do…"
Note: You can check the console of python to see what commands and being received and alternatively you can check the ‘Activity’ box to see what commands are being sent from BitVoicer
If this worked well, congrats!!! You have made it through this tutorial and are well on your way to upgrade Sophie or whatever you would choose to call it and make it do what ever you want.
An optional thing you can do here is install a voice pack from Ivona (use the trial version for now, install the voice you think suits you (I like to switch between Salli and Raveena).
You can change voices by going to Start >> ‘Change text to speech settings’ and doing this:
Apply changes and try your code again. Always remember, in python, terminate the current program before coding and executing your program.
The uploaded files needs to be used with each other but you will need to install the corresponding libaries by yourself, and also check the COM port settings.
The reason I wrote this tutorial is because I loved the AI in the movie "Her", but to be able to make an AI as powerful and efficient as that, there would require a lot of research and thousands of lines of code. I just wanted to motivate you to be one of those people to create a breakthrough in this field of an Artificial Personal Assistant.
If you have any questions or suggestions or even your own cool updates, just post it as a comment and I will try to get back to you ASAP.
by Adith Jagadish Boloor