Skip to content

Commit 41637e1

Browse files
committed
binary classifier for cat and dog working
1 parent 1e00d22 commit 41637e1

File tree

5 files changed

+36
-11
lines changed

5 files changed

+36
-11
lines changed

keras_image_classifier_web/flaskr.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import os
22
from keras.models import model_from_json
3-
import sqlite3
4-
import uuid
3+
from PIL import Image
4+
import numpy as np
55

66
from flask import Flask, request, session, g, redirect, url_for, abort, \
77
render_template, flash
88
from flask import send_from_directory
99
from werkzeug.utils import secure_filename
1010

11-
UPLOAD_FOLDER = '../uploads'
11+
UPLOAD_FOLDER = 'uploads'
1212
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
1313

1414
app = Flask(__name__) # create the application instance :)
@@ -18,12 +18,32 @@
1818
app.config.from_envvar('FLASKR_SETTINGS', silent=True)
1919
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
2020

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'))
2323

2424
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
2525

2626

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+
2747
@app.route('/')
2848
def classifiers():
2949
return render_template('classifiers.html')
@@ -63,7 +83,9 @@ def cats_vs_dogs():
6383
@app.route('/cats_vs_dogs_result/<filename>')
6484
def cats_vs_dogs_result(filename):
6585
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)
6789

6890

6991
@app.route('/images/<filename>')

keras_image_classifier_web/models/cnn_bi_classifier_architecture.json

Lines changed: 0 additions & 1 deletion
This file was deleted.
-4.65 MB
Binary file not shown.

keras_image_classifier_web/templates/cats_vs_dogs.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{% extends "layout.html" %}
22
{% block body %}
3+
<p>Upload your Cat or Dog picture and we will try to guess it is a Cat or a Dog</p>
34
<form method=post enctype=multipart/form-data>
45
<p><input type=file name=file>
56
<input type=submit value=Upload>
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{% extends "layout.html" %}
22
{% block body %}
3-
<form method=post enctype=multipart/form-data>
4-
<p><input type=file name=file>
5-
<input type=submit value=Upload>
6-
</form>
3+
<img src="/images/{{ filename }}" />
4+
<br />
5+
<hr />
6+
I guess you have uploaded a {{ predicted_label }} picture, as the probability of this being a dog picture is
7+
{{ probability_of_dog }}
8+
<hr />
9+
<a href="{{ url_for('cats_vs_dogs') }}">Try another picture</a>
710
{% endblock %}

0 commit comments

Comments
 (0)