【问题标题】:How to read the shape of all images and display them, present in a dataset folder through google colab?如何读取所有图像的形状并显示它们,通过google colab呈现在数据集文件夹中?
【发布时间】:2019-07-10 18:06:42
【问题描述】:

我正在尝试在 google colab 上训练我的图像数据集。我在 colab 中有数据集文件夹。当尝试从 colab 目录中读取图像时,我只能读取所有图像的文件名。但是,如果我尝试提取数组中图像的形状,则会使用不同的方法给出不同的错误。我尝试过使用 os 库和 PIL.Image 甚至 pickle,但我仍然无法排序甚至猜测可能是什么问题。

我得到的错误是:

1) AttributeError: 'list' 对象没有属性 'read'
2) AttributeError: 'list' 对象没有属性 'seek'

在 for 循环中使用 os.walk(path) 函数并从路径中存在的所有文件的结果列表中提取文件时。

3) FileNotFoundError: [Errno 2] No such file or directory: '7119-220.jpg'

这看起来很奇怪,因为每次我运行代码时它都会专门寻找同一个文件。通过使用 try 和除了这个 FileNotFoundError 我没有得到任何输出。

问题:我没有得到什么错误?

import os
import matplotlib.pyplot as plt
import time
import numpy as np
from PIL import Image

imagesPath = 'Neural_Net-for-Concrete-Crack-Detection/Wall_crack_dataset/W/CW'

target_names = [item for item in os.listdir(imagesPath)
                if os.path.isdir(os.path.join(imagesPath, item))]
number_train_samples = sum([len(files) for _, _, files in os.walk(imagesPath)])  

image = np.zeros((256, 256), dtype=int)

total_number_samples = number_train_samples

print('Training a CNN Multi-Classifier Model ......')
print(' - # of trained samples: ', number_train_samples, 
       '\n - total # of samples: ', total_number_samples)

这件作品仅用于计算图像文件的数量。

from PIL import Image
import os
i=0
image = np.zeros((256, 256), dtype='uint8')

imagesPath = 'Neural_Net-for-Concrete-Crack-Detection/Wall_crack_dataset/W/CW'

for _, _, files in os.walk(imagesPath):
  for file in files:
    image = Image.open(file)

如果我在要绘制的目录中指定一个特定的图像文件,此代码效果会更好,但不是全部。

【问题讨论】:

    标签: python opencv deep-learning


    【解决方案1】:

    os.walk(...) 产生一个三元组(dirpath, dirnames, filenames)。因此,您应该尝试打开os.path.join(dirpath, file) 而不是file

    from PIL import Image
    import os
    i=0
    image = np.zeros((256, 256), dtype='uint8')
    
    imagesPath = 'Neural_Net-for-Concrete-Crack-Detection/Wall_crack_dataset/W/CW'
    
    for dirpath, _, files in os.walk(imagesPath):  # <--
      for file in files:
        image = Image.open(os.path.join(dirpath, file))  # <--
    

    如果你需要一个形状为(n_samples, channels, height, width)dataset 并且你想坚持PIL.Image,你可以这样做:

    dataset_dir = "[DATASET_DIR]"
    dataset = np.asarray([
        np.asarray(  # convert from PIL.Image to np.array
            Image.open(os.path.join(dirpath, img_fname))  # open image
        ).transpose((2,0,1))  # change from (H,W,C) to (C,H,W)
        for dirpath, _, fnames in os.walk(dataset_dir)  # scan the `dataset_dir`
        for img_fname in fnames  # for each file in `dataset_dir`
    ])
    

    注意它要求所有图片都具有相同的shape

    【讨论】:

    • 我正在使用这种方法获取文件,但仍然无法存储图像形状和数组。我尝试使用 keras.preprocessing.image 库中的 img_to_array 。我想要的只是将图像作为数组存储在数据集数组中,定义为: dataset = np.ndarray(shape=(len(train_files), channels, image_height, image_width), dtype=np.float32)
    • @rajkumaryadav 您的原始问题缺少这些信息。我用代码更新了我的答案,该代码在不使用 keras 的情况下为您提供所需的数据集,如果您对 keras 有疑问,我建议您打开另一个问题。如果您觉得我的回答有帮助,请考虑点赞。如果您打开后续问题,可以在此处粘贴链接,我可能会看一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 2017-03-04
    • 2022-12-21
    相关资源
    最近更新 更多