-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDog-vs-Cat-Predictor.py
58 lines (41 loc) · 1.34 KB
/
Dog-vs-Cat-Predictor.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
import cv2
import numpy as np
from tensorflow import keras
import os
import matplotlib.pyplot as plt
# Processes input images
folder = "prediction_inputs"
images = []
processed_images = []
for file in os.listdir(folder):
image = cv2.imread(os.path.join(folder, file))
if image is not None:
rescaled = cv2.resize(image, (256, 256))/255.0
images.append(image)
processed_images.append(rescaled)
processed_images = np.array(processed_images)
# Loads pre-trained model
model = keras.models.load_model('Dogs-vs-Cats_model.h5')
# Feeds input data to model
predictions = model.predict(processed_images)
# Displays image and prediction
plt.ion()
plt.rcParams['toolbar'] = 'None'
plt.rcParams['font.size'] = 18
plt.rcParams['figure.figsize'] = [8.0, 6.0]
for index in range(len(images)):
plt.figure("Dog vs Cat Classifier")
plt.xticks([])
plt.yticks([])
if predictions[index][0] > 0.5:
title = "Prediction: Dog \n"
x_label = "\n Confidence: {:5.2f}%".format(200*(predictions[index][0]-0.5))
else:
title = "Prediction: Cat \n"
x_label = "\n Confidence: {:5.2f}%".format(200*(0.5-predictions[index][0]))
plt.title(title)
plt.xlabel(x_label)
plt.imshow(cv2.cvtColor(images[index], cv2.COLOR_BGR2RGB))
plt.show()
if plt.waitforbuttonpress():
plt.close()