【问题标题】:plt.imshow() giving TypeError("Image data cannot be converted to float")plt.imshow() 给出 TypeError("图像数据不能转换为浮点数")
【发布时间】:2019-03-14 05:57:47
【问题描述】:

我是 python 新手。尝试使用以下代码绘制图像时 -

import tensorflow as tf;
import os, sys;
import skimage;
from skimage.color import rgb2gray
import matplotlib.pyplot as plt;
import random


def load_data(data_directory) :
    directories = [d for d in os.listdir(data_directory) if os.path.isdir(os.path.join(data_directory,d))]

    labels =[]
    images =[]

    for d in directories:
        label_directory = os.path.join(data_directory,d)
        file_names = [os.path.join(label_directory, f)
                      for f in os.listdir(label_directory)
                      if f.endswith(".ppm")]
        for f in file_names:
            images.append(skimage.data)
            labels.append(int(d))
    return images, labels

ROOT_PATH = "C:/Users/Administrator/Downloads/"

train_data_directory= os.path.join(ROOT_PATH, "BelgiumTSC_Training/Training")
test_data_directory = os.path.join(ROOT_PATH, "BelgiumTSC_Testing/Testing")
images, labels = load_data(train_data_directory)
traffic_signs = [300, 2250, 3650, 4000]
for i in range(len(traffic_signs)):
    plt.subplot(1, 4, i+1)
    plt.axis('off')
    plt.imshow(images[traffic_signs[i]])
    plt.subplt_adjust(wspace=0.5)
    plt.show()

我收到以下错误 -

raise TypeError("Image data cannot be converted to float")

TypeError:图像数据无法转换为浮点数

谁能解释我哪里出错了。我正在学习来自 - https://www.datacamp.com/community/tutorials/tensorflow-tutorial#tensors

的教程

【问题讨论】:

  • 能否给出image变量的数据类型?以及有关图像变量的见解
  • 方法load_data来自哪个包?
  • 我尝试使用数据营中的相同代码,但我没有遇到任何错误。请检查您的图像变量print(len(images)) 的长度可能是图像未导入
  • 我已经添加了方法-load_data。此外,当我尝试打印 len(images) 时,我得到 4575。
  • 请提供完整的错误信息

标签: python tensorflow matplotlib


【解决方案1】:

在images.append(skimage.data) 行中的load_data 方法中,您实际上并没有加载任何图像,您只需将python 模块skimage.data 附加到图像,这就是它无法显示任何内容的原因,因为您的images不要包含任何图像。 你可能想要images.append(skimage.data.load(f))。

【讨论】:

    猜你喜欢
    • 2017-11-12
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多