Reputation:
let us suppose i have a lot of images located in this directory
directory ='/content/drive/My Drive/Colab Notebooks/GAN network/Celebra/img_align_celeba'
i have done following steps in order to read and show all images : step 1 : mount drive
from google.colab import drive
drive.mount('/content/drive')
step 2: create a directory variable
directory ='/content/drive/My Drive/Colab Notebooks/GAN network/Celebra/img_align_celeba'
step 3:create a basic code for reading files
from os import listdir
from numpy import asarray
from PIL import Image
import matplotlib.pyplot as plt
def load_image(filename):
image =Image.open(filename)
image =image.convert('RGB')
pixels =asarray(image)
return pixels
def load_faces(directory,n_faces):
faces =list()
for filename in listdir(directory):
pixesl =load_image(directory + filename)
faces.append(pixels)
if len(faces) >= n_faces:
break
return asarray(faces)
def plot_faces(faces,n):
for i in range(n*n):
plt.subplot(n,n,1+i)
plt.axis('off')
plt.imshow(faces[i])
plt.show()
and final step is to check program :
faces =load_faces(directory,25)
print('Loaded: ', faces.shape)
plot_faces(faces,5)
but it gives me following error :
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-9-3392f98fc252> in <module>()
----> 1 faces =load_faces(directory,25)
2 print('Loaded: ', faces.shape)
3 plot_faces(faces,5)
<ipython-input-8-99365c573bbf> in load_faces(directory, n_faces)
10 def load_faces(directory,n_faces):
11 faces =list()
---> 12 for filename in listdir(directory):
13 pixesl =load_image(directory + filename)
14 faces.append(pixels)
OSError: [Errno 5] Input/output error: '/content/drive/My Drive/Colab Notebooks/GAN network/Celebra/img_align_celeba'
please help me to clarify what is wrong?
Upvotes: 1
Views: 1062
Reputation: 978
I had problems with listdir
too. My solutions were to force the drive mount and to use glob
to list files in the folder.
import glob
from google.colab import drive
drive.mount('/gdrive', force_remount=True)
files = glob.glob(f"/gdrive/My Drive/path_to_folder")
for file in files:
do_something(file)
Upvotes: 1