用vgg16模型进行单张图片或多张识别

本文介绍了如何使用VGG16预训练模型在TensorFlow中对单张或多张图片进行识别,包括加载图片、预处理和预测概率的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用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开始

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值