-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
54 lines (39 loc) · 1.45 KB
/
Copy pathapp.py
File metadata and controls
54 lines (39 loc) · 1.45 KB
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
# Importing required libs
from flask import Flask, render_template, request
# from model import preprocess_img, predict_result
# from Mo import predict_image_class
# import sys
from PIL import Image
import io
import os
from module import Mo
# sys.path.insert(1, '/module')
# Instantiating flask app
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads/'
app.config['ALLOWED_EXTENSIONS'] = set(['png', 'jpg', 'jpeg'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
# Home route
@app.route("/")
def main():
return render_template("index.html")
# Prediction route
@app.route('/prediction', methods=['POST'])
def predict_image_file():
try:
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = file.filename
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
else:
return render_template("result.html", err="Only .jpg .jpeg .png files are allowed")
pred = Mo.predict_image_class(os.path.join(
app.config['UPLOAD_FOLDER'], filename))
return render_template("result.html", predictions=str(pred))
except Exception as e:
error = "File cannot be processed."
return render_template("result.html", err=error)
if __name__ == "__main__":
app.run(port=9000, debug=True)