I Am trying to transfer images from a folder into a separated sub-folders using GroundTruth.csv file

image for the csv file head :

https://user-images.githubusercontent.com/45392637/57573574-36e5f780-742a-11e9-8e16-bf14bf389f0e.JPG

here is my code :

 import pandas as pd
 import os
 import shutil

Read in the data

 ground_truth = pd.read_csv('GroundTruth.csv')

Get a list of images in each of the two folders

 folder = os.listdir('Training_Input')

Get a list of train and val images

 train_list = list(ground_truth['image'])
 base_dir = 'base_dir'
 os.mkdir(base_dir)

Creating files directories

 train_dir = os.path.join(base_dir, 'train_dir')
 os.mkdir(train_dir)
 nv = os.path.join(train_dir, 'NV')
 os.mkdir(nv)
 mel = os.path.join(train_dir, 'MEL')
 os.mkdir(mel)
 bkl = os.path.join(train_dir, 'BKL')
 os.mkdir(bkl)
 bcc = os.path.join(train_dir, 'BCC')
 os.mkdir(bcc)
 akiec = os.path.join(train_dir, 'SCC')
 os.mkdir(akiec)
 vasc = os.path.join(train_dir, 'VASC')
 os.mkdir(vasc)
 df = os.path.join(train_dir, 'DF')
 os.mkdir(df)
 df = os.path.join(train_dir, 'UNK')
 os.mkdir(df)
 df = os.path.join(train_dir, 'AK')
 os.mkdir(df)

the problem is when I try to transfer the images

here:

 # Transfer the training images
 for row in ground_truth.iterrows():

 image = row[1].image
 label = row[1].idxmax()
 fname = image + '.jpg'

  if fname in folder:
       # source path to image
       src = os.path.join('Training_Input', fname)
       # destination path to image
       dst = os.path.join(train_dir, label, fname)
       # copy the image from the source to the destination
       shutil.copyfile(src, dst)

I get this error :

  TypeError                                 Traceback (most recent call last)
  <ipython-input-35-9ee8f2c063cc> in <module>()
       2 
       3     image = row[1].image
 ----> 4     label = row[1].idxmax()
       5     fname = image + '.jpg'
       6 

 1 frames
 /usr/local/lib/python3.6/dist-packages/pandas/core/nanops.py in _f(*args, 
 **kwargs)
    71             if any(self.check(obj) for obj in obj_iter):
    72                 msg = 'reduction operation {name!r} not allowed for 
   this  dtype'
    ---> 73     
    raise TypeError(msg.format(name=f.__name__.replace('nan', '')))
   74             try:
   75                 with np.errstate(invalid='ignore'):

   TypeError: reduction operation 'argmax' not allowed for this dtype

the problem is in dtype of columns I think!

1 Answers

0
Guissous Allaeddine On Best Solutions

I tried this and it works The argmax error was occuring because the first column (image) is a string using row[1].values[1:] we skip the first column

    for row in ground_truth.iterrows():
    image = row[1].image
    label = row[1].index[row[1].values[1:].argmax() + 1]
    fname = image + '.jpg'
    if fname in folder:
    src = os.path.join('ISIC_2019_Training_Input', fname)
    dst = os.path.join(train_dir, label, fname)
    shutil.copyfile(src, dst)