【问题标题】:How I can display images in file?如何在文件中显示图像?
【发布时间】:2020-12-25 13:02:52
【问题描述】:

当我运行它时,我收到了这个错误(在代码下方)

DATADIR =r"C:\Users\Müco\Desktop\YAZILIM\kaggle_deeplearning\cat-dog\PetImages"
CATEGORIES =['Cat','Dog']

for category in CATEGORIES:
path =os.path.join(DATADIR,category)
for img in os.listdir(path):
    img_path =os.path.join(path,img)
    img_arr =cv2.imread(img_path)
    plt.imshow(img_arr,cmap='gray')
    plt.show()
    break

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

我还尝试打印 img_arr 以查看可能是什么问题......我得到了:

for category in CATEGORIES:
    path =os.path.join(DATADIR,category)
    for img in os.listdir(path):
        img_path =os.path.join(path,img)
        img_arr =cv2.imread(img_path)
        print(cv2.imread(img_arr))
        break

None
None

我不认为我的路径是错误的,或者我已经检查了几次并尝试了很多东西......有什么想法吗?

【问题讨论】:

  • 问题实际上与neural-networkdeep-learning 无关 - 请不要向无关标签发送垃圾邮件(已删除)。
  • 这里的答案能解决您的问题吗? stackoverflow.com/questions/32302180/…

标签: python opencv matplotlib


【解决方案1】:

嗯.. 上帝知道我已经尝试了很多东西,最后其中一个奏效了。我仍然不知道为什么cv2.imread( ) 不起作用,但我改用plt.imread( ),它就像一个魅力。我还想要特定的图像形状(否则我在进一步的步骤中遇到问题,尤其是当我想将它们变成一个 numpy 数组时)。

DATADIR =r"C:\Users\Müco\Desktop\YAZILIM\kaggle_deeplearning\cat-dog\PetImages"
CATEGORIES =['Dog','Cat']
IMG_SIZE =50
data =[]

for category in CATEGORIES:
    path =os.path.join(DATADIR,category)
    label =CATEGORIES.index(category)
    for img in os.listdir(path):
        try:
            img_path =os.path.join(path,img)
            img_arr =plt.imread(img_path)
            img_arr = cv2.resize(img_arr,(IMG_SIZE,IMG_SIZE)) 
            if img_arr.shape==(50,50,3):
                data.append([img_arr,label])
        except Exception as e:
            print(str(e))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    • 2020-12-16
    • 2013-10-29
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    相关资源
    最近更新 更多