JuniorPython
JuniorPython

Reputation: 113

how to separate images in a folder to train folder according to the filename in my train csv file

currently learning deep learning with my own dataset and have around 70k images in 1 folder and already input the images to csv file that have filename, width, height, class and already divide them randomly into train,valid, and test csv

my question is, is there any way how to seperate the images based on the filename on my csv files?

any answer would be appreciated <3 thank you

Upvotes: 2

Views: 860

Answers (1)

Sai Chandra Reddy
Sai Chandra Reddy

Reputation: 89

First, you need to extract the values from a column from the data frame and save them in a list

      filenames = data['filenames'].values
      filenames = filenames.tolist()
      classes = data['classes'].values
      classes = classes.tolist()

Now, Extract the file names from the directory "C:/data/Images/" or use os.getcwd()

     path = os.getcwd()
     images = [imagefilename for imagefilename in os.listdir(path) if imagefilename.endswith('.jpg') or imagefilename.endswith('.png')]
     

Now, compare the images and filename

  finalclasses = []
  finalimages = []
  for i in range(len(filename)):
       if len(images) != len(filenames): break;
       if filename[i] == images[i]:
            finalclasses.append(classes[i])
            finalimages.append(os.path.join(path, image)) #or read with opencv by importing cv2   finalimages.append(cv2.read(os.path.join(path, image)))

The above will helps to solve your problem, Thank, Happy Learning :)

Upvotes: 1

Related Questions