-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
72 lines (59 loc) · 2.39 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
# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing import image
import numpy as np
from flask import Flask, render_template, request, flash, redirect, url_for
from werkzeug.utils import secure_filename
import os
print(tf.__version__)
ALLOWED_EXT = set(['png', 'jpg', 'jpeg'])
BASE = os.path.abspath(os.path.dirname(__file__))
UPLOAD_FOLDER = os.path.join(BASE, 'static/upload')
CLASSES = np.array(['Buildings', 'Forest' ,'Glacier' ,'Mountain' ,'Sea' ,'Street'])
app = Flask(__name__)
app.secret_key = "marj-app"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16*1024*1024
model = None
def get_model():
global model
if model == None:
model = keras.models.load_model(os.path.join(BASE, 'model/model.h5'))
return model
def allowed_file(filename):
if '.' in filename and filename.split('.')[-1].lower() in ALLOWED_EXT:
return True
return False
def load_image(img_path):
img = image.load_img(img_path, target_size=(150, 150))
img_array = image.img_to_array(img)
img_batch = np.expand_dims(img_array, axis=0)
return img_batch
def classify(img_path):
imgs = load_image(img_path)
model = get_model()
result = model.predict(imgs)
return result
@app.route('/')
def index():
return render_template('main.html', pred=None, image=None)
@app.route('/', methods=['POST'])
def upload_image():
for file in os.listdir(UPLOAD_FOLDER):
os.remove(os.path.join(app.config['UPLOAD_FOLDER'], file))
if 'file' not in request.files:
return render_template('main.html', pred="No file part", image=None)
file = request.files['file']
if file.filename == '':
return render_template('main.html', pred="No Image selected form uploading", image=None)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
result = classify(os.path.join(app.config['UPLOAD_FOLDER'], filename))
image_url = url_for('static', filename='upload/' + filename)
return render_template('main.html', pred=str('Class: {}, Confidence: {}'.format(CLASSES[result.argmax()], result.max())), image=image_url)
else:
return render_template('main.html', pred="Format not supported", image=None)
if __name__ == '__main__':
app.run(debug=True)