MQS
MQS

Reputation: 41

Getting specific images from a folder using a csv file with the labels of the images

I have a folder with 4000 images from chest x-rays and they are classified as having pathologies or not. There is a csv file with the label for each image and the name of the file. I have already selected the first 100 "normal" images with a small python code, but know I would like to get those images and copy them to a new folder. How do I do that? I have tried many suggestions I saw online but none of them apply to what I want to do. Thank you for your help.

dataset = pd.read_csv("Data_Entry_2017.csv")

No_Finding = dataset.loc[dataset['Finding Labels'] == 'No Finding']
No_Finding = No_Finding.iloc[:100,0]
print(No_Finding)
No_Finding.to_csv(path_or_buf='NormalImages.csv')
df = pd.read_csv("NormalImages.csv")
df = df.iloc[:,1].values

def load_images_from_folder(folder):
images = []
for filename in os.listdir(folder):
    img = cv2.imread(os.path.join(folder,filename))
    if img is not None:
        images.append(img)
return images

I've tried to load the images but even that is starting to get difficult.

Upvotes: 2

Views: 1142

Answers (1)

MQS
MQS

Reputation: 41

 # Source path with original set of images
source = "C:/Users/.../images"
  
# Destination path for the first 100 normal images
destination = "C:/Users/.../images_normal"
  

# Copy normal images to destination folder
for file in df:
    shutil.copy(os.path.join(source, file), destination)

Upvotes: 2

Related Questions