getclass by filename.py 599 Bytes
import pandas as pd
import os
from natsort import natsorted

csv = 'ce_train_targets.csv'
data_path = 'ce_train'

df = pd.read_csv(csv)

#idx = df.index[df.iloc[:,0]=='BraTS19_CBICA_BHB_1_seg_flair_8.png'].tolist()

# df = df.loc[df.iloc[:,0]=='BraTS19_CBICA_BHB_1_seg_flair_8.png']
# print(df.iloc[0, 1])


imgs = natsorted(os.listdir(data_path)) # img file list
targets = []

for fname in imgs:
    row = df.loc[df['filename'] == fname]
    targets.append(row.iloc[0, 1])

#print(targets, len(targets))

#BraTS19_2013_10_1_seg_flair_0.png -> class 0
#BraTS19_CBICA_AYU_1_seg_flair_2.png -> class 1