How to access images from Google Drive folders in Colab notebook

I’m working on a machine learning project and need help accessing image files stored in my Google Drive from a Colab environment. My folder structure looks like this: My Drive > ML Projects > CNN_Project > petImages > training > Golden_Retriever > Golden_Retriever01.jpg. The petImages directory contains three main subdirectories (training, testing, validation). Each subdirectory has multiple folders named after different dog breeds, and each breed folder contains the actual image files. I want to load all these images from the various subdirectories and store them in a single variable called “pet_images_data” for processing. What’s the best way to mount my Google Drive and access these nested folders in Colab? Any code examples would be really helpful.

first, mount your gdrive with from google.colab import drive and then run drive.mount('/content/drive'). to get your images, try using os.listdir() or os.walk() for accessing those nested folders easily.

I’ve handled similar nested folder setups for computer vision projects. After mounting your drive, use the glob module - it’s perfect for pattern matching with this structure. Import glob, then run image_paths = glob.glob('/content/drive/MyDrive/ML Projects/CNN_Project/petImages/**/*.jpg', recursive=True) to grab all jpg files from every subdirectory at once. Loop through the paths to load images with opencv or PIL. The recursive parameter is crucial - it automatically traverses all those nested breed folders. Way cleaner than manually walking through each directory level, especially with multiple breeds and split folders.

Try using pathlib instead - it gives you way more control over file paths. After mounting your drive, import pathlib and create a Path object for your petImages directory. Then run Path('/content/drive/MyDrive/ML Projects/CNN_Project/petImages').rglob('*.jpg') to grab all jpg files recursively. The cool thing is you can pull metadata like breed names and split types straight from the path components while loading images. I found this super helpful for building training labels - just parse the parent directory names from the path object to get breed classifications and figure out if the image goes in training, testing, or validation.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.