从文件夹中随机选择多个文件并在cv2中读取

时间:2019-04-21 00:09:24

标签: python neural-network cv2

我正在使用Python构建CNN,我的数据集包含约14k图像和6个标签,因此运行神经网络需要大量时间。我所有的标签都位于不同的文件夹中,标签名称为文件夹名称,我想从每个文件夹中随机选择500张图像以供CNN运行。我写了下面的代码

imageset = []
labels =[]
mountain = os.listdir('../folder/mountain')
glacier = os.listdir('../folder/glacier')

filenames = random.sample(mountain, 500)

for i in filenames :
    image = cv2.imread('../folder/mountain/'+i,cv2.IMREAD_COLOR)
    image_size =cv2.resize(image,(50,50))
    imageset.append(np.array(image_size))
    labels.append(3)

#for fname in filenames:
train_images =np.array(imageset)
print(train_images.shape)

由于我从文件夹中随机选择了100张图像,因此print(train_images.shape)应该给我(500,50,50,3)。但是它显示的是图像(3000,50,50,3)的实际数量。

0 个答案:

没有答案