I have a set of pictures, which are labelled dog, cat, truck, airplane and car in a folder. Once I import them in to python I want to assign them binary labels. The following code shows how I can extract pictures from the folder and do it for 1 class but how can I do it for multiple classes? For example 1 for 'dog', 2 for 'cat', 3 for 'truck', 4 for 'airplane' and 5 for 'car'.
Test_dir = "C:/Users/Instructor/Dropbox/Data Science/2.Temp_WORD FILES/test"
image_width = 32
image_height = 32
def read_images(directory, resize_to=(128, 128)):
"""This function extracts images from given
directory"""
files = glob.glob(directory + "/*.jpg")
images = []
labels = []
for f in tqdm.tqdm_notebook(files):
im = Image.open(f)
im = im.resize(resize_to)
im = np.array(im) / 255.0
im = im.astype("float32")
images.append(im)
label = 1 if "dog" in f.lower() else 0
labels.append(label)
return np.array(images), np.array(labels)
X, y = read_images(directory=Test_dir, resize_to=(IM_WIDTH, IM_HEIGHT))
Aucun commentaire:
Enregistrer un commentaire