Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- """Write on a canvas, store on-line data."""
- from __future__ import unicode_literals
- __version__ = '0.1'
- from gi.repository import Gtk, Gdk
- import time
- import math
- import json
- # For the call for classification
- import requests
- import urllib
- try:
- from urllib.parse import urlencode
- except:
- # Python 2.7 fallback
- chr = unichr
- from urllib import urlencode
- # For user interaction / pasting stuff where it should be
- from pykeyboard import PyKeyboard
- classification_guess = ""
- class FormulaWriter(Gtk.Window):
- def __init__(self):
- super(FormulaWriter, self).__init__()
- self.odata = [] # On-line writing information, grouped by strokes
- # General properties
- self.set_title("Formula Writer %s" % __version__)
- self.resize(400, 400)
- self.set_position(Gtk.WindowPosition.CENTER)
- self.connect("delete-event", self.on_quit)
- self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
- # Set up canvas
- self.canvas = Gtk.DrawingArea()
- self.canvas.connect("draw", self.on_draw)
- self.canvas.connect("button-press-event", self.on_button_press)
- self.canvas.connect("button-release-event", self.on_button_released)
- self.canvas.connect("motion-notify-event", self.on_mouse_move)
- self.canvas.connect("motion-notify-event", self.on_mouse_move)
- self.canvas.set_events(self.canvas.get_events() |
- Gdk.EventMask.BUTTON_MOTION_MASK |
- Gdk.EventMask.BUTTON1_MOTION_MASK |
- Gdk.EventMask.BUTTON2_MOTION_MASK |
- Gdk.EventMask.BUTTON3_MOTION_MASK |
- Gdk.EventMask.BUTTON_PRESS_MASK |
- Gdk.EventMask.BUTTON_RELEASE_MASK)
- self.add(self.canvas)
- self.show_all()
- def on_button_press(self, w, event):
- """When a button is pressed, the location gets stored and the canvas
- gets updated.
- """
- self.odata.append([{'x': event.x, 'y': event.y, 'time': time.time()}])
- self.canvas.queue_draw()
- def on_mouse_move(self, w, event):
- """When mouse is moved, the mouse position gets stored."""
- point = {'x': event.x, 'y': event.y, 'time': time.time()}
- self.odata[-1].append(point)
- self.canvas.queue_draw()
- def on_button_released(self, w, event):
- global classification_guess
- classification = request_classification(self.odata)
- for tmp in classification:
- tmp = list(tmp.items())[0]
- classinfo, probability = tmp
- #print("%0.2f: %s" % (probability, classinfo))
- db_id, latex, unicode_dec, font, font_style = classinfo.split(";")
- classification_guess = latex
- classification_guess = chr(int(unicode_dec))
- break
- self.clipboard.set_text(classification_guess, -1)
- def on_draw(self, wid, cr):
- """Handler for drawing action. Draw all strokes.
- :param wid: The DrawingArea
- :param cr: Context
- """
- cr.set_source_rgb(1, 0, 0) # All strokes get drawn in red
- cr.set_line_width(2.5)
- for stroke in self.odata:
- for i, point in enumerate(stroke):
- if len(stroke) == 1:
- radius = 2
- cr.arc(point['x'], point['y'], radius, 0, 2.0*math.pi)
- cr.fill()
- cr.stroke()
- elif i != 0:
- cr.move_to(stroke[i-1]['x'], stroke[i-1]['y'])
- cr.line_to(point['x'], point['y'])
- cr.stroke()
- def on_quit(self, formulawriter, event):
- """Handle after the user closed the application."""
- Gtk.main_quit()
- def write_guess():
- global classification_guess
- k = PyKeyboard()
- k.press_key(k.control_key)
- k.tap_key('v')
- k.release_key(k.control_key)
- def request_classification(odata, identifier=''):
- """Get the classification for online data.
- :param odata: A list of strokes, where each stroke is a list of dicts.
- :param identifier: Identifier for single users (to improve classification /
- prevent attacks)
- :returns: A list of dictionarys which map class to probability
- """
- url = 'http://i13pc106.ira.uka.de/~mthoma/cgi-bin/test.php'
- payload = {'classify': json.dumps(odata)}
- data = urlencode(payload)
- headers = {'Content-Type': ('application/x-www-form-urlencoded; '
- 'charset=UTF-8'),
- 'Content-Length': len(data)}
- r = requests.post(url, data=data, headers=headers)
- return json.loads(r.text)
- def main():
- FormulaWriter()
- Gtk.main()
- write_guess()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement