0

I wanted my image (.png) file in my path to be able to show on a list. So i tried to run this code, but it doesn't seems to work.

path = ('/tmp/BDC-main/images')

classes = os.listdir(path+'/train')

plt.figure(figsize=(30 , 30))
for x in range(10):
    i = random.randint(0,3)                    # getting the class
    images = os.listdir(path+'/train'+'/'+classes[i])
    j = random.randint(0,600)                  # getting the image
    image = cv2.imread(path+'/train'+'/'+classes[i]+'/'+images[j])
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    ax = plt.subplot(5, 5, x + 1)
    plt.imshow(image)
    plt.title(classes[i])
    plt.axis("off")

plt.show()

This is the result :

---------------------------------------------------------------------------
NotADirectoryError                        Traceback (most recent call last)
<ipython-input-29-02bb2a1a65a2> in <cell line: 4>()
      4 for x in range(10):
      5     i = random.randint(0,3)                    # getting the class
----> 6     images = os.listdir(path+'/train'+'/'+classes[i])
      7     j = random.randint(0,600)                  # getting the image
      8     image = cv2.imread(path+'/train'+'/'+classes[i]+'/'+images[j])

NotADirectoryError: [Errno 20] Not a directory: '/tmp/BDC-main/images/train/DataTrain384.png'

What did i do wrong?

  • 1
    You have a file in your `/tmp/BDC-main/images/train` folder and call `os.listdir()` on it. Maybe [filter the contents](https://stackoverflow.com/a/141327/10513287) of `classes` to only have folders. – ivvija Jun 19 '23 at 11:11
  • Thanks for the help, but it's still not working for me. – Rayhan Nuansa Jun 20 '23 at 01:40

0 Answers0