解决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)) #预测