Commits

Owen Nelson  committed 626743b

initial checkin

  • Participants

Comments (0)

Files changed (4)

+.idea
+.venv
+build
+*.iml

File canvas-test.py

+import json
+from flask import Flask, request, render_template
+import zinnia
+
+
+app = Flask(__name__)
+recognizer = zinnia.Recognizer()
+recognizer.open('/usr/local/lib/zinnia/model/tomoe/handwriting-ja.model')
+
+
+@app.route('/')
+def front():
+    return render_template('index.html')
+
+
+@app.route('/read-curves', methods=['POST', 'GET'])
+def read_curves():
+    curves = json.loads(request.form['curves'])
+    width = request.form['width']
+    height = request.form['height']
+
+    char = zinnia.Character()
+    char.set_height(height)
+    char.set_height(width)
+    for (idx, x, y) in (
+            (idx, x, y)
+            for (idx, data) in enumerate(curves)
+            for (x, y) in data):
+        char.add(idx, x, y)
+
+    result = recognizer.classify(char, len(curves))
+
+    if not result:
+        return recognizer.what()
+    else:
+        return repr([res for res in result])
+
+@app.errorhandler(500)
+def derp(error):
+    return repr(error)
+
+
+if __name__ == '__main__':
+    app.run()

File requirements.txt

+Flask==0.10.1
+ipython==1.2.1
+zinnia-python==0.0.0

File templates/index.html

+<!DOCTYPE html>
+<html>
+  <head>
+    <style type="text/css">
+      form {
+        display: block;
+        clear: both;
+      }
+
+      .cols {
+        width: 50%;
+        float: left;
+      }
+
+      #input, #output {
+        height: 400px;
+        padding: 20px;
+        width: 100%;
+      }
+      #output {
+        background: cornsilk;
+
+      }
+      #input  {
+        background: #f5f5f5;
+      }
+
+    </style>
+  </head>
+  <body>
+  <form action="/read-curves" method="post">
+    <input type="text" name="curves"/>
+    <input type="text" name="height" value="200"/>
+    <input type="text" name="width" value="200"/>
+    <input type="submit">
+  </form>
+
+  <div class="cols">
+    <pre id="output">
+    </pre>
+  </div>
+  <div class="cols">
+    <canvas id="input"></canvas>
+  </div>
+
+  <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
+  <script>
+    'use strict';
+
+    $(document).ready(function (){
+      $('form').submit(function () {
+        $.post($(this).attr('action'), $(this).serializeArray(), function (resp) {
+          $('#output').text(resp);
+        });
+        return false;
+      });
+
+      function getMousePos(canvas, evt) {
+        var rect = canvas.getBoundingClientRect();
+        return {
+          x: evt.clientX - rect.left,
+          y: evt.clientY - rect.top
+        };
+      }
+
+
+      var canvas = $('#input').get(0),
+          context = canvas.getContext('2d'),
+          drawing = false;
+
+      canvas.addEventListener('mousemove', function(event) {
+        if (drawing) {
+          console.log(event);
+          console.log(getMousePos(canvas, event));
+        }
+      });
+      canvas.addEventListener('mousedown', function(event) {
+        drawing = true;
+      });
+      canvas.addEventListener('mouseup', function(event) {
+        drawing = false;
+      });
+
+
+      canvas.addEventListener('touchmove', function(event) {
+        event.preventDefault();
+        if (drawing) {
+          console.log(event);
+          console.log(getMousePos(canvas, event.touches[0]));
+        }
+      }, false);
+      canvas.addEventListener('touchstart', function(event) {
+        drawing = true;
+      });
+      canvas.addEventListener('touchend', function(event) {
+        drawing = false;
+      });
+      canvas.addEventListener('touchcancel', function(event) {
+        drawing = false;
+      });
+
+    });
+  </script>
+  </body>
+</html>