-
Notifications
You must be signed in to change notification settings - Fork 44
/
deploy.py
56 lines (41 loc) · 1.77 KB
/
deploy.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
import os
import requests
from flask import Flask, flash, request, redirect, url_for, render_template, session
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = './static/images'
ALLOWED_EXTENSIONS = ['png', 'jpg', 'jpeg']
app = Flask(__name__, template_folder='template')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.secret_key = "m4xpl0it"
@app.route('/empty_page')
def empty_page():
filename = session.get('filename', None)
os.remove(os.path.join(UPLOAD_FOLDER, filename))
return redirect(url_for('index'))
@app.route('/pred_page')
def pred_page():
pred = session.get('pred_label', None)
f_name = session.get('filename', None)
return render_template('pred.html', pred=pred, f_name=f_name)
@app.route('/', methods=['POST', 'GET'])
def index():
try:
if request.method == 'POST':
f = request.files['bt_image']
filename = str(f.filename)
if filename!='':
ext = filename.split(".")
if ext[1] in ALLOWED_EXTENSIONS:
filename = secure_filename(f.filename)
f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
with open(os.path.join(app.config['UPLOAD_FOLDER'], filename),'rb') as img:
predicted = requests.post("http://localhost:5000/predict", files={"file": img}).json()
session['pred_label'] = predicted['class_name']
session['filename'] = filename
return redirect(url_for('pred_page'))
except Exception as e:
print("Exception\n")
print(e, '\n')
return render_template('index.html')
if __name__=="__main__":
app.run(port=3000)