如何将 12 个不同文件夹中的所有图像复制到一个文件夹中?

How may I copy all images from different 12 folders to a single folder?

我正在使用 image dataset。它有 12 different folders12 different classes。为此,我想reserve all images中的a single directory也就是all_im。我在上面写代码,但它总共只复制了 808 images。但是我的主文件夹包含 more than 5000 images。我如何 copymain folderGoogle-Colab 中的 new folder 的所有图像?

我的完整代码:

from numpy.random import seed
seed(101)
from tensorflow import set_random_seed
set_random_seed(101)

import pandas as pd
import numpy as np

import tensorflow

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Conv2D, MaxPooling2D, Flatten
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint

import os
import cv2

import imageio
import skimage
import skimage.io
import skimage.transform

from sklearn.utils import shuffle
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
import itertools
import shutil
import matplotlib.pyplot as plt
%matplotlib inline

SAMPLE_SIZE = 250

# The images will all be resized to this size.
IMAGE_SIZE = 96

os.listdir('content/image_dataset')

folder_list = os.listdir('/content/image_dataset')

all_im_dir = 'all_im'
os.mkdir(all_im)

destination_path = "/content/all_images"
pattern = "/content/Weeds_dataset/*/*"  
for img in glob.glob(pattern):
    shutil.copy(img, destination_path)

打印函数:len(os.listdir('all_images'))

输出:808 images

预期:主文件夹包含 5300 pictures 但我只能复制 808 Images.

您必须重命名图像。您可以在最后一个循环中添加一个计数器并使用计数器来命名图像。

from numpy.random import seed
seed(101)
from tensorflow import set_random_seed
set_random_seed(101)

import pandas as pd
import numpy as np

import tensorflow

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Conv2D, MaxPooling2D, Flatten
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint

import os
import cv2

import imageio
import skimage
import skimage.io
import skimage.transform

from sklearn.utils import shuffle
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
import itertools
import shutil
import matplotlib.pyplot as plt
%matplotlib inline

SAMPLE_SIZE = 250

# The images will all be resized to this size.
IMAGE_SIZE = 96

os.listdir('content/image_dataset')

folder_list = os.listdir('/content/image_dataset')

all_im_dir = 'all_im'
os.mkdir(all_im)

destination_path = "/content/all_images/"
pattern = "/content/image_dataset/*/*"
counter = 0
for img in glob.glob(pattern):
    counter += 1
    shutil.copy(img, destination_path + str(counter) + img.split('.')[-1])