卷积神经网络的知识此处不再赘述 如不有疑问可以参考这篇文章CNN卷积神经网络
接下来的代码使用VGGNET模型去识别不同图片的特征,我们将使用ImageNet数据集上训练完成的RexNet-50模型去识别现实生活的实物 如动物等...
原图片如下
识别源码如下
from tensorflow import keras from keras.applications.resnet import ResNet50 from keras.preprocessing import image from keras.applications.resnet import preprocess_input,decode_predictions import numpy as np import cv2 from PIL import ImageFont,ImageDraw,Image img1=r'C:dog.jpg' img2=r'C:cat.jpg' img3=r'C:deer.jpg' weight_path=r'C:\Users\Admin\Desktop\resnet50_weights_tf_dim_ordering_tf_kernels.h5' img=image.loda_img(img1,target_size=(224,224)) x=image.img_to_array(img) x=np.expand_dims(x,axis=0) x=preprocess_input(x) def get_model(): model=ResNet50(weights=weight_path) print(model.summary()) return model model=get_model() #预测图片 preds=model.predict(x) #打印出top-5的结果 print('predicted',decode_predictions(preds,top=5)[0]) #使用dog照片预测的结果 Predicted:[('n02108422','bull_mastiff','0.366562'),('n02110958','pug','0.3122419'),('n02093754','Border_terrier','0.16009717'), ('n02108915','French_bulldog','0.049768772'),('n02099712','Labrador_retriever','0.04569989')] #使用猫照片预测的结果 Predicted:[('n02123045','tabby','0.92873186'),('n021204075','Eqyptian_cat','0.02793644'),('n02123159','tiger_cat','0.021263707'), ('n04493381','tub','0.0037994932'),('n04040759','radiator','0.0017052109')] #使用🦌照片预测的结果 Predicted:[('n02422699','impala','0.30113414'),('n02417914','ibex','0.28613034'),('n02423022','gazelle','0.26537818'), ('n02422106','hartebeest','0.031009475'),('n02412080','ram','0.030351655')]