-1

Say I am given a training folder. The training folder contains separate folders that has images (CNV, DME, DRUSEN, and NORMAL are all types of detections):

enter image description here

I want to go through every image in each folder and assign every image an

  • Unique ID and a Disease Name

Then have it write to a CSV

Example of what I am trying to accomplish:

enter image description here

I want to have a CSV that has a unique id for all 60,000 images I have that are contained within the folders on one column and the type of disease (or file: CNV, DME, DRUSEN, NORMAL) on the other column.

Community
  • 1
  • 1
Trush P
  • 43
  • 1
  • 10

1 Answers1

1

You can do this with the help of os.listdir().

csv = 'id, disease\n'
for disease in os.listdir(root_data_path):
    for file in os.listdir(os.path.join(root_data_path, disease)):
        csv += f'{file},{disease}\n'

with open('diseases_ids.csv', 'w+') as output_csv:
    output_csv.write(csv)

I haven't tested it, but you got the idea, right?

Adelson Araújo
  • 332
  • 1
  • 5
  • 17
  • Yeah! This is what I was trying to go for, I've tried doing something similar to this, but it was taking forever that I had to cancel it because I thought I did it wrong. Im guessing I'll have to run the for loop 4 times (one for each folder) before I run the with open code correct? – Trush P May 16 '20 at 02:22