0

How can i load image from keras with local image ? can i ? because im getting error permission access denied? im trying to classification image with tensorflow from local jupyter notebook.

import numpy as np
from keras.preprocessing import image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg


img = image.load_img(os.getcwd()+'/tmp/test/british_cat/', target_size=(150,150))
imgplot = plt.imshow(img)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)

images = np.vstack([x])
classes = model.predict(images, batch_size=10)
# model.summary()
print(classes[0:10])
  

if classes[0][0]==1:
    print('British Cat')
elif classes[0][1]==1:
    print('Love Bird')
elif classes[0][2]==1:
    print('Koi Fish')
else:
    print('error')

the error is :

---------------------------------------------------------------------------
PermissionError                           Traceback (most recent call last)
<ipython-input-49-7b705ffefd08> in <module>
      6 
      7 
----> 8 img = image.load_img(os.getcwd()+'/tmp/test/british_cat/', target_size=(150,150))
      9 imgplot = plt.imshow(img)
     10 x = image.img_to_array(img)

~\miniconda3\envs\myenv\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 

~\miniconda3\envs\myenv\lib\site-packages\keras_preprocessing\image\utils.py in load_img(path, grayscale, color_mode, target_size, interpolation)
    111         raise ImportError('Could not import PIL.Image. '
    112                           'The use of `load_img` requires PIL.')
--> 113     with open(path, 'rb') as f:
    114         img = pil_image.open(io.BytesIO(f.read()))
    115         if color_mode == 'grayscale':

PermissionError: [Errno 13] Permission denied: 'c:\\Users\\AZHAR IE\\Documents\\project\\webscraping google\\Google-Image-Scraper-master/tmp/test/british_cat/
  • Check this https://stackoverflow.com/questions/13207450/permissionerror-errno-13-in-python – Innat Mar 31 '21 at 06:32

1 Answers1

1

You can get the image names by "os.listdir" then you can load images one by one using for loop.

folder= "./tmp/test/british_cat/"
image_names=os.listdir(folder)
for filename in image_names:
    img = tf.keras.preprocessing.image.load_img(folder+filename, target_size=(150, 150))
          
    x=image.img_to_array(img)
    x=np.expand_dims(x, axis=0)
    images = np.vstack([x])
      
    classes = model.predict(images, batch_size=10)