Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

How do I create a CSV file using .jpg pictures from a leaf dataset and label each?

Every code or answer I find here only has parts of what I want to do. I attempted to use this code (below) and was able to obtain no errors, but I am so new at this I have no clue what I've done. I need to create this CSV file so that each leaf has a label that will be the output. These will be run through a neural network trainer so that they may be identified. I don't know how to access my dataset...I DID upload the whole dataset into "files" in Colab. I just don't know how to code in order to access them or use them to create a CSV file like I mentioned above.

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

#Useful function
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/')

fileList = []

for fileNAME in fileList:
    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("img_pixels.csv", 'a') as f:
        writer = csv.writer(f)
        writer.writerow(value)

Comments