Dear community,
As a fairly new Codea user I have been learning Lua and generally getting familiar with the platform.
I wanted to investigate the ability for gesture recognition in a project I am working on, initially for basic ‘swipe’ detection, but also for some more sophisticated gestures…
I came across the following algorithm: http://pokristensson.com/increc.html which was exactly what I was looking for and had the bonus of a good Java implementation.
I have ported the algorithm to Lua/Codea and it seems to work really well. I contacted the author and with their permission have made it available here: https://github.com/brookesi/Codea/tree/master/src
Usage of this algorithm requires attribution to the original paper, so I have added the full copyright notice from the original Java source to the top of CGR.lua. If you use this please do not modify the header.
I do not fully understand how the algorithm works, but it seems to work really well. I have provided a basic Main.lua to demonstrate the algorithm.
Basically you define a set of gesture templates where each template is a series of coordinates in a virtual space. You then register the templates with the library and call the CGR_recognize(…) function with the gesture coordinates generated by the user using the Codea ‘touch’ API.
Hopefully it is fairly clear from the code and example how this works.
The algorithm is actually designed for continuous recognition, e.g. you could call the recognise function each time a swipe coordinate is added to the array, which then returns you an array of probabilities in descending order for each templated gesture… In my example I only call CGR_recognize(…) when the touch event is finished as I suspect performance may be an issue…
I made some minor optimisations to the algorithm during the Lua port to reduce table creation for points, using vec2() instead of a table for each Point with an x and y key, otherwise it is basically as originally written by the authors…
I am new to Lua, but a long time Java programmer so there may well be other optimisations. The copyright allows for general usage (including commercial) and modification so long as the paper is cited…
Notes, warnings and caveats:
-
My Main.lua implementation uses CurrentTouch rather than the touch() callback, so I occasionally find the gesture touch finish event does not process correctly, experts on this forum will probably know why that is, I am doing touch processing in the draw() callback, so I think that may provide a clue…
-
The algorithm uses a coordinate space where (0, 0) is in the top left corner of the virtual grid. Codea/iPad has (0, 0) in the bottom left corner so when storing the user coordinates you need to use HEIGHT-y to ‘flip’ the y-axis, if that makes sense…
-
As I mention in the porting notes in CGR.lua, registering similar templates causes the probabilities to decrease as the algorithm reasonably isn’t so sure what gesture you mean, so this is a potential issue with signalling whether the user has performed a ‘correct’ gesture as CGR will always return probabilities for all templates ranked in descending order, so some thought would be required for ‘real world’ usage…
-
I have left the implementation as a Lua script with the required functions as globals e.g. CGR_recognize(…) because I ported this on Windows where I did not have the Codea class mechanism, the CGR_ prefix is a lazy namespace as it were…
-
The function parameters are a bit opaque in places as e.g. p1, p2,…pn as I wrote myself a Java2Lua converter to create the script ‘skeleton’ by parsing the Java static classes and methods using Java Reflection, then implemented each function in Lua by hand (1-based indexing, arghhhh!!!)
I hope this may be of interest to the Codea community at large. I am also new to Github so I hope you can see the files. Please chat on this discussion if you have issues,
Best regards,
Simon Brooke