用vgg16模型进行单张图片
from tensorflow.keras.applications.vgg16 import VGG16,preprocess_input,decode_predictions
from tensorflow.keras.preprocessing import image
import numpy as np
model = VGG16(weights='imagenet',include_top=True)
img_path = "3.jpg"
img = image.load_img(img_path,target_size = (224,224))
x = image.img_to_array(img)
x=np.expand_dims(x,axis=0)
x=preprocess_input(x)
y_pred = model.predict(x)
print("测试图",decode_predictions(y_pred))
print("预测该图为:",decode_predictions(y_pred)[0][0][1],"概率为:",decode_predictions(y_pred)[0][0][2])
第五行需要自己改路径,放图片
用vgg16模型进行多张图片识别(以下代码案例为5张)
from tensorflow.keras.applications.vgg16 import VGG16,preprocess_input,decode_predictions
from tensorflow.keras.preprocessing import image
import numpy as np
model = VGG16(weights='imagenet',include_top=True)
for i in range(5):
img_path = ("C:/Users/Administrator/"+str(i)+".jpg")
img = image.load_img(img_path,target_size = (224,224))
x = image.img_to_array(img)
x=np.expand_dims(x,axis=0)
x=preprocess_input(x)
y_pred = model.predict(x)
# print("测试图",decode_predictions(y_pred)[0][0][1])
print("第",i+1,"预测该图为:",decode_predictions(y_pred)[0][0][1],"概率为:",decode_predictions(y_pred)[0][0][2])
第六行需要改路径,且图片名称必须为0.jpg-4.jpg
for循环图片需要从0开始