利用keras加载训练好的.H5文件,并实现预测图片

import matplotlib
matplotlib.use('Agg')
import os
from keras.models import load_model
import numpy as np
from PIL import Image
import cv2
#加载模型h5文件
model = load_model("C:\\python\\python3_projects\\cat_dog\\cats_dogs_fifty_thousand.h5")
model.summary()
#规范化图片大小和像素值
def get_inputs(src=[]):
  pre_x = []
  for s in src:
    input = cv2.imread(s)
    input = cv2.resize(input, (150, 150))
    input = cv2.cvtColor(input, cv2.COLOR_BGR2RGB)
    pre_x.append(input) # input一张图片
  pre_x = np.array(pre_x) / 255.0
  return pre_x
#要预测的图片保存在这里
predict_dir = 'C:\python\python3_projects\cat_dog\pics'
#这个路径下有两个文件,分别是cat和dog
test = os.listdir(predict_dir)
#打印后:['cat', 'dog']
print(test)
#新建一个列表保存预测图片的地址
images = []
#获取每张图片的地址,并保存在列表images中
for testpath in test:
  for fn in os.listdir(os.path.join(predict_dir, testpath)):
    if fn.endswith('jpg'):
      fd = os.path.join(predict_dir, testpath, fn)
      print(fd)
      images.append(fd)
#调用函数,规范化图片
pre_x = get_inputs(images)
#预测
pre_y = model.predict(pre_x)
print(pre_y)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大富大贵7

很高兴能够帮助到你 感谢打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值