def on_touch_up(self, touch): self.points += [touch.pos] gesture = Gesture() gesture.add_stroke(self.points) gesture.normalize() gdb = GestureDatabase() print "Gesture:", gdb.gesture_to_str(gesture)
def on_touch_up(self, touch): self.points += [touch.pos] gesture = Gesture() gesture.add_stroke(self.points) gesture.normalize() gdb = GestureDatabase() print("Gesture:", gdb.gesture_to_str(gesture).decode(encoding='UTF-8'))
class GestureManager(object): def __init__(self, path='./chronos/assets/gestures/'): with open(path + 'gestures.json', "r+") as f: self.path = path self.str_gestures = json.load(f) self.db = GestureDatabase() for name, gesture_string in self.str_gestures.items(): gesture = self.db.str_to_gesture(gesture_string) gesture.name = name self.db.add_gesture(gesture) def add_gesture(self, name, gesture_path): gesture = Gesture() gesture.add_stroke(gesture_path) gesture.normalize() if name in self.str_gestures: raise ValueError('Cannot overwrite existing gesture in file.') gesture.name = name self.str_gestures[name] = self.db.gesture_to_str(gesture).decode( 'utf-8') self.db.add_gesture(gesture) def save(self, path=None): if not path: path = self.path with open(path + 'gestures.json', "r+") as f: f.seek(0) json.dump(self.str_gestures, f) f.truncate()
class cGestureBoard(cTouchRectangle): ''' base class for recording gestures ''' def __init__(self, **kwargs): super(cGestureBoard, self).__init__(**kwargs) self.gdb = GestureDatabase() def on_touch_down(self, touch): #self.DrawStandardGestures() # start collecting points in touch.ud # create a line to display the points if not self.collide_point(touch.x, touch.y): return False touch.grab(self) userdata = touch.ud with self.canvas: Color(1, 1, 0) d = 10. Ellipse(pos=(touch.x - d/2, touch.y - d/2), size=(d, d)) userdata['line'] = Line(points=(touch.x, touch.y)) return True def on_touch_move(self, touch): if touch.grab_current is not self: return # store points of the touch movement try: touch.ud['line'].points += [touch.x, touch.y] return True except KeyError: pass return True def on_touch_up(self, touch): if touch.grab_current is not self: return g = simplegesture('',zip(touch.ud['line'].points[::2], touch.ud['line'].points[1::2])) # print "gesture representation:", ':',self.gdb.gesture_to_str(g) uLogName=oORCA.uGestureLogName oLogFile = open(uLogName, 'a') oLogFile.write('Gesturecode:'+self.gdb.gesture_to_str(g)+'\n') oLogFile.close() return True
class cGestureBoard(cTouchRectangle): """ base class for recording gestures """ def __init__(self, **kwargs): super(cGestureBoard, self).__init__(**kwargs) self.gdb = GestureDatabase() def on_touch_down(self, touch): #self.DrawStandardGestures() # start collecting points in touch.ud # create a line to display the points if not self.collide_point(touch.x, touch.y): return False touch.grab(self) userdata = touch.ud with self.canvas: Color(1, 1, 0) d = 10. Ellipse(pos=(touch.x - d/2, touch.y - d/2), size=(d, d)) userdata['line'] = Line(points=(touch.x, touch.y)) return True def on_touch_move(self, touch): if touch.grab_current is not self: return # store points of the touch movement try: touch.ud['line'].points += [touch.x, touch.y] return True except KeyError: pass return True def on_touch_up(self, touch): if touch.grab_current is not self: return g = simplegesture('',zip(touch.ud['line'].points[::2], touch.ud['line'].points[1::2])) # print "gesture representation:", ':',self.gdb.gesture_to_str(g) uLogName=Globals.oFnGestureLog.string oLogFile = open(uLogName, 'a') oLogFile.write('Gesturecode:'+self.gdb.gesture_to_str(g)+'\n') oLogFile.close() return True
class GestureBoard(FloatLayout): """ Our application main widget, derived from touchtracer example, use data constructed from touches to match symboles loaded from my_gestures. """ def __init__(self, *args, **kwargs): super(GestureBoard, self).__init__() self.gdb = GestureDatabase() # add pre-recorded gestures to database self.gdb.add_gesture(cross) self.gdb.add_gesture(check) self.gdb.add_gesture(circle) self.gdb.add_gesture(square) def on_touch_down(self, touch): # start collecting points in touch.ud # create a line to display the points userdata = touch.ud with self.canvas: Color(1, 1, 0) d = 30. Ellipse(pos=(touch.x - d / 2, touch.y - d / 2), size=(d, d)) userdata['line'] = Line(points=(touch.x, touch.y)) return True def on_touch_move(self, touch): # store points of the touch movement try: touch.ud['line'].points += [touch.x, touch.y] return True except (KeyError) as e: pass def on_touch_up(self, touch): # touch is over, display informations, and check if it matches some # known gesture. g = simplegesture( '', list( zip(touch.ud['line'].points[::2], touch.ud['line'].points[1::2]))) # gestures to my_gestures.py print("gesture representation:", self.gdb.gesture_to_str(g)) # print match scores between all known gestures print("cross:", g.get_score(cross)) print("check:", g.get_score(check)) print("circle:", g.get_score(circle)) print("square:", g.get_score(square)) # use database to find the more alike gesture, if any g2 = self.gdb.find(g, minscore=0.70) print(g2) if g2: if g2[1] == circle: print("circle") if g2[1] == square: print("square") if g2[1] == check: print("check") if g2[1] == cross: print("cross") # erase the lines on the screen, this is a bit quick&dirty, since we # can have another touch event on the way... self.canvas.clear()
class GestureBoard(FloatLayout): """ Our application main widget, derived from touchtracer example, use data constructed from touches to match symboles loaded from my_gestures. """ def __init__(self, *args, **kwargs): super(GestureBoard, self).__init__() self.gdb = GestureDatabase() # add pre-recorded gestures to database self.gdb.add_gesture(cross) self.gdb.add_gesture(check) self.gdb.add_gesture(circle) self.gdb.add_gesture(square) def on_touch_down(self, touch): # start collecting points in touch.ud # create a line to display the points userdata = touch.ud with self.canvas: Color(1, 1, 0) d = 30. Ellipse(pos=(touch.x - d / 2, touch.y - d / 2), size=(d, d)) userdata['line'] = Line(points=(touch.x, touch.y)) return True def on_touch_move(self, touch): # store points of the touch movement try: touch.ud['line'].points += [touch.x, touch.y] return True except (KeyError) as e: pass def on_touch_up(self, touch): # touch is over, display informations, and check if it matches some # known gesture. g = simplegesture('', list(zip(touch.ud['line'].points[::2], touch.ud['line'].points[1::2]))) # gestures to my_gestures.py print("gesture representation:", self.gdb.gesture_to_str(g)) # print match scores between all known gestures print("cross:", g.get_score(cross)) print("check:", g.get_score(check)) print("circle:", g.get_score(circle)) print("square:", g.get_score(square)) # use database to find the more alike gesture, if any g2 = self.gdb.find(g, minscore=0.70) print(g2) if g2: if g2[1] == circle: print("circle") if g2[1] == square: print("square") if g2[1] == check: print("check") if g2[1] == cross: print("cross") # erase the lines on the screen, this is a bit quick&dirty, since we # can have another touch event on the way... self.canvas.clear()
class GestureBoard(FloatLayout): ponts = 0 def __init__(self, *args, **kwargs): super(GestureBoard, self).__init__() self.gdb = GestureDatabase() self.error_sound = SoundLoader.load('error.mp3') self.success_sound = SoundLoader.load('success.mp3') #Gestures for key, ges in gestures.items(): self.gdb.add_gesture(ges) self.sort_letter() def sort_letter(self): self.letter = random.choice(gestures.keys()) letter_button = Button( text=self.letter, size_hint=(.1, .1), pos_hint={'x':0, 'y':.9}, font_size=38, line_height=1 ) self.add_widget(letter_button) ponts_button = Button( text=u"Pontos: %s" % self.ponts, size_hint=(.3, .1), pos_hint={'x':.7, 'y':.9} ) self.add_widget(ponts_button) def on_touch_down(self, touch): # start collecting points in touch.ud # create a line to display the points userdata = touch.ud with self.canvas: Color(1, 1, 0) d = 30. Ellipse(pos=(touch.x - d/2, touch.y - d/2), size=(d, d)) userdata['line'] = Line(points=(touch.x, touch.y)) return True def on_touch_move(self, touch): # store points of the touch movement try: touch.ud['line'].points += [touch.x, touch.y] return True except (KeyError) as e: pass def on_touch_up(self, touch): # touch is over, display informations, and check if it matches some # known gesture. g = self.simplegesture( '', list(zip(touch.ud['line'].points[::2], touch.ud['line'].points[1::2])) ) print("gesture representation:", self.gdb.gesture_to_str(g)) # use database to find the more alike gesture, if any g2 = self.gdb.find(g, minscore=0.70) if g2: if self._get_key_by_gesture(g2[1]) == self.letter: self.ponts += 10 if self.success_sound.status != 'stop': self.success_sound.stop() self.success_sound.play() else: if self.error_sound.status != 'stop': self.error_sound.stop() self.error_sound.play() # erase the lines on the screen, this is a bit quick&dirty, since we # can have another touch event on the way... self.canvas.clear() self.sort_letter() def _get_key_by_gesture(self, g): for key, ges in gestures.items(): if g == ges: return key def simplegesture(self, name, point_list): """ A simple helper function """ g = Gesture() g.add_stroke(point_list) g.normalize() g.name = name return g