2 回答
TA贡献1883条经验 获得超3个赞
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 = []
switch_values = {'dog':1,'cat':2, 'truck':3, 'airplane':4 ,'car':5}
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 = switch_values.get(f.lower())
labels.append(label)
return np.array(images), np.array(labels)
X, y = read_images(directory=Test_dir, resize_to=(IM_WIDTH, IM_HEIGHT))
TA贡献1803条经验 获得超3个赞
定义字典以将动物名称映射到标签
animal_to_label = {'dog': 1,'cat': 2,'truck': 3,'airplane': 4,'car': 5 }
Test_dir = "C:/Users/Instructor/Dropbox/Data Science/2.Temp_WORDFILES/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 = []
switch_values = {'dog':1,'cat':2, 'truck':3, 'airplane':4 ,'car':5}
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)
name = f.split("/")[-1].split(".")[0]
label = animal_to_label[name.lower()]
labels.append(label)
return np.array(images), np.array(labels)
X, y = read_images(directory=Test_dir, resize_to=(IM_WIDTH, IM_HEIGHT))
添加回答
举报