Saturday, 20 December 2014

No superpowers? Try programming...

My son is five and he wants a couple of things these Xmas from Santa. The first option is a flying broom, so that he can play quidditch. Not easy. The second option is a real lightsaber, so that he can fight like a real Jedi Knight. He already has one made of plastic, but apparently it doesn´t make the cut (pun intended!). Not easy either...

I have been thinking about it and came to the obvious conclusion that Santa´s job is quite difficult these days. It is not only the insane single milestone planning (who was the programme manager?). It is also the crazy requests of his customers. So I have decided to give Santa a hand this year and see what I can think of. Flying and fighting are both rather dangerous activities for children, so instead of the real thing, he will just have to make do with a similar experience. 

Many will have noticed the "Hour of Code" campaign this year. One of the videos actually says that being able to program is the closest thing we have to superpowers (min 4:58). Well, that´s what I need! Therefore the solution is relatively simple, write a program that makes him think he is flying with a broom or fighting as a Jedi Knight. Easy peasy? Not quite, but not impossible.

The idea was to create a program that streams video from the webcam to the screen, tracking the plastic lightsaber and overlaying a light on it. The result may not be professional, but the effects is still kind of nice to see in real time. In order to do this I have used cv2, yet another great library available in Python. After a bit of googling, I noticed that all I have to do is to convert the RGB frames to HSV, threshold the image to single out the area with the selected colour and create a mask which can later be coloured whichever way I want. After a couple of nights fighting with developing the code I got a working example today. I was so proud that I showed my daughter immediately to get some feedback. Her comment was, "Dad, lightsabers are not white. They are green, or blue. Or red if you are one of the baddies. But not white!". D'oh! This younger generation is definitely spoiled when it comes to expectations... 

But here we are, a working copy of the program (simple colour, no glow, sorry) to play with. From here, creating a chroma key background and tweaking the code to let him think he is flying on a broom should be a breeze! And not a minute too late.

Special thanks to the people maintaing and contributing to and It´s always great to stand on the shoulders of giants.

In order to get the thresholds correctly, I created a calibration program to play with the HSV values. It can be downloaded here.

Tuesday, 2 December 2014

Automate the boring stuff with Python

A few weeks ago I found out that Al Sweigart is preparing a new book. I decided to send him an email to ask for a draft version and, surprise, surprise, he did send it to me! Unfortunately I have not had as much spare time as I thought I would, but I have finally started reading some chapters. Looks like I will have to wait till March for the paper version though.

Something I've been wanting to do for some time was a word cloud of my blog posts. But downloading 700 posts manually was never in my head (well, almost never). Enter Al´s book and python. Following some simple instructions from the Web Scraping chapter, I have written a fifty-line program that automatically downloads every post from my blog into a single text file. From there to the chart below there is a very small step.