Ai
1 Star 1 Fork 0

EricLife/PyImageSearch-CV-DL-CrashCourse

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
test_network.py 1.87 KB
一键复制 编辑 原始数据 按行查看 历史
David Lopera 提交于 2018-11-24 05:31 +08:00 . Day 12 and 13
# import the necessary packages
from __future__ import print_function
from keras.models import load_model
from imutils import paths
import numpy as np
import argparse
import imutils
import cv2
def image_to_feature_vector(image, size=(32, 32)):
# resize the image to a fixed size, then flatten the image into
# a list of raw pixel intensities
return cv2.resize(image, size).flatten()
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-m", "--model", required=True,
help="path to output model file")
ap.add_argument("-t", "--test-images", required=True,
help="path to the directory of testing images")
ap.add_argument("-b", "--batch-size", type=int, default=32,
help="size of mini-batches passed to network")
args = vars(ap.parse_args())
# initialize the class labels for the Kaggle dogs vs cats dataset
CLASSES = ["cat", "dog"]
# load the network
print("[INFO] loading network architecture and weights...")
model = load_model(args["model"])
print("[INFO] testing on images in {}".format(args["test_images"]))
# loop over our testing images
for imagePath in paths.list_images(args["test_images"]):
# load the image, resize it to a fixed 32 x 32 pixels (ignoring
# aspect ratio), and then extract features from it
print("[INFO] classifying {}".format(imagePath[imagePath.rfind("/") + 1:]))
image = cv2.imread(imagePath)
features = image_to_feature_vector(image) / 255.0
features = np.array([features])
# classify the image using our extracted features and pre-trained
# neural network
probs = model.predict(features)[0]
prediction = probs.argmax(axis=0)
# draw the class and probability on the test image and display it
# to our screen
label = "{}: {:.2f}%".format(CLASSES[prediction], probs[prediction] * 100)
cv2.putText(image, label, (10, 35), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), 3)
cv2.imshow("Image", image)
cv2.waitKey(0)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/EricRobots/PyImageSearch-CV-DL-CrashCourse.git
git@gitee.com:EricRobots/PyImageSearch-CV-DL-CrashCourse.git
EricRobots
PyImageSearch-CV-DL-CrashCourse
PyImageSearch-CV-DL-CrashCourse
master

搜索帮助