-1

我正在尝试通过使用此代码来使用存储在目录中的图像,

import random

path = "/content/drive/MyDrive/Colab Notebooks/Low Images/*.png"
low_light_images = glob(path)
for image_path in random.choice(low_light_images):
    original_image, output_image = inferer.infer(image_path)
    plot_result(original_image, output_image)

但是遇到这个错误,

---------------------------------------------------------------------------
IsADirectoryError                         Traceback (most recent call last)
<ipython-input-62-668719a88426> in <module>()
      4 low_light_images = glob(path)
      5 for image_path in random.choice(low_light_images):
----> 6     original_image, output_image = inferer.infer(image_path)
      7     plot_result(original_image, output_image)

1 frames
/usr/local/lib/python3.7/dist-packages/PIL/Image.py in open(fp, mode)
   2841 
   2842     if filename:
-> 2843         fp = builtins.open(filename, "rb")
   2844         exclusive_fp = True
   2845 

IsADirectoryError: [Errno 21] Is a directory: '/'

我该如何解决这个问题?完整代码链接:这里

4

1 回答 1

0

线

for image_path in random.choice(low_light_images):

正在抓取一个随机文件路径,例如

for image_path in "/content/drive/MyDrive/Colab Notebooks/Low Images/some_image.png":

当 for 循环开始时image_path将包含第一个字符 a /,您可以看到问题。

要随机循环所有数据,请使用random.shuffle. (最后还有random.choices一个 s random.sample,它将抓取所有图像的随机子集)。

low_light_images = glob(path)
random.shuffle(low_light_images)
for image_path in low_light_images:

如果您可以将问题简化为简短的MVCE ,则最容易调试。使用随机函数时,我将用它输出的创建错误条件的示例替换随机函数,如上所示。我要做的另一件事是,有时我需要检查我的变量是否包含我认为它们包含的数据,因此我会将它们打印出来(或者您可以使用调试器)。这样做我们会看到它image_path包含一个/而不是预期的文件路径。

于 2022-02-05T19:32:54.390 回答