If you want to import images from a folder in your computer you can import images 1 by 1 from the folder in insert the in a list.
Your folder format is as you have shown:
PetData
|
Dog - images
|
Cat - images
Assume path
is a variable storing the address of PetData folder. We will use OpenCV to import images but you can use other libraries as well.
data = []
label = []
Files = ['Dog', 'Cat']
label_val = 0
for files in Files:
cpath = os.path.join(path, files)
cpath = os.path.join(cpath, 'images')
for img in os.listdir(cpath):
image_array = cv2.imread(os.path.join(cpath, img), cv2.IMREAD_COLOR)
data.append(image_array)
label.append(label_val)
label_val = 1
Convert the list to a numpy array.
data = np.asarray(data)
label = np.asarray(label)
After importing the images you can use train_test_split
to split the data for training and testing.
X_train, X_test, y_train, y_test = train_test_split(data, label, test_size=0.33, random_state=42)