Skip to content

Commit af411a0

Browse files
authored
Add files via upload
1 parent db0b695 commit af411a0

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

SimpleNeuralNetwork/test_network.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from __future__ import print_function
2+
from keras.models import load_model
3+
from imutils import paths
4+
import numpy as np
5+
import argparse
6+
import imutils
7+
import cv2
8+
9+
def image_to_feature_vector(image, size=(32, 32)):
10+
return cv2.resize(image, size).flatten()
11+
12+
ap = argparse.ArgumentParser()
13+
ap.add_argument("-m", "--model", required=True,
14+
help="path to output model file")
15+
ap.add_argument("-t", "--test-images", required=True,
16+
help="path to the directory of testing images")
17+
ap.add_argument("-b", "--batch-size", type=int, default=32,
18+
help="size of mini-batches passed to network")
19+
args = vars(ap.parse_args())
20+
21+
CLASSES = ["cat", "dog"]
22+
23+
print("[INFO] loading network architecture and weights...")
24+
model = load_model(args["model"])
25+
print("[INFO] testing on images in {}".format(args["test_images"]))
26+
27+
for imagePath in paths.list_images(args["test_images"]):
28+
print("[INFO] classifying {}".format(
29+
imagePath[imagePath.rfind("/") + 1:]))
30+
image = cv2.imread(imagePath)
31+
features = image_to_feature_vector(image) / 255.0
32+
features = np.array([features])
33+
34+
probs = model.predict(features)[0]
35+
prediction = probs.argmax(axis=0)
36+
37+
label = "{}: {:.2f}%".format(CLASSES[prediction],
38+
probs[prediction] * 100)
39+
cv2.putText(image, label, (10, 35), cv2.FONT_HERSHEY_SIMPLEX,
40+
1.0, (0, 255, 0), 3)
41+
cv2.imshow("Image", image)
42+
cv2.waitKey(0)

0 commit comments

Comments
 (0)