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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
- import os
- import urllib.request
- from flask import Flask, flash, request, redirect, url_for, render_template
- from werkzeug.utils import secure_filename
- from keras.preprocessing.image import load_img
- from keras.preprocessing.image import img_to_array
- from keras.models import load_model
- import numpy as np
- # Constants
- CLASSES = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
- LBL = dict(zip(range(10), CLASSES))
- MODEL_PATH = os.path.join(os.getcwd(), 'saves/model.h5')
- ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
- UPLOAD_FOLDER = os.path.join(os.getcwd(), 'static/uploads')
- app = Flask(__name__)
- app.secret_key = "secret key"
- app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
- def allowed_file(filename):
- return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
- # load and prepare the image
- def load_image(filename):
- # load the image
- img = load_img(os.path.join(UPLOAD_FOLDER, filename), target_size=(32, 32))
- # convert to array
- img = img_to_array(img)
- # reshape into a single sample with 3 channels
- img = img.reshape(1, 32, 32, 3)
- # prepare pixel data
- img = img.astype('float32')
- img = img / 255.0
- return img
- def predict(filename):
- # load the image
- img = load_image(filename)
- # load model
- model = load_model(MODEL_PATH)
- # predict the class
- res = model.predict(img)
- label = np.argmax(res)
- print(res, label)
- label_name = LBL[label]
- return label_name
- @app.route('/')
- def upload_form():
- return render_template('upload.html')
- @app.route('/', methods=['POST'])
- def upload_image():
- if 'file' not in request.files:
- flash('No file part')
- return redirect(request.url)
- file = request.files['file']
- if file.filename == '':
- flash('No image selected for uploading')
- return redirect(request.url)
- if file and allowed_file(file.filename):
- filename = secure_filename(file.filename)
- file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
- flash('Image successfully uploaded and displayed below')
- label = predict(filename)
- flash(f'Prediction: {label.title()}')
- return render_template('upload.html', filename=filename)
- else:
- flash('Allowed image types are -> png, jpg, jpeg, gif')
- return redirect(request.url)
- @app.route('/display/<filename>')
- def display_image(filename):
- return redirect(url_for('static', filename='uploads/' + filename), code=301)
- if __name__ == "__main__":
- app.run()
|