导入错误:无法导入 PIL.Image。使用`load_img`需要PIL。即使使用枕头库之后

问题描述 投票:0回答:1

我的程序是图像处理程序的特征提取,我们正在加载模型并从每个图像中提取特征。

  • 我们从文件加载图像
  • 将图像的像素转换为 numpy 数组
  • 提取特征
  • 店铺特色
  • 保存到文件(pkl)

这是代码片段。

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并将其导入到程序中,但它仍然给我同样的错误。

python tensorflow keras python-imaging-library jupyter-lab
1个回答
0
投票

执行“pip install Image”即可解决您的错误,不要忘记先卸载 PIL

© www.soinside.com 2019 - 2024. All rights reserved.