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 docs.opencv.org and stackoverflow.com. 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.

1 comment:

Kety said...

Que paséis buena noche. Nos vemos pronto. ;-))
Besos.besos.besos.