Simple gesture recognition in AS3

A few years ago I coded a simple AS2 gesture recognition package in the hope I could use it in the future. Well. 5 years have passed. No real project have ever made use of it, except a small prototype for a (successful) pitch for Nintendo… well… you know… they’ve got that little DS thing… seemd appropriate, I guess. As often though, that which have been pitched wasn’t built, and the whole gesture/drawable UI component got ditched. And now I need this thing to work again, and I’m scared to even take a look at the old AS2 base code. Well it turns out, that stuff (in this simple form) isn’t really complicated and rewriting the whole thing from scratch didn’t take long at all.

Gesture recognition, the basic kind

Get Adobe Flash player

GestureRecognition.swf, draw the symbols

How it works

This gesture recognition method is done in three steps:

  • Capture user input
  • Homogenize and normalize input
  • Match to a predefined pattern

The first step is trivial and won’t be discussed. The second step is best described with a diagram. I chose to use an approach relying on a line segment/circle intersection algorithm. The goal is to get a path defined by equally distanced points, scaled to fit a 1×1 square. We can obtain this by iterating through the initial path, and finding intersections with a circle of radius r. This radius is very simply determined by dividing the length of the initial path by the number of points desired in the resulting path.

Note that the simplified path is most certainly going to be shorter than the initial path as a result of the process (see the diagram). In some occasions, the final path will effectively “miss” points. In this implementation we stop as soon as there is no intersection left with the next circle. We could think of lots of complicated and/or “expensive” ways to get that done more accurately. But really, as will show the next diagram, this does not affect the recognition itself.

Processing user input, for a simple gesture recognition engine

Processing user input

Once we have a normalized path, we can find a matching pattern in the dictionary. Comparing the data sets is now simply a matter of comparing where the points are relatively to each other within this 1×1 square.

Finding a match, for a simple gesture recognition engine

Finding a match

Since the points are ordered (there is a first point and a last point, and the order matters) we can find the average distance between the paths. We can expect the best matching pattern to have the lower average distance between its points and the normalized user generated path, as pictured on the diagram.

Note that the implementation I propose shows no optimization whatsoever. It will bluntly go through each and every pattern in the dictionary. There is also a total lack of quality control (or threshold value) regarding the “best match”. Whatever you draw, a match will be found. I personally think it is the way it should work, but well, you can disagree and add some extra checks yourself (see in the source, there are a couple hints on where to look).

I can see two very easy ways to optimize the search though. The relative position of the first points is absolutely important. Should you draw one of these symbols backwards, matching exactly the shape, well… it’s very unlikely it’ll be the best match. We can easily extrapolate that too great of a distance between the first points should discard the pattern (let’s say something like >=.5). What is also meaningful is the length of these paths, which should be “close enough”. A very first check combining a comparison in length and relative position of first points could discard a good number of irrelevant patterns. Then… we can look for the best match in whatever remains. Writing this, I suddenly feel like implementing it. Well. I certainly will if this gets used, as I hope, in a real project. Which, yes, is still up in the air.

Useful links

Github repo (includes rough gesture creation tool)
Paul Bourke’s line/sphere intersection algorithm

This entry was posted in ActionScript 3. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

16 Comments

  1. Posted January 14, 2010 at 4:51 am | Permalink

    Great work man :)

  2. dave
    Posted February 2, 2010 at 5:02 pm | Permalink

    Thanks, this helped get me on the right track on a current project involving gesture recognition.

  3. Posted March 8, 2010 at 7:38 pm | Permalink

    Thanks for the cool post. I’ve been digging around all day looking at gesture recognition classes. I’m particularly pleased that you went to the trouble of explaining your methodology. Most of the posts I’ve seen on the subject so far make vague allusions to existing algorithms that they conveniently don’t feel like explaining.

  4. Arnaud
    Posted March 8, 2010 at 8:39 pm | Permalink

    Glad you found this helpful!

  5. Posted June 14, 2010 at 5:53 am | Permalink

    Hello there :-)

    You have done great work!
    We’re using your awesome gesture recognition module in our AS 3.0 multitouch mindmapping application. Maybe you’ll take a look at our Dev-Weblog (it’s written in german only, we’ll maybe translate the blog after the project ist finished).
    -> http://web102.www8.united-systems.org/IG/ // http://touch.myst-ro.de/

    We’ll use & present our application for education only, there’s no commercial purpose at all.

    Best regards,
    Jens

  6. Arnaud
    Posted June 14, 2010 at 10:00 am | Permalink

    Hi Jens,
    I wished I could read German ;)
    Your project seems really interesting as far as I can tell. I’m looking forward to read the English translation!
    Best,
    Arnaud

  7. yann
    Posted June 15, 2010 at 8:21 am | Permalink

    Hello Arnaud,

    Great example, I try to add function when gesture are detected but without sucess.

    How to add a function to each icon when it is recognized ?

    Like this:
    when I draw heart I would like: heart_mc.visible = true;

    Can you explain ?

    Thanks and great works !!!

  8. Arnaud
    Posted June 15, 2010 at 10:55 am | Permalink

    Hi Yann,

    You can search for a matching pattern using:
    gestureDictionary.findMatch([your_points])
    This method returns an Object with details regarding the search results. As noted in the source code, this isn’t very pretty… Feel free to improve this!
    The matchingIndex property of the search result object should help you identify that a “heart” has been drawn.

  9. yann
    Posted June 15, 2010 at 11:42 am | Permalink

    Thanks Arnaud,

    I try with: gestureDictionary.findMatch(["heart"].matchingIndex);

    but error 1009…
    maybe a simple example can help me thanks

  10. Arnaud
    Posted June 15, 2010 at 12:22 pm | Permalink

    Yann,
    The findMatch method only accepts a parameter of type Vector.<Point>, the points of the path to be compared to the elements in the GestureDictionary.

  11. yann
    Posted June 15, 2010 at 12:56 pm | Permalink

    Yes Arnaud but I have a coercition error when I put points… anyway, I simply used _index like this: compar the result of array

    trace (_index)
    if (_index == 0) {
    bottleMc.visible = true;
    bottleMc.x = 435;
    bottleMc.y = 30;
    } else {
    bottleMc.visible = false;
    }
    Sure I think is not the best way to do… but work fine :-)

    Thanks

  12. Mark Yould
    Posted July 13, 2010 at 5:41 pm | Permalink

    hi,
    can anyone post working .fla demo? i don’t exactly know how to get it working from those gitHub files. thanks

  13. Mark Yould
    Posted July 13, 2010 at 6:20 pm | Permalink

    or give me any guide how to start? I’m lost now. thanks a lot

  14. Arnaud
    Posted July 14, 2010 at 9:12 am | Permalink

    You can use the .sh shell script files as a reference to compile using mxmlc. Alternatively, use any of the .as listed in these files as a document class in the Flash IDE, and voilà!

  15. Mark Yould
    Posted July 14, 2010 at 1:56 pm | Permalink

    ou yesss! I have used GestureRecognition.as as a main class in lib.fla and it works now. Thank you very much!

  16. Rein
    Posted July 25, 2010 at 6:16 pm | Permalink

    Hi,

    Could someone guide me on creating a new gesture and how to get the vector points to input into the dictionary?

    Thanks

5 Trackbacks

  1. By New year on January 5, 2010 at 12:20 pm

    [...] Coder's journal of Arnaud Icard Skip to content HomeAboutArchivesPlaces of interest « Simple gesture recognition in AS3 [...]

  2. By Simple gesture recognition in AS3 « G.I. on March 4, 2010 at 5:28 am

    [...] March 4, 2010 by Jos Simple gesture recognition in AS3 [...]

  3. [...] Simple Gesture Recognition in AS3 von Arnaud Icard (Schöne Animation und einfache Implementierung unter AS3) [...]

  4. By Es mindmapped wieder « Multitouch Mindmapping on June 14, 2010 at 5:37 am

    [...] Mindmapelement lässt sich mit einer Geste zeichnen (Gesture Framework von Sqrt(5)) – Mindmapelemente verbinden sich bei Kollision – Piemenü lässt sich skalieren und rotieren (Min [...]

  5. By Arnaud Icard & AS3 « byK on July 8, 2010 at 4:59 pm

    [...] Traer.physics port to AS3 (second image, link), a simple gesture recognition engine (top image, link), and 3D grass experiment with stitching sprite instances together seamlessly (third image, link). [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>