Chris Miles
November 15, 2012
210

CMUnistrokeGestureRecognizer - iOS port of \$1 Unistroke Recognizer

Introducing CMUnistrokeGestureRecognizer, my open source implementation of the \$1 Unistroke Recognizer for iOS. Presented at Melbourne Cocoaheads Nov 2012.

Chris Miles

November 15, 2012

Transcript

1. CMUnistrokeGestureRecognizer Chris Miles Freelance Software Engineer iOS Specialist @chrismiles http:/

/chrismiles.info/ Friday, 8 February 13

6. \$1 Goals Resilient to movement & sampling speed Rotation, scale,

position invariance No advanced maths (e.g., matrix inversions, derivatives, integrals) Easily written with few lines of code Friday, 8 February 13
7. \$1 Goals Fast enough for interactive use Define gestures with

minimum of only one example Return top results ordered by score [0-1] Provide recognition rates that are competitive with more complex algorithms Friday, 8 February 13

11. Step 1: Resample Resample to N equidistantly spaced points 32

≤ N ≤ 256 Friday, 8 February 13
12. Step 2: Rotate Rotate to "Indicative Angle" Indicative Angle is

the angle required to rotate the gesture’s first point to 0° about the centroid of the gesture Friday, 8 February 13
13. Step 3: Scale & Translate Scale to a reference square

Translate the centroid to (0,0) Friday, 8 February 13
14. Step 4: Recognise Calculate the average distance di between corresponding

points of candidate C and each template Ti This is the "Path-Distance" Friday, 8 February 13
15. Step 4: Recognise The template Ti with the least path-

distance to C is the winner Calculate a [0-1] score with: Friday, 8 February 13
16. Step 4: Recognise Optional: Rotation Invariance Attempt to align two

gestures C and Ti more optimally A strategy called Golden Section Search is used to scan best fit between ±45° from indicative angle with threshold 2° Friday, 8 February 13
17. Step 4: Recognise Alternatively: Protractor method Similar steps 1-3: Resample,

rotate, scale & translate Step 4 compares a vector representation of the gesture against templates using Optimal-Cosine- Distance Friday, 8 February 13
18. CMUnistrokeGestureRecognizer UIGestureRecognizer Core algorithm implemented in C GLKMath for optimal

vector math Friday, 8 February 13
19. @interface CMUnistrokeGestureRecognizer : UIGestureRecognizer @property (nonatomic, weak) id<CMUnistrokeGestureRecognizerDelegate> unistrokeDelegate; @property

(nonatomic, assign) float minimumScoreThreshold; @property (nonatomic, assign) BOOL protactorMethodEnabled; @property (nonatomic, assign) BOOL rotationInvarianceEnabled; @property (nonatomic, strong, readonly) CMUnistrokeGestureResult *result; @property (nonatomic, strong, readonly) UIBezierPath *strokePath; - (void)registerUnistrokeWithName:(NSString *)name bezierPath: (UIBezierPath *)path; - (void)clearAllUnistrokes; @end Friday, 8 February 13
20. @protocol CMUnistrokeGestureRecognizerDelegate <NSObject> @optional - (void)unistrokeGestureRecognizer:(CMUnistrokeGestureRecognizer *)unistrokeGestureRecognizer isEvaluatingStrokePath:(UIBezierPath *)strokePath; -

(void)unistrokeGestureRecognizerDidFailToRecognize: (CMUnistrokeGestureRecognizer *)unistrokeGestureRecognizer; @end Friday, 8 February 13

13