-
Notifications
You must be signed in to change notification settings - Fork 12
/
not_hotdog.py
59 lines (48 loc) · 1.73 KB
/
not_hotdog.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
import os
from flask import Flask, redirect, url_for, request, render_template, send_from_directory
from werkzeug import secure_filename
import not_hotdog_model
# todo: more pretty interface
# folder to upload pictures
UPLOAD_FOLDER = 'uploads/'
# what files can upload
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
# start + config
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['ALLOWED_EXTENSIONS']=ALLOWED_EXTENSIONS
# main route
@app.route('/')
def index():
return render_template('upload.html')
# is file allowed to be uploaded?
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
# result of model prediction
@app.route('/classification/<result>')
def classification(result):
return render_template('result.html', result=result)
# file upload route
@app.route('/upload', methods=['POST'])
def upload():
# Get the name of the uploaded file
file = request.files['file']
# Check if the file is one of the allowed types/extensions
if file and allowed_file(file.filename):
# remove unsupported chars etc
filename = secure_filename(file.filename)
#save path
save_to=os.path.join(app.config['UPLOAD_FOLDER'], filename)
#save file
file.save(save_to)
#pass file to model and return bool
is_hotdog=not_hotdog_model.is_hotdog(save_to)
#show if photo is a photo of hotdog
return redirect(url_for('classification', result=is_hotdog))
#file show route (not using now)
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
if __name__ == '__main__':
app.run(debug=True)