|
1 | 1 | import os |
2 | 2 | from keras.models import model_from_json |
3 | | -import sqlite3 |
4 | | -import uuid |
| 3 | +from PIL import Image |
| 4 | +import numpy as np |
5 | 5 |
|
6 | 6 | from flask import Flask, request, session, g, redirect, url_for, abort, \ |
7 | 7 | render_template, flash |
8 | 8 | from flask import send_from_directory |
9 | 9 | from werkzeug.utils import secure_filename |
10 | 10 |
|
11 | | -UPLOAD_FOLDER = '../uploads' |
| 11 | +UPLOAD_FOLDER = 'uploads' |
12 | 12 | ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif']) |
13 | 13 |
|
14 | 14 | app = Flask(__name__) # create the application instance :) |
|
18 | 18 | app.config.from_envvar('FLASKR_SETTINGS', silent=True) |
19 | 19 | app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 |
20 | 20 |
|
21 | | -model = model_from_json(open(os.path.join('models', '/cnn_bi_classifier_architecture.json')).read()) |
22 | | -model.load_weights(os.path.join('models', '/cnn_bi_classifier_weights.h5')) |
| 21 | +model = model_from_json(open(os.path.join('../keras_image_classifier/models', 'cnn_bi_classifier_architecture.json')).read()) |
| 22 | +model.load_weights(os.path.join('../keras_image_classifier/models', 'cnn_bi_classifier_weights.h5')) |
23 | 23 |
|
24 | 24 | model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) |
25 | 25 |
|
26 | 26 |
|
| 27 | +def predict(filename): |
| 28 | + img = Image.open(filename) |
| 29 | + img = img.resize((150, 150), Image.ANTIALIAS) |
| 30 | + |
| 31 | + input = np.asarray(img) |
| 32 | + input = np.expand_dims(input, axis=0) |
| 33 | + |
| 34 | + print(input.shape) |
| 35 | + |
| 36 | + output = model.predict(input) |
| 37 | + |
| 38 | + probability_of_a_dog = output[0][0] |
| 39 | + predicted_label = ("Dog" if probability_of_a_dog > 0.5 else "Cat") |
| 40 | + return probability_of_a_dog, predicted_label |
| 41 | + |
| 42 | +prob, label = predict('../keras_image_classifier/bi_classifier_data/training/cat/cat.2.jpg') |
| 43 | +print(prob) |
| 44 | +print(label) |
| 45 | + |
| 46 | + |
27 | 47 | @app.route('/') |
28 | 48 | def classifiers(): |
29 | 49 | return render_template('classifiers.html') |
@@ -63,7 +83,9 @@ def cats_vs_dogs(): |
63 | 83 | @app.route('/cats_vs_dogs_result/<filename>') |
64 | 84 | def cats_vs_dogs_result(filename): |
65 | 85 | filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) |
66 | | - return render_template('cats_vs_dogs_result.html', filename=filename) |
| 86 | + probability_of_dog, predicted_label = predict(filepath) |
| 87 | + return render_template('cats_vs_dogs_result.html', filename=filename, |
| 88 | + probability_of_dog=probability_of_dog, predicted_label=predicted_label) |
67 | 89 |
|
68 | 90 |
|
69 | 91 | @app.route('/images/<filename>') |
|
0 commit comments