Categories
Python Robotics ROS

TensorFlow / Keras Model Predict Error

Unexpected error: Tensor Tensor("dense_1/BiasAdd:0", shape=(?, 3), dtype=float32) is not an element of this graph.

To Fix:

Make sure to include the following global:

import tensorflow as tf

global graph,model
graph = tf.get_default_graph()

And surround the prediction as such:

with graph.as_default():
                segment = self.model.predict(features)
Categories
Blog Posts Programming Python

Python – Making Use of Google’s Text To Speech Translation Tool

Text to speech (tts) is a difficult task to get right and there are quite a few packages that show just how hard with their terrible tinny voices, these are also usually only limited to English, which for the larger part than most people would like to believe don’t find it very useful. Nevermind, Google to the rescue, their Translation tool has had the ability to also voice translations for a while now. So wouldn’t it be awesome if we could utilize this, stable, constantly developing and ultimately free utlilty? Yes? Well luckly for you it’s possible. It’s been done before with the use of JavaScript, like here but those more used to scripting and desktop programming don’t worry, you can use it too.

Basically it works by sending a request to the Google translate servers which then, pretty much instantly, reply with an MP3 file which contains the requested words in their most sexy of robot voices.

Check this URL for an example of said voice – http://translate.google.com/translate_tts?q=check+out+my+sexy+robot+voice (if it doesn’t load in your browser, open VLC, CTRL+N and paste the URL in)

First, we want to be able to get python to stream an MP3 file off the web:

More info on this can be found here, the following code was is a stripped down version of the code found at codeboje.de.

[gist][/gist]

Now we want to make a string to send :

We’re going to use command line arguments as the string which wants to be read by the Google TTS engine, the following code will grab those arguments and concatenate them into a string. It also has to be in a format like a usual Google URL, these usually replace white space for the ‘+’ sign, that’s easy to do.

[gist][/gist]

Now let’s just pass this URL to the bit we wrote to stream an MP3 and watch it go:

Full Source Code:

(Uses command line arguments as it’s input, run like ‘python ttsString.py Hello World‘)
[gist][/gist]

That’s pretty much how easy it is to use Google’s free online TTS engine, one main thing to watch out for is the 100 character limit to the use of this service in this way along with the API call limits which apply to each IP address. There are loads of cool things this can be used for and I’d love to see what anyone comes up with, keep me posted and have fun!

Categories
Blog Posts Programming Python

Python – Writing a ‘Fuzzy Clock’

If you want a clock that’s a little more human, there’s no better way than a ‘Fuzzy’ clock, essentially this converts the time in to a more brain friendly format such as “Twenty past Twelve” rather than 12:22.

Writing a simple fuzzy clock
Source Code:

[gist][/gist]

Output:

$ python fuzzytime.py
It’s 25 to 2

Writing a more readable fuzzy clock:

Now, this returns in a form of “It’s 20 past 1” rather than “It’s twenty past one”, if you’re going to want this then it’s only a little more tricky. For this I used a lookup table which would essentially replace the numbers for the written numbers.

Source Code:

[gist][/gist]

Output:

$ python fuzzytime.py
It’s two o’clock

Categories
Linux Posts Web

Downloading all your Facebook Photos for Google Plus – In Linux

Okay so, with Google Plus just being released to a select few (and invites) you might be wanting to export all your Facebook photos, tagged and albums you’ve uploaded to Google+. There are several ways of going about this, a few webapps, apps and through Facebook itself, the problem is that most of these don’t work in Linux making it a little more tricky to do without switching to Windows or OSX or borrowing some ones laptop for a while.

Through Facebook Route:

This way is actually pretty easy, which came as a surprise to me since I would have though it be in Facebooks best interest to keep users there just because it’s too much hassle to change.

First head over to Facebook.com, login and make your way through the labrynth that is Facebook settings to
‘Account Settings’ > Next to ‘Download Your Information’ click ‘Learn more’ > click ‘Download’.

This way you’re going to have to wait for Facebook to process all your data, they’ll then send you an e-mail once it’s done to a download link. This is a compressed (ZIP) file which contains all your information from Facebook, in the Photos folder you’ll find all your pictures which can then be uploaded to Google Plus.

The problem with this way is that it can take a reasonable amount of time (a couple of days depending on how much information you have..) and I’ve had it fail on a couple of occasions.

Alternatively you can use the following:

The PhotoGrabber Option:

For this you’ll need a couple of dependencies installed, it’s a desktop app that downloads the files for you.

Install dependencies:

sudo apt-get install python python-tk

Install PhotoGrabber:

svn checkout http://photograbber.googlecode.com/svn/trunk/ photograbber-read-only
cd photograbber-read-only/
./pg.py

Once you’ve ran all that and the ./pg.py bit it should load up with the following:

Click Login and then proceed to login and allow PhotoGrabber to do its thing, once you’ve done that you’ll get to a screen similar to below with a code:

Copy and paste that into the PhotoGrabber window (you’ll need to use Ctrl+V no right click to paste) and then it’ll let you choose which photos to take. Once you’ve picked, hit Download and watch them download to the folder you specify. There’ll be a lot folders, one for each place you were tagged. Once it’s done you can upload them however you want to Google Plus otherwise just keep them.

Categories
Arch Linux HowTo Linux Posts

Failed to build Planner-0.14.4 in Arch Linux

When trying to install Gnome Planner ( http://live.gnome.org/Planner ) in Arch Linux, I came across this error:

Error:

/usr/bin/pygobject-codegen-2.0: line 11: /usr/bin/python2: No such file or directory
make[2]: *** [planner.c] Error 127
make[2]: Leaving directory `/home/alex/Desktop/planner-0.14.4/python'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/home/alex/Desktop/planner-0.14.4'
make: *** [all] Error 2

To fix this, I made a symbolic link from /usr/bin/python2.7 to /usr/bin/python2, this should work with other similar errors involving missing python2 file.

sudo ln -s /usr/bin/python2.7 /usr/bin/python2

Once you have made the made the symbolic link continue to make and install the program as usual.

Categories
Linux Posts Python Ubuntu

ImportError: No module named …

Numeric

Error:

Traceback (most recent call last):
  File "", line 1, in
ImportError: No module named Numeric

Package:

sudo apt-get install python-numeric

ImageTk

Error:

ITraceback (most recent call last):
  File "", line 1, in
ImportError: No module named ImageTk

Package:

sudo apt-get install python-imaging-tk
Categories
Linux Posts Ubuntu

How To Fix: fatal error: Python.h: No such file or directory

 (cd ./brain/psom && make)
make[1]: Entering directory `/home/ubuntu/pyrobot/brain/psom'
(cd csom_src && make _csom.so)
make[2]: Entering directory `/home/ubuntu/pyrobot/brain/psom/csom_src'
gcc -c -g -I -I/usr/include/python2.6 som_pak_wrap.c -I/usr/include/python2.6 -Isom_pak-dev
som_pak_wrap.c:125: fatal error: Python.h: No such file or directory
compilation terminated.
make[2]: *** [som_pak_wrap.o] Error 1
make[2]: Leaving directory `/home/ubuntu/pyrobot/brain/psom/csom_src'
make[1]: *** [csom_src/_csom.so] Error 2
make[1]: Leaving directory `/home/ubuntu/pyrobot/brain/psom'
make: *** [brain/psom] Error 2

To fix

 sudo apt-get install python2.6-dev
Categories
Command Line Linux Posts Ubuntu

How To: Install Pyrobot in Ubuntu

A quick guide for getting pyrobot running in Ubuntu, I’ve managed to get it running on 32bit and 64bit Ubuntu (10.10) so follow the guide accordingly to whichever architecture you’re using (64bit is about half way down the post).

32-bit

1. Download this file, or copy the below into a file and save it as pyroinstall.sh to your home dir.

#!/bin/sh

currentdir=$(pwd)

sudo apt-get install build-essential python2.6 python2.6-dev python-tk python-numeric libjpeg62-dev libncurses5-dev swig

wget http://pyrorobotics.org/download/pyrobot-latest.tgz

tar -zxvf pyrobot-latest.tgz

cd pyrobot

python configure.py

make

sed -ie 's/-e #!/#!/g' $currentdir/pyrobot/bin/pyrobot
sed -ie 's/-e # /# /g' $currentdir/pyrobot/system/version.py

echo 'export PATH=${PATH}:'$currentdir'/pyrobot/bin' >> $currentdir/.bashrc

Note: The ‘ in the code highlighter I have above won’t work in terminal, there isn’t much I can do about it so you’ll have to re-write it. Also if you’re doing this manually change the “$currentdir” to “~” if you are doing it in your home directory.

2. Make sure this file is in your home directory (e.g /home/alex/pyroinstall.sh) and make it executable:

 chmod +x pyroinstall.sh

3. Run the usual way (as super user because it has to install stuff etc):

 sudo ./pyroinstall.sh

4. It’ll ask you some questions, below are the answers I used – you can use other configurations if you know what you need. I also put the full output in pastebin ’cause it was bunging up the guide – http://pastebin.com/Kc5jyhja

1. 	2.6
2. 	/usr/include/python2.6
3. 	/usr/bin/python2.6
4. 	/etc/X11
5. 	none
6.01	n
6.02	n
6.03	n
6.04	n
6.05	n
6.06	n
6.07	n
6.08	n
6.09	n
6.10	n
6.11	y
6.12	y

5. Once it’s done reboot or run:

 source ~/.bashrc

6. Allow read/write/execute permissions for the pyrobot folder:

 sudo chmod -R 777 ~/pyrobot

You should all be done, try typing the following to get it running!

 pyrobot

64-bit

In order to get pyrobot running on 64bit, you basically need to add -fPIC to CFLAGS in all the relative Makefiles as explained in this mailing list post – http://www.mail-archive.com/pyro-users@pyrorobotics.org/msg00344.html – Lucky for you I’ve already gone through the effort of doing it and compressed it (download here you won’t need to if you are going to run the script though, it’ll do it for you..).

This script should download the file, configure and make and output the path to the .bashrc file for you (similar to the 32bit one does) so download this file, or copy and paste the script below into a file in your home directory:

#!/bin/sh

currentdir=$(pwd)

sudo apt-get install build-essential python2.6 python2.6-dev python-tk python-numeric libjpeg62-dev libncurses5-dev swig

wget http://dl.dropbox.com/u/307455/pyrobot-5.0.0_64bit.tar

tar -zxvf pyrobot-5.0.0_64bit.tar

cd pyrobot

python configure.py

make

echo 'export PATH=${PATH}:'$currentdir'/pyrobot/bin' >> $currentdir/.bashrc

2. Make sure this file is in your home directory (e.g /home/alex/pyroinstall64.sh) and make it executable:

 chmod +x pyroinstall64.sh

3. Run the usual way (as super user because it has to install stuff etc):

 sudo ./pyroinstall64.sh

Steps 4. 5. and 6. are the same as above..

Change Default Editor

If you want to change the default editor from emacs to anything else (this will change the default editor used in other terminal applications too..) use the following commands, just change gedit to whatever you’d like (vi, vim, kedit etc):

echo "export EDITOR=/usr/bin/gedit" >> ~/.bashrc
source ~/.bashrc

Any problems leave a comment and I’ll try and help as best as I can..