Make has a write-up on wand-control with a simple reflective wand…
…it *was* broken. Fixed it.
Back in January when I first read the article I was so excited to try it, I ordered a bunch of parts, downloaded the Git, and then figured out it didn’t work.
Not to worry though. A fellow named John Horton contacted me, and inspired me to try again. This time, I decided to try to understand the code, and get it up to snuff. If you want to skip ahead. The code is here:
To be clear… I’m not a python dev… those that are will definitely cringe… Sorry.
Here is the original writeup…
The original concept is fantastic… it just didn’t work for me.
So I tried to get it going from the ground up, and rearchitected the source so its multi-threaded, and uses machine learning to match gestures. Even the image feed is fast! 😉
First, prepare the PI3 by installing OpenCV
First – start with a fresh disk. I use piBakery:
These are the exact steps I used to get a fresh full-desktop PI3 up and running:
They were mostly gleaned from here (with one minor fix):
Like many of you, I tried piles of instructions without success. I was surprised when this set worked with out a hitch.
Next, get the code…
I’ve made the code available here:
git clone https://github.com/mamacker/pi_to_potter
It has a version that is very close to the original, just sped up and tweaked. That one is called rpotter.py.
The other one I created is called trained.py, in that one I used machine learning!!! Which was extremely entertaining.
To run it, cd into pi_to_potter:
Note – it takes a while to start up, because it runs through all of the images in the Pictures directory to train itself to recognize those gestures.
Make sure your environment is mostly free of reflective surfaces. Those reflections behind you will ruin the wand detection. You want one dot… the wand. 🙂
Once the code is running, put something reflective in your camera’s field of view. Make sure its just a point, otherwise your gesture will be difficult to see. Once something is seen. Two windows will come up:
The “Original” will flicker between the real image, and any detected, thresholded, light reflection. Original, should be where you see motion.
The “Raspberry Potter” window, will show you any tracks created by Optical Flow.
Finally, watch the command-line logs. That’s where you’ll see the name of the recognized image. When you are ready to do something based on a recognition, update the Spells function. You can refer to some other articles on how to control outlets for fun:
Universal Studio’s Wands are really bad reflectors… Or there is other magic…
Universal Studio’s wands are really bad reflectors. They must have some serious emitters at the park, because getting light back from them is terribly difficult. So I ordered a bunch of other materials to try out.
This is the tape on the end of cost-effective-for-kids wands I found:
I found the wands on Etsy:
The camera I used – the Pi Noir
How this technique works…
This technique uses image processing to track the wands position through a series of pictures taken on the camera. It first has to find the wand within the view, once its identified the wand light, it uses a function in the OpenCV package to track its movement:
calcOpticalFlowPyrLK: Calculates an optical flow for a sparse feature set using the iterative Lucas-Kanade method with pyramids.
This provides points from the image set which can be matched against the gesture “shapes”. Where the shape check in the original simply takes two line segments, identifies them as move up, left, down or right. The combination of any two creates a recognizable request.
It’s really quite brilliant in its simplicity.
The original code for the image recognition is found here:
And it’s wonderfully tiny. The updated version is found in my repo here:
Now you can train it!!!
The “triangle” training set
So, while I was in there I was able to add the ability to train for gestures. Once you have the whole system up and running. Add the –train flag. That will start storing new images in the /Pictures directory. These will be the attempts at gestures people do in front of your camera. You get a starter set I recorded when you get the repo.
python trained.py --train
Practice the gestures until you get a good set of them in the Pictures folder, at least 5, and they need to be distinct enough from the others to not conflict. Once you have a good collection, create a folder for them with a simple name. Something like “star”, or “book”, or “funny”. Then that command will be auto-learned at the next restart.
The last step is to add an “if” statement that uses it in the “Spell” function:
Add your new name in that list… and make it do something! Once you’re done, restart the code, and watch for your recognition to show in the logs.
I’ll let you know…
I’ll try again as soon as my reflective bits come in.
If you have a little more funds and less time – the build where the smarts are in the wand can be found here: