-1

I am following a tutorial to load data from a local file using Google Colaboratory.

Here is my code:

import numpy as np #To do relay operations
import matplotlib.pyplot as plt #to show image
import os #Iterate through directories and join paths
import cv2 #To do image operations

DATADIR = "C:\\Users\\Family\\Desktop\\Nottingham\\Year 4\\FYP\\Software\\Datasets\\PetImages"
CATEGORIES = ["Dog","Cat"]

for category in CATEGORIES:
  path = os.path.join(DATADIR,category) #path to cats or dogs dir
  print(category)
  print(path)
  for img in os.listdir(path):
    print(img)
    img_array = cv2.imread(os.path.join(path,img), cv2.IMREAD_GRAYSCALE)
    plt.imshow(img_array, cmap = "gray")
    plt.show()
    break
    break

However, I am getting the following error in Line 13:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\Family\Desktop\Nottingham\Year 4\FYP\Software\Datasets\PetImages/Dog'`

I have verified multiple times that this directory indeed exists, yet the same error continues to happen.

I tried changing the format of the directory in DATADIR, by trying these three methods:

DATADIR = "C:\\Users\\Family\\Desktop\\Nottingham\\Year 4\\FYP\\Software\\Datasets\\PetImages"

DATADIR = "C:/Users/Family/Desktop/Nottingham/Year 4/FYP/Software/Datasets/PetImages"

DATADIR = r"C:\Users\Family\Desktop\Nottingham\Year 4\FYP\Software\Datasets\PetImages"

However, none of these methods seemed to work. What else can I try?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Zelreedy
  • 15
  • 4
  • 1
    Google Colab is running in some machine _in the cloud_. It has no access to your files on your `C:\\Users\\Family\\Desktop` on your _local PC_. You need to _upload_ your file to Google Colab (so that it's in the same machine _in the cloud_.) – Gino Mempin Nov 12 '22 at 10:15

1 Answers1

0

If you are on Google collab, just in the left pane go to the directory and right-click, copy path. But if you are on your PC (Not Google Collab) I am not sure, I faced this problem before and this is my solution:

  1. Go to this directory
  2. Open the cmd in this directory and write python

write the following code to get the exact path of this directory:

>>>import os
>>>os.getcwd()

You will get the full path, copy it and use it Hope this helps

Omar
  • 297
  • 5
  • 16