解决AttributeError: module ‘scipy.ndimage‘ has no attribute ‘imread‘报错

解决AttributeError: module 'scipy.ndimage' has no attribute 'imread'报错

报错原因是因为,imread 已弃用!imread 在 SciPy 1.0.0 中已弃用,并将在 1.2.0 中删除。

ndimage.imread(fname)
#ndimage.imread已经弃用可以用如下的方式替代

#第一种,利用imageio库
imageio.imread(fname)
#第二种,利用matplotlib库
plt.imread(fname)

综合应用如下:(代码因为你们没有模型运行不了,重在看思想,看新版本的代码如何使用,然后应用到自己的代码上)

import imageio.v2 as imageio
from skimage.transform import resize

my_image = "my_image5.jpg"
my_label_y = [1]

fname = "images/" + my_image
# image = np.array(plt.imread(fname))  #装载图片,读取图片
image = np.array(imageio.imread(fname))  #装载图片,读取图片

my_image = resize(image, output_shape=(num_px, num_px, 3)).reshape(
    (num_px * num_px * 3, 1))  #调整图片数据为确定的尺寸,并通过reshape格式数据维度,以便传入模型。
my_predicted_image = predict(my_image, my_label_y, parameters) #使用了预测函数,自己编写的,parameters是模型得到的参数

plt.rcParams['figure.figsize'] = (5.0, 5.0)
plt.axis('off')
plt.imshow(image)
print("y = " + str(np.squeeze(my_predicted_image)) +
      ", your L-layer model predicts a \"" +
      classes[int(np.squeeze(my_predicted_image)), ].decode("utf-8") +
      "\" picture.")

在此提供一种全新的加载图片的方式,使用keras.preprocessing.image的load_img()函数。

from keras.preprocessing import image

img_path = 'images/smail01.png'    #图片路径
img = image.load_img(img_path, target_size=(64, 64))   #使用 load_img 函数加载图像文件,使用的包from keras.preprocessing.image import load_img, img_to_array
imshow(img)   #plt.imshow(img);   import matplotlib.pyplot as plt

x = image.img_to_array(img)       #将其转换为 Keras 可以处理的张量格式
# print(x.shape)#(64, 64, 3)
x = np.expand_dims(x, axis=0)    #将数组 `x` 在轴0上扩展,即在数组的最外层添加一个新的维度。
# print(x.shape)#(1, 64, 64, 3)
x = preprocess_input(x)     #对图像进行预处理;   from keras.applications.imagenet_utils import preprocess_input

print(happyModel.predict(x))    #预测
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值