【问题标题】:How to add a column in a CSV file while converting Image folder to CSV file in Python如何在 Python 中将图像文件夹转换为 CSV 文件时在 CSV 文件中添加列
【发布时间】:2021-05-22 04:22:46
【问题描述】:

我在一个文件夹中有很多数字图像,我已经在 CSV 文件中转换了这些图像,但是就像在 MNIST 数据集中,当我们在 CSV 文件中转换一个数字时,它附加了一个标签列但是当我转换我的image 文件夹转换为 CSV ,我的 CSV 文件不包含标签列。

请帮助我如何在我自己创建的 CSV 文件中添加标签列。 我写的代码如下:-

from PIL import Image
import numpy as np
import sys
import os
import csv  

# default format can be changed as needed
def createFileList(myDir, format='.jpg'):
    fileList = []
    print(myDir)
    for root, dirs, files in os.walk(myDir, topdown=False):
        for name in files:
            if name.endswith(format):
                fullName = os.path.join(root, name)
                fileList.append(fullName)
    return fileList

# load the original image
myFileList = createFileList('/path_to_directory_with_images/')

for file in myFileList:
    print(file)
    img_file = Image.open(file)
    # img_file.show()

    # get original image parameters...
    width, height = img_file.size
    format = img_file.format
    mode = img_file.mode

    # Make image Greyscale
    img_grey = img_file.convert('L')
    #img_grey.save('result.png')
    #img_grey.show()

    # Save Greyscale values
    value = np.asarray(img_grey.getdata(), dtype=np.int).reshape((img_grey.size[1], img_grey.size[0]))
    value = value.flatten()
    print(value)
    with open("name_you_want.csv", 'a') as f:
        writer = csv.writer(f)
        writer.writerow(value)
 

【问题讨论】:

    标签: python machine-learning deep-learning mnist


    【解决方案1】:

    假设您在一个目录中有子目录,其中每个子目录表示一个类。

    我过去也遇到过类似的问题,虽然不是很确切,但我是这样处理的。

    1. 创建一个字典对象,将您的类名作为键,将它们的序号编码值作为键的值。

    代码示例:-

    class_label = {
    
      'angry' : '0', 
      'disgust' : '1', 
      'fear' : '2', 
      'happy' : '3', 
      'neutral' : '4', 
      'sad' : '5',
      'surprise' : '6'
    
    }
    

    在这里,在上面的示例中,我的类是键,我定义的它们的序数值是键的值。

    1. 循环访问您的目录文件夹,其中包含利用上述字典键名的子目录。

    代码示例:-

    image_label = []
    file_name = []
    for label in class_label:
        files = glob.glob( directory_path + label + '/*.jpg' )
        for names in files:
            file_name.append( names )
            image_label.append( str( class_label[label] ) ) 
    

    这样您将在file_name 中获得完整的文件名,在image_label 中获得相应的标签

    注意-上述功能只是为了回答问题,您可能需要更改它以适合您的目标。

    1. 从你的createFileList(..)函数返回image_labelfile_name(假设你按照上面的代码改变了createFileList(..)函数)

    代码示例:-

    img_label, file_name = createFileList( .. )
    
    for i in range( len( file_name ) ):
        .
        .
        .
        value = np.asarray(...)
        value = value.flatten()
        value.append( img_label[i] )
        print(value)
        with open("name_you_want.csv", 'a') as f:
            writer = csv.writer(f)
            writer.writerow(value)
    

    达尼亚瓦德。

    【讨论】:

      猜你喜欢
      • 2018-08-10
      • 2023-03-30
      • 2021-03-07
      • 2019-02-16
      • 2020-11-03
      • 2017-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多