-
Notifications
You must be signed in to change notification settings - Fork 1
/
webserver.py
71 lines (53 loc) · 2.14 KB
/
webserver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import argparse
import base64
import os
from fastai.vision import *
from flask import Flask, render_template, request
from flask_ngrok import run_with_ngrok
app = Flask(__name__)
run_with_ngrok(app) # Start ngrok when app is run
UPLOAD_DIR = 'uploads'
OUPUT_FILE = 'output.png'
TITLE_MAPPING = {'digits': "၁၂၃၄",
'alphabets': "ကခ"}
STR_TYPE_MAPPING = {'digits': "နံပါတ်",
'alphabets': "အက္ခရာ"}
def get_args():
parser = argparse.ArgumentParser(description="Web app to recognize hand written Myanmar characters")
parser.add_argument('--type', type=str, help='Type of recognition [digits|alphabets]')
parser.add_argument('--weights', type=str, default=None, help='Saved weight file')
return parser.parse_args()
def create_folders():
if not os.path.isdir(UPLOAD_DIR):
os.makedirs(UPLOAD_DIR)
@app.route('/')
def index():
return render_template('index.html', title=TITLE_MAPPING[args.type],
str_type=STR_TYPE_MAPPING[args.type])
@app.route('/predict', methods=['POST'])
def predict():
parseImage(request.get_data())
# read parsed image back in 8-bit, black and white mode (L)
im = open_image('uploads/output.png')
preds_class, preds_idx, preds_output = learn.predict(im)
class_idx = preds_class.data.item()
return MAPPING[class_idx]
def parseImage(imgData):
# parse canvas bytes and save as output.png
imgstr = re.search(b'base64,(.*)', imgData).group(1)
output_file = '{}/{}'.format(UPLOAD_DIR, OUPUT_FILE)
with open(output_file, 'wb') as output:
output.write(base64.decodebytes(imgstr))
if __name__ == "__main__":
args = get_args()
assert args.type in ['digits', 'alphabets'], "--type should either be 'digits' or 'alphabets'"
create_folders()
if args.weights is None:
learn = load_learner('train', 'export.pkl')
else:
path, filename = os.path.split(args.weights)
learn = load_learner(path, filename)
MAPPING = {v: k for k, v in learn.data.c2i.items()}
app.config['UPLOAD_FOLDER'] = UPLOAD_DIR
app.secret_key = 'supersecret'
app.run()