Search code examples
conv-neural-networkpathlibgdrive

Trying to print image count


I am new to Python and I am trying to start CNN for one project. I mounted the gdrive and I am trying to download images from the gdrive directory. After, I am trying to count the images that I have in that directory. Here is my code:

import pathlib
dataset_dir = "content/drive/My Drive/Species_Samples"
data_dir = tf.keras.utils.get_file('Species_Samples', origin=dataset_dir, untar=True)
data_dir = pathlib.Path(data_dir)

image_count = len(list(data_dir('*/*.png')))
print(image_count)

However, I get the following error.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-78-e5d9409807d9> in <module>()
----> 1 image_count = len(list(data_dir('*/*.png')))
      2 print(image_count)

TypeError: 'PosixPath' object is not callable

Can you help, please?

After suggestion, my code looks like this:

import pathlib
data_dir = pathlib.Path("content/drive/My Drive/Species_Samples/")
count = len(list(data_dir.rglob("*.png")))
print(count)

Solution

  • You are trying to glob files you need to use one of the glob methods that pathlib has:

    import pathlib
    
    data_dir = pathlib.Path("/path/to/dir/")
    
    count = len(list(data_dir.rglob("*.png")))
    

    In this case .rglob is a recursive glob.