Is cv2.imread trying to read the folder and not the images in it?

631 Views Asked by At
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2

DATADIR = "C:\\Users\\Timmi K. Rysgaard\\OneDrive\\Desktop\\Sculptures\\"
CATEGORIES = ["Beetle", "Nerd"]

for category in CATEGORIES:
    path = os.path.join(DATADIR,category) # path to nerd or beetle
    for img in os.listdir(path):
        img_array = cv2.imread(os.path.join(path,img) , 0)
        plt.imshow(img_array, cmap="gray")
        plt.show()
        
        break
    break 

I am receiving:

  • TypeError: Image data of dtype object cannot be converted to float

Could I be trying to imread the folder, and if so, how do I fix it?

1

There are 1 best solutions below

0
On

You probably have other files or directories in the same folder. You could try limiting the file types to be accepted using something like this:

import numpy as np
import matplotlib.pyplot as plt
import os
import cv2

DATADIR = "C:\\Users\\Timmi K. Rysgaard\\OneDrive\\Desktop\\Sculptures\\"
CATEGORIES = ["Beetle", "Nerd"]

for category in CATEGORIES:
    path = os.path.join(DATADIR, category)  # path to nerd or beetle
    for img in os.listdir(path):
        if img.endswith((".png", ".jpg", ".jpeg")):
            img_array = cv2.imread(os.path.join(path, img), 0)
            plt.imshow(img_array, cmap="gray")
            plt.show()
            plt.clf()