-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
63 lines (48 loc) · 1.49 KB
/
main.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
from flask import Flask
from flask import redirect, make_response, request
from flask import render_template
from mathreader import api
from mathreader.helpers.exceptions import GrammarError, LexicalError, SintaticError
import os
import inspect
import json
import cv2
import base64
import numpy as np
import re
import time
app = Flask(__name__)
app.config.from_object(os.environ['APP_SETTINGS'])
BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))
@app.route('/', methods=['GET'])
def index():
return render_template('index.html')
@app.route('/ajax/recognize', strict_slashes=False, methods = ['POST', 'OPTIONS'])
def recognize():
latex = ""
error = False
try:
data = request.get_json()
img_data = data['image']
img_data = img_data.split(',')[1]
hme_recognizer = api.HME_Recognizer()
hme_recognizer.load_image(img_data)
latex, modified_img = hme_recognizer.recognize()
hme_recognizer = None
except Exception as e:
if hasattr(e, 'data'):
if 'latex_string_original' in e.data:
latex = e.data['latex_string_original']
error = True
print(e.data)
return json.dumps({
'latex': latex,
'error': error
})
def write_show_image(img, name):
cv2.imwrite('%s.jpg' % name, img)
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__=='__main__':
app.run(debug=True)