Home > front end >  How do I read and label images stored in two different directories for binary classification?
How do I read and label images stored in two different directories for binary classification?

Time:05-25

The two directories I have contain many images, each image having a unique name. One directory is healthy, and another one is unhealthy.

I need to read images from both the directories, and label the images from one directory as 1, and 0 from another directory, then create a numpy array with each each image mapped to its corresponding label.

healthy = glob.glob('/path to healthy directory/'   '*.nii.gz')
unhealthy = glob.glob('path to unhealthy directory'   '*.nii.gz')

images = []

for i in healthy:
img = nib.load(i).get_fdata()
images.append(img)

for i in unhealthy:
img = nib.load(i).get_fdata()
images.append(img)

CodePudding user response:

Use glob.glob(/path/to/directory/**.<extension>) to get a list of images in a directory. You might need to use more than one call if there are multiple file extensions.

Once you have a list of images, you can do

np.concatenate(np.array([images]), np.ones((1, len(images))))

or

np.concatenate(np.array([images]), np.zeros((1, len(images))))

as appropriate.

CodePudding user response:

There's a good tutorial here that addresses this, specifically the function you're likely looking for is this one. You have to use labels=inferred and name your directories based on the classes you're dealing with. Also, you likely don't need to use numpy arrays. You can probably use functions from here to do what you need to do. Best of luck!

  • Related