Table Of Contents
Multistroke gesture recognizer¶
New in version 1.9.0.
This is experimental and subject to change as long as this warning notice is present.
kivy/examples/demo/multistroke/main.py for a complete application
This module implements the Protractor gesture recognition algorithm.
ProgressTracker tracks the progress of a
call. It can be used to interact with the running recognizer task, for example
forcing it to stop half-way, or analyzing results as they arrive.
MultistrokeGesture represents a gesture in the gesture database
Recognizer.db). It is a container for
objects, and implements the heap permute algorithm to automatically generate
all possible stroke orders (if desired).
kivy/examples/demo/multistroke/main.py for a complete application
You can bind to events on
Recognizer to track the state of all
Recognizer.recognize(). The callback function will receive an
ProgressTracker that can be used to analyze and control
various aspects of the recognition process
from kivy.vector import Vector from kivy.multistroke import Recognizer gdb = Recognizer() def search_start(gdb, pt): print("A search is starting with %d tasks" % (pt.tasks)) def search_stop(gdb, pt): # This will call max() on the result dictionary, so it's best to store # it instead of calling it 3 times consecutively best = pt.best print("Search ended (%s). Best is %s (score %f, distance %f)" % ( pt.status, best['name'], best['score'], best['dist'] )) # Bind your callbacks to track all matching operations gdb.bind(on_search_start=search_start) gdb.bind(on_search_complete=search_stop) # The format below is referred to as `strokes`, a list of stroke paths. # Note that each path shown here consists of two points, ie a straight # line; if you plot them it looks like a T, hence the name. gdb.add_gesture('T', [ [Vector(30, 7), Vector(103, 7)], [Vector(66, 7), Vector(66, 87)]]) # Now you can search for the 'T' gesture using similar data (user input). # This will trigger both of the callbacks bound above. gdb.recognize([ [Vector(45, 8), Vector(110, 12)], [Vector(88, 9), Vector(85, 95)]])
On the next
Clock tick, the matching process starts
(and, in this case, completes).
# Same as above, but keep track of progress using returned value progress = gdb.recognize([ [Vector(45, 8), Vector(110, 12)], [Vector(88, 9), Vector(85, 95)]]) progress.bind(on_progress=my_other_callback) print(progress.progress) # = 0 # [ assuming a kivy.clock.Clock.tick() here ] print(result.progress) # = 1
For more information about the matching algorithm, see:
- “Protractor: A fast and accurate gesture recognizer” by Yang Li
- “$N-Protractor” by Lisa Anthony and Jacob O. Wobbrock
Recognizerprovides a gesture database with matching facilities.
Fired when a new search is started using this Recognizer.
Fired when a running search ends, for whatever reason. (use
ProgressTracker.statusto find out)
add_gesture(name, strokes, **kwargs)¶
Add a new gesture to the database. This will instantiate a new
MultistrokeGesturewith strokes and append it to self.db.
If you already have instantiated a
MultistrokeGestureobject and wish to add it, append it to
Export a list of
MultistrokeGestureobjects. Outputs a base64-encoded string that can be decoded to a Python list with the
parse_gesture()function or imported directly to
Recognizer.import_gesture(). If filename is specified, the output is written to disk, otherwise returned.
This method accepts optional
filter()returns a subset of objects in
self.db, according to given criteria. This is used by many other methods of the
Recognizer; the arguments below can for example be used when calling
Recognizer.export_gesture(). You normally don’t need to call this directly.
Limits the returned list to gestures where
MultistrokeGesture.namematches given regular expression(s). If re.match(name, MultistrokeGesture.name) tests true, the gesture is included in the returned list. Can be a string or an array of strings
gdb = Recognizer() # Will match all names that start with a capital N # (ie Next, New, N, Nebraska etc, but not "n" or "next") gdb.filter(name='N') # exactly 'N' gdb.filter(name='N$') # Nebraska, teletubbies, France, fraggle, N, n, etc gdb.filter(name=['[Nn]', '(?i)T', '(?i)F'])
Limits the returned list to gestures with certain
MultistrokeGesture.priorityvalues. If specified as an integer, only gestures with a lower priority are returned. If specified as a list (min/max)
# Max priority 50 gdb.filter(priority=50) # Max priority 50 (same result as above) gdb.filter(priority=[0, 50]) # Min priority 50, max 100 gdb.filter(priority=[50, 100])
When this option is used,
Recognizer.dbis automatically sorted according to priority, incurring extra cost. You can use force_priority_sort to override this behavior if your gestures are already sorted according to priority.
Limits the returned list to gestures that are orientation sensitive (True), gestures that are not orientation sensitive (False) or None (ignore template sensitivity, this is the default).
Limits the returned list to gestures that have the specified number of strokes (in
MultistrokeGesture.strokes). Can be a single integer or a list of integers.
Limits the returned list to gestures that have specific
MultistrokeGesture.numpointsvalues. This is provided for flexibility, do not use it unless you understand what it does. Can be a single integer or a list of integers.
Can be used to override the default sort behavior. Normally
MultistrokeGestureobjects are returned in priority order if the priority option is used. Setting this to True will return gestures sorted in priority order, False will return in the order gestures were added. None means decide automatically (the default).
For improved performance, you can load your gesture database in priority order and set this to False when calling
Can be set if you want to filter a different list of objects than
Recognizer.db. You probably don’t want to do this; it is used internally by
import_gesture(data=None, filename=None, **kwargs)¶
Import a list of gestures as formatted by
export_gesture(). One of data or filename must be specified.
This method accepts optional
Recognizer.filter()arguments, if none are specified then all gestures in specified data are imported.
This method is used to prepare
UnistrokeTemplateobjects within the gestures in self.db. This is useful if you want to minimize punishment of lazy resampling by preparing all vectors in advance. If you do this before a call to
Recognizer.export_gesture(), you will have the vectors computed when you load the data later.
This method accepts optional
force_numpoints, if specified, will prepare all templates to the given number of points (instead of each template’s preferred n; ie
UnistrokeTemplate.numpoints). You normally don’t want to do this.
recognize(strokes, goodscore=None, timeout=0, delay=0, **kwargs)¶
Search for gestures matching strokes. Returns a
This method accepts optional
If you manually supply a
Candidatethat has a skip-flag, make sure that the correct filter arguments are set. Otherwise the system will attempt to load vectors that have not been computed. For example, if you set skip_bounded and do not set orientation_sensitive to False, it will raise an exception if an orientation_sensitive
If this is set (between 0.0 - 1.0) and a gesture score is equal to or higher than the specified value, the search is immediately halted and the on_search_complete event is fired (+ the on_complete event of the associated
ProgressTrackerinstance). Default is None (disabled).
Specifies a timeout (in seconds) for when the search is aborted and the results returned. This option applies only when max_gpf is not 0. Default value is 0, meaning all gestures in the database will be tested, no matter how long it takes.
Specifies the maximum number of
MultistrokeGestureobjects that can be processed per frame. When exceeded, will cause the search to halt and resume work in the next frame. Setting to 0 will complete the search immediately (and block the UI).
This does not limit the number of
UnistrokeTemplateobjects matched! If a single gesture has a million templates, they will all be processed in a single frame with max_gpf=1!
Sets an optional delay between each run of the recognizer loop. Normally, a run is scheduled for the next frame until the tasklist is exhausted. If you set this, there will be an additional delay between each run (specified in seconds). Default is 0, resume in the next frame.
forces all templates (and candidate) to be prepared to a certain number of points. This can be useful for example if you are evaluating templates for optimal n (do not use this unless you understand what it does).
ProgressTracker(candidate, tasks, **kwargs)¶
Represents an ongoing (or completed) search operation. Instantiated and returned by the
Recognizer.recognize()method when it is called. The results attribute is a dictionary that is updated as the recognition operation progresses.
You do not need to instantiate this class.
Candidateobject to be evaluated
Total number of gestures in tasklist (to test against)
Fired for every gesture that is processed
Fired when a new result is added, and it is the first match for the name so far, or a consecutive match with better score.
Fired when the search is completed, for whatever reason. (use ProgressTracker.status to find out)
A dictionary of all results (so far). The key is the name of the gesture (ie
UnistrokeTemplate.nameusually inherited from
MultistrokeGesture). Each item in the dictionary is a dict with the following entries:
Name of the matched template (redundant)
Computed score from 1.0 (perfect match) to 0.0
Cosine distance from candidate to template (low=closer)
MultistrokeGestureobject that was matched
Index of the best matching template (in
List of distances for all templates. The list index corresponds to a
UnistrokeTemplateindex in gesture.templates.
Was stopped by the user (
A timeout occurred (specified as timeout= to recognize())
The search was stopped early because a gesture with a high enough score was found (specified as goodscore= to recognize())
The search is complete (all gestures matching filters were tested)
Return the best match found by recognize() so far. It returns a dictionary with three keys, ‘name’, ‘dist’ and ‘score’ representing the template’s name, distance (from candidate path) and the computed score value. This is a Python property.
Returns the progress as a float, 0 is 0% done, 1 is 100%. This is a Python property.
Raises a stop flag that is checked by the search process. It will be stopped on the next clock tick (if it is still running).
MultistrokeGesture(name, strokes=None, **kwargs)¶
Identifies the name of the gesture - it is returned to you in the results of a
Recognizer.recognize()search. You can have any number of MultistrokeGesture objects with the same name; many definitions of one gesture. The same name is given to all the generated unistroke permutations. Required, no default.
A list of paths that represents the gesture. A path is a list of Vector objects:
gesture = MultistrokeGesture('my_gesture', strokes=[ [Vector(x1, y1), Vector(x2, y2), ...... ], # stroke 1 [Vector(), Vector(), Vector(), Vector() ] # stroke 2 #, [stroke 3], [stroke 4], ... ])
For template matching purposes, all the strokes are combined to a single list (unistroke). You should still specify the strokes individually, and set stroke_sensitive True (whenever possible).
Once you do this, unistroke permutations are immediately generated and stored in self.templates for later, unless you set the permute flag to False.
Recognizer.recognize()will attempt to match this template, lower priorities are evaluated first (only if a priority filter is used). You should use lower priority on gestures that are more likely to match. For example, set user templates at lower number than generic templates. Default is 100.
Determines the number of points this gesture should be resampled to (for matching purposes). The default is 16.
Determines if the number of strokes (paths) in this gesture is required to be the same in the candidate (user input) gesture during matching. If this is False, candidates will always be evaluated, disregarding the number of strokes. Default is True.
Determines if this gesture is orientation sensitive. If True, aligns the indicative orientation with the one of eight base orientations that requires least rotation. Default is True.
This is used by the
Recognizer.recognize()function when a candidate is evaluated against this gesture. If the angles between them are too far off, the template is considered a non-match. Default is 30.0 (degrees)
If False, do not use Heap Permute algorithm to generate different stroke orders when instantiated. If you set this to False, a single UnistrokeTemplate built from strokes is used.
Add a stroke to the self.strokes list. If permute is True, the
permute()method is called to generate new unistroke templates
get_distance(cand, tpl, numpoints=None)¶
Compute the distance from this Candiate to a UnistrokeTemplate. Returns the Cosine distance between the stroke paths.
numpoints will prepare both the UnistrokeTemplate and Candidate path to n points (when necessary), you probably don’t want to do this.
Match a given candidate against this MultistrokeGesture object. Will test against all templates and report results as a list of four items:
- index 0
- Best matching template’s index (in self.templates)
- index 1
- Computed distance from the template to the candidate path
- index 2
- List of distances for all templates. The list index
corresponds to a
UnistrokeTemplateindex in self.templates.
- index 3
- Counter for the number of performed matching operations, ie templates matched against the candidate
Generate all possible unistroke permutations from self.strokes and save the resulting list of UnistrokeTemplate objects in self.templates.
We use Heap Permute  (p. 179) to generate all stroke orders in a multistroke gesture. Then, to generate stroke directions for each order, we treat each component stroke as a dichotomous [0,1] variable. There are 2^N combinations for N strokes, so we convert the decimal values 0 to 2^N-1, inclusive, to binary representations and regard each bit as indicating forward (0) or reverse (1). This algorithm is often used to generate truth tables in propositional logic.
See section 4.1: “$N Algorithm” of the linked paper for details.
Using heap permute for gestures with more than 3 strokes can result in very large number of templates (a 9-stroke gesture = 38 million templates). If you are dealing with these types of gestures, you should manually compose all the desired stroke orders.
UnistrokeTemplate(name, points=None, **kwargs)¶
Represents a (uni)stroke path as a list of Vectors. Normally, this class is instantiated by MultistrokeGesture and not by the programmer directly. However, it is possible to manually compose UnistrokeTemplate objects.
Identifies the name of the gesture. This is normally inherited from the parent MultistrokeGesture object when a template is generated.
A list of points that represents a unistroke path. This is normally one of the possible stroke order permutations from a MultistrokeGesture.
The number of points this template should (ideally) be resampled to before the matching process. The default is 16, but you can use a template-specific settings if that improves results.
Determines if this template is orientation sensitive (True) or fully rotation invariant (False). The default is True.
You will get an exception if you set a skip-flag and then attempt to retrieve those vectors.
Add a point to the unistroke/path. This invalidates all previously computed vectors.
This function prepares the UnistrokeTemplate for matching given a target number of points (for resample). 16 is optimal.
Candidate(strokes=None, numpoints=16, **kwargs)¶
Represents a set of unistroke paths of user input, ie data to be matched against a
UnistrokeTemplateobject using the Protractor algorithm. By default, data is precomputed to match both rotation bounded and fully invariant
MultistrokeGesture.strokesfor format example. The Candidate strokes are simply combined to a unistroke in the order given. The idea is that this will match one of the unistroke permutations in MultistrokeGesture.templates.
The Candidate’s default N; this is only for a fallback, it is not normally used since n is driven by the UnistrokeTemplate we are being compared to.
If True, do not generate/store rotation bounded vectors
If True, do not generate/store rotation invariant vectors
Note that you WILL get errors if you set a skip-flag and then attempt to retrieve the data.
Add a stroke to the candidate; this will invalidate all previously computed vectors
(Internal use only) Compute the angle similarity between this Candidate and a UnistrokeTemplate object. Returns a number that represents the angle similarity (lower is more similar).
(Internal use only) Return vector for comparing to a UnistrokeTemplate with Protractor
(Internal use only) Get the start vector for this Candidate, with the path resampled to numpoints points. This is the first step in the matching process. It is compared to a UnistrokeTemplate object’s start vector to determine angle similarity.
Prepare the Candidate vectors. self.strokes is combined to a single unistroke (connected end-to-end), resampled to
numpointspoints, and then the vectors are calculated and stored in self.db (for use by get_distance and get_angle_similarity)