-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
78 lines (63 loc) · 2.66 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
from werkzeug.utils import secure_filename
from flask import Flask, render_template, request, jsonify
import json
from utility import decode_image, stringify
from inference_core import inference_engine
inference_instance = inference_engine()
UPLOAD_FOLDER = os.getcwd() + '/upload'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
# app = Flask(__name__, static_url_path='')
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
basedir = os.path.abspath(os.path.dirname(__file__))
def allowed_file(filename):
"""
Checks to make sure sent image has a valid filetype
"""
return '.' in filename and \
filename.rsplit('.', 1)[-1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def home():
"""
Serves the index.html
"""
return render_template('index.html')
# Sample command to POST request to this route:
# curl -X POST -F "[email protected]" http://0.0.0.0:5000/uploadajax
@app.route('/uploadajax', methods=["GET", "POST"])
def uploadfile():
if request.method == 'POST':
files = request.files['file']
print(files.filename, flush=True)
# check validity of file
if files and allowed_file(files.filename):
filename = secure_filename(files.filename)
print(filename, flush=True)
updir = os.path.join(basedir, 'upload/')
image_path = os.path.join(updir, filename)
files.save(image_path)
file_size = os.path.getsize(os.path.join(updir, filename))
else:
app.logger.info('ext name error')
return jsonify(error='ext name error')
image = decode_image(image_path) # read in the image
predictions = inference_instance.get_class_probabilities(image)
return stringify(predictions)
@app.route('/radio_button', methods=["GET", "POST"])
def radio_selection():
if request.method == 'POST':
selection = json.loads(request.get_data(as_text=True))['radio_sel'] # converts json byte string to dict
print(selection)
image_path = None
if selection=='1': #(cat)
image_path = os.path.join(os.getcwd(), 'static', 'images', 'n02121808_1421_domestic_cat.jpg')
elif selection=='2': # (whale)
image_path = os.path.join(os.getcwd(), 'static', 'images', 'grey_whale.jpeg')
elif selection=='3': # (dog)
image_path = os.path.join(os.getcwd(), 'static', 'images', 'n02084071_1365_dog.jpg')
image = decode_image(image_path) # read the image
predictions = inference_instance.get_class_probabilities(image)
return stringify(predictions)
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')