-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
66 lines (56 loc) · 2.1 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
from flask import Flask, render_template, request, redirect, url_for
import time
from werkzeug.utils import secure_filename
import numpy as np
import matplotlib.pyplot as plt
import os
import torch
from classification import predict
def static_clear():
for root, dirs, files in os.walk("static"):
for f in files:
for i in ["JPG", "jpeg", "jpg", "JPEG", "PNG", "png"]:
if i in f.split("."):
try:
os.remove("static/{}".format(f))
print("removed {}".format(f))
except FileNotFoundError:
pass
rooms={'diningroom':'Dining Room','livingroom':'Living Room','kitchen':'Kitchen','bedroom':'Bed Room'}
UPLOAD_FOLDER = "static"
ALLOWED_EXTENSIONS = set(['jpeg', 'jpg', 'JPG', 'JPEG','png','PNG'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/')
def index():
static_clear()
return render_template("home.html")
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
try:
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('resultroom', filename=filename))
else:
return render_template("404.html")
except Exception:
return render_template("404.html")
@app.route('/resultroom/<filename>')
def resultroom(filename):
dataset = 'allrooms'
model = torch.load('_model_24.pt')
result=predict(model,filename)
for i in result:
if result[i]==max(result.values()):
res=rooms[i]
percentage=round(max(result.values())*100,2)
break
return render_template("resultroom.html", name=res, filename=filename,percentage=percentage)
if __name__ == '__main__':
app.run(debug=True)