2

My program is a feature extraction for an image processing program, we are loading the model and extracting features from each image.

  • We load the image from file
  • convert the pixels of image to numpy array
  • extract features
  • store features
  • save to file(pkl)

This is the code snippet.

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

It gives me this error

 ---------------------------------------------------------------------------
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.

I've installed pillow and imported it into the program but it still gives me the same error.

Likith R
  • 21
  • 1
  • 2
  • Can you check in each step if the file path you give actually exists ? – Frodon Nov 30 '20 at 18:43
  • 1
    Also, make sure you have installed Pillow not PIL. Check this answer for more info: https://stackoverflow.com/a/41688942/6025629 – Mike Xydas Nov 30 '20 at 18:59
  • 1
    @Likith R, Did you install `pillow` ? PIL is part of pillow. Install it as `pip install pillow`. Thanks! –  Dec 02 '20 at 14:28

1 Answers1

0

Do "" pip install Image"" that will resolve your error, don't forget to uninstall PIL first