-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_imagenet.py
More file actions
48 lines (38 loc) · 1.41 KB
/
test_imagenet.py
File metadata and controls
48 lines (38 loc) · 1.41 KB
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
from keras.preprocessing import image as image_utils
from imagenet_utils import decode_predictions
from imagenet_utils import preprocess_input
from vgg16 import VGG16
import argparse
import cv2
import numpy as np
import os
import random
ap = argparse.ArgumentParser()
ap.add_argument("-f", "--folder", required=True)
args = vars(ap.parse_args())
files = [os.path.join(args["folder"], f) for f in os.listdir(args["folder"])]
random.shuffle(files)
# Load the VGG16 network
print("[INFO] loading network...")
model = VGG16(weights="imagenet")
for file in files:
# Load the image using OpenCV
orig = cv2.imread(file)
# Load the image using Keras helper ultility
print("[INFO] loading and preprocessing image...")
image = image_utils.load_img(file, target_size=(224, 224))
image = image_utils.img_to_array(image)
# Convert (3, 224, 224) to (1, 3, 224, 224)
# Here "1" is the number of images passed to network
# We need it for passing batch containing serveral images in real project
image = np.expand_dims(image, axis=0)
image = preprocess_input(image)
# Classify the image
print("[INFO] classifying image...")
preds = model.predict(image)
(inID, label) = decode_predictions(preds)[0]
# Display the predictions
print("ImageNet ID: {}, Label: {}".format(inID, label))
cv2.putText(orig, "Label: {}".format(label), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
cv2.imshow("Classification", orig)
cv2.waitKey(0)