首先网上找了7张图

打算对这7张图片进行识别,直接上代码:
from tensorflow.keras import applications
#decode_predictions 对预测结果进行处理,解码
from tensorflow.keras.applications.vgg16 import decode_predictions
#preprocess_input处理输入图片
from tensorflow.keras.applications.vgg16 import preprocess_input
from tensorflow import keras
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.preprocessing.image import load_img
#VGG 图片输入大小要求 224*224*3
#VGG 有1.4亿个参数
#获取已训练好的模型
def predict(my_model,images):
y_lables = []
for image in images:
y_predictions = my_model.predict(image)
# print(y_predictions)
y_lable = decode_predictions(y_predictions)
y_lables.append(y_lable)
return y_lables
#图片读取处理
def load_img1(files_path):
images=[]
#加载图片
for path in files_path:
image = load_img(path, target_size=(224,224))
#图片进行数组转换
image = img_to_array(image)
print('-----')
print(image.shape)
#形状修改,输入到卷积需要四维[1,224,224,3],1代表batch,如果多张图片就使用多张
image = image.reshape(1, image.shape[0], image.shape[1], image.shape[2])
print(image.shape)
# print(image)
#对图片进行归一化处理
images.append(preprocess_input(image))
return images
#输入到模型进行预测
if __name__ == '__main__':
import os
#获取当前路径
path = os.getcwd()
files=os.listdir(os.path.join(path,"tiger"))
file_count = len(files)
print(file_count)
files_path=[]
for i in range(file_count):
print(i)
files_path.append(os.path.join(path,'tiger',files[i]))
print(files_path)
my_model = applications.vgg16.VGG16()
images = load_img1(files_path)
print('123')
y_predicts = predict(my_model,images)
for y_predict in y_predicts:
print('预测结果是 %s,预测概率为 %.2f'%(y_predict[0][0][1],y_predict[0][0][2]*100),"%")
图片识别结果:非常完美,全部识别正确,而且狗的分类很详细
预测结果是 tiger,预测概率为 71.90 %
预测结果是 tiger,预测概率为 60.39 %
预测结果是 tabby,预测概率为 33.14 %
预测结果是 tabby,预测概率为 8.50 %
预测结果是 English_foxhound,预测概率为 61.34 %
预测结果是 Afghan_hound,预测概率为 51.58 %
预测结果是 Labrador_retriever,预测概率为 40.17 %
文件结构:


2264

被折叠的 条评论
为什么被折叠?



