Home > Software design >  Google Colab: No such file or directory on local PC
Google Colab: No such file or directory on local PC

Time:10-18

Good day, friends.

I'm trying to load my own file to Google Colab from my own disc, and I use the code with image.load_img. But programm thinks that there is no such a file. I see this file and not agree with Google. )

Could you please make an advice how can I make code to work correctly. Please tell me if I made any mistake. And what is the right way to type path when file is on PC and file is in Colab folder

Thank you very much.

Code is:

from tensorflow.keras import utils
from tensorflow.keras.preprocessing import image
import numpy as np
import pandas as pd
import pylab
from google.colab import files
from PIL import Image

path = 'C:\XYZ\pic7.jpg'

x = image.load_img(path, target_size = (800, 600), color_mode = 'grayscale')

Colab says:

...

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-43-a3755b9d97b5> in <module>()
      9 path = 'C:\XYZ\pic7.jpg'
      10 
 ---> 11 x = image.load_img(path, target_size = (800, 600), color_mode = 'grayscale')

 1 frames
 /usr/local/lib/python3.7/dist-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':
 
 FileNotFoundError: [Errno 2] No such file or directory: 'C:\\XYZ\\pic7.jpg'

CodePudding user response:

  1. Google Colab runs on a remote server, not your local machine, so it has no access to "C:\" or any of your local drives.

  2. See the examples for how to work with external data in Colab, including mounting Google Drive - so you'll need to put your images there first.

CodePudding user response:

For those who are interested in decision.

  1. I mounted my Google drive, as dicribed at https://colab.research.google.com/notebooks/io.ipynb#scrollTo=u22w3BFiOveA (Aneroid adviced this link),
  2. Changed path to '/content/drive/MyDrive/myfolder/pic7.jpg'.

Anf after that programm stoped to critisize my path. And error changed. It's Success for me. )

Best wishes to Aneroid.

  • Related