简体   繁体   English

尝试打印图像计数

[英]Trying to print image count

I am new to Python and I am trying to start CNN for one project.我是 Python 的新手,我正在尝试为一个项目启动 CNN。 I mounted the gdrive and I am trying to download images from the gdrive directory.我安装了 gdrive 并尝试从 gdrive 目录下载图像。 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)

You are trying to glob files you need to use one of the glob methods that pathlib has:您正在尝试使用pathlib具有的 glob 方法之一来 glob 文件:

import pathlib

data_dir = pathlib.Path("/path/to/dir/")

count = len(list(data_dir.rglob("*.png")))

In this case .rglob is a recursive glob.在这种情况下.rglob是一个递归 glob。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM