-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
57 lines (43 loc) · 1.37 KB
/
app.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
import base64
from flask import Flask, jsonify, request, render_template
from io import BytesIO
from network import Network
from utils import load_image, get_networks, get_decision
class CustomFlask(Flask):
jinja_options = Flask.jinja_options.copy()
jinja_options.update(dict(
block_start_string='(%',
block_end_string='%)',
variable_start_string='((',
variable_end_string='))',
comment_start_string='(#',
comment_end_string='#)',
))
app = CustomFlask(__name__)
networks = get_networks()
@app.route('/')
def hello_world():
return render_template('index.html')
@app.route('/recognize/', methods=['POST'])
def recognize_file():
data = request.get_json()
if 'image' not in data:
return jsonify({'msg': 'Image required'}), 400
image = data['image'].split(',')[1]
image_data = load_image(BytesIO(base64.b64decode(image)))
value_list = [net.recognize(image_data) for net in networks]
result = get_decision(value_list)
image_data = Network.get_image_data(image_data)
return jsonify({
'digit': str(result['result']),
'represented': image_data,
'answers': [
{
'index': str(key),
'count': str(value)
}
for key, value in result['answers'].items()
]
})
if __name__ == '__main__':
app.run()