我想测试训练好的模型,我使用keras和opencv,代码如下:
创建参数解析器并传递它们
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to input image we are going to classify")
ap.add_argument("-m", "--model", required=True,
help="path to trained Keras model")
ap.add_argument("-l", "--label-bin", required=True,
help="path to label binarizer")
ap.add_argument("-w", "--width", type=int, default=28,
help="target spatial dimension width")
ap.add_argument("-e", "--height", type=int, default=28,
help="target spatial dimension height")
ap.add_argument("-f", "--flatten", type=int, default=-1,
help="whether or not we should flatten the image")
args = vars(ap.parse_args())
加载输入图像并将其大小更改为所需的大小
image = cv2.imread(args['image'])
output = image.copy()
image = cv2.resize(image, (args['width'], args['height']))
# scaling the pixel values to the range
image = image.astype("float") / 255.0
# check whether you need to smooth the image and add the batch size
if args['flatten'] > 0:
image = image.flatten()
image = image.reshape((1, image.shape))
# otherwise, we work with CNN -- don't smooth the image
# and just add the package size
else:
image = image.reshape((1, image.shape, image.shape,image.shape))
print(" loading network and label binarizer...")
model = load_model(args['model'])
lb = pickle.loads(open(args['label-bin'], "rb").read())
# recognizing the image
preds = model.predict(image)
# finding the index of the class label with the highest probability
# matches
i = preds.argmax(axis=1)
label = lb.classes_
text = "{}: {:.2f}%".format(label, preds * 100)
cv2.putText(output, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
(0, 0, 255), 2)
# showing the output image
cv2.imshow("Image", output)
cv2.waitKey(0)
但我收到以下错误:
image = image.change the shape((1, Image.form, image.form,image.form))
TypeError: the 'tuple' object cannot be interpreted as an integer
我得到同样的东西if args['flatten'] > 0
有人可以帮助您解决问题吗?