【发布时间】:2020-11-30 18:30:01
【问题描述】:
我的程序是一个图像处理程序的特征提取,我们正在加载模型并从每个图像中提取特征。
- 我们从文件中加载图片
- 将图像的像素转换为 numpy 数组
- 提取特征
- 商店功能
- 保存到文件(pkl)
这是代码 sn-p。
from os import listdir
from pickle import dump
from keras.applications.inception_v3 import InceptionV3
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.inception_v3 import preprocess_input
from keras.models import Model
import string
from timeit import default_timer as timer
from PIL import Image
import sys
for name in listdir(directory):
# load an image from file
filename = directory + '/' + name
image = load_img(filename, target_size=(230,230))
# convert the image pixels to a numpy array
image = img_to_array(image)
# reshape data for the model
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
# prepare the image for the VGG model
image = preprocess_input(image)
# get features
feature = model.predict(image, verbose=0)
# get image id
image_id = name.split('.')[0]
# store feature
features[image_id] = feature
#print('>%s' % name)
return features
它给了我这个错误
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-5-566a23558a68> in <module>
46 # extract features from all images
47 directory = 'data/caption/1'
---> 48 features = extract_features(directory)
49 #print('Extracted Features: %d' % len(features))
50 # save to file
<ipython-input-5-566a23558a68> in extract_features(directory)
27 # load an image from file
28 filename = directory + '/' + name
---> 29 image = load_img(filename, target_size=(230,230))
30 # convert the image pixels to a numpy array
31 image = img_to_array(image)
c:\users\likith\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\preprocessing\image.py in load_img(path, grayscale, color_mode, target_size, interpolation)
298 ValueError: if interpolation method is not supported.
299 """
--> 300 return image.load_img(path, grayscale=grayscale, color_mode=color_mode,
301 target_size=target_size, interpolation=interpolation)
302
c:\users\likith\appdata\local\programs\python\python38\lib\site-packages\keras_preprocessing\image\utils.py in load_img(path, grayscale, color_mode, target_size, interpolation)
109 color_mode = 'grayscale'
110 if pil_image is None:
--> 111 raise ImportError('Could not import PIL.Image. '
112 'The use of `load_img` requires PIL.')
113 with open(path, 'rb') as f:
ImportError: Could not import PIL.Image. The use of `load_img` requires PIL.
我已经安装了枕头并将其导入程序,但它仍然给我同样的错误。
【问题讨论】:
-
您提供的文件路径是否确实存在,您可以检查每个步骤吗?
-
另外,请确保您安装了 Pillow 而不是 PIL。检查此答案以获取更多信息:stackoverflow.com/a/41688942/6025629
-
@Likith R,你安装了
pillow吗? PIL 是枕头的一部分。将其安装为pip install pillow。谢谢!
标签: python tensorflow keras python-imaging-library jupyter-lab