import keras
#import tensorflow
from keras.applications.resnet_v2 import preprocess_input,decode_predictions
from keras.preprocessing import image
import numpy as np
import matplotlib.pyplot as plt
def classify(img_path):
'''图片加载器,将目标图片标准化为我们想要的格式,这里是224*224像素的图片'''
img = image.load_img(img_path,target_size=(224,224))
plt.imshow(img)
plt.show()
'''到服务器将所需要的模型缓存'''
model = keras.applications.resnet_v2.ResNet50V2()
'''
大多深度学习模型都需要一批图像,我们现在只有一张图,我们需要将三维拓展为四维。
这边image.img_to_array将图片转换为矩阵数值,即224*224*3
'''
img_array = image.img_to_array(img)
print(np.shape(img_array))
'''np.extend_dims扩维操作,axis表示扩第几维'''
img_batch = np.expand_dims(img_array,axis=0)
'''preprocess_input()接受神经网络的输入数据,矩阵形式'''
img_preprocessed = preprocess_input(img_batch)
prediction = model.predict(img_preprocessed)
'''解析预测结果'''
print(decode_predictions(prediction,top=3)[0])
classify('maomao.JPG')
附上我家毛毛图片



