Created
August 29, 2021 20:22
-
-
Save KananMahammadli/9ebc1c579c47dc82a051133f20ff3688 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
train_images = [] | |
train_labels = [] | |
test_images = [] | |
test_labels = [] | |
dataset_path = 'tomato' | |
for train_test_folder in os.listdir(dataset_path): | |
# if we are in train folder, we go through disease/healthy folders there | |
if train_test_folder == 'train': | |
train_path = os.path.join(dataset_path, train_test_folder) | |
# for each disease/healthy folder we take folder name as label and go through it to read images | |
for disease_folder in os.listdir(train_path): | |
disease_path = os.path.join(train_path, disease_folder) | |
label = disease_folder.split('___')[1] | |
# in each disease/healthy folder we read files with jpg format, i.e images and normalize them | |
for file in os.listdir(disease_path): | |
if file.endswith('jpg'): | |
img_path = os.path.join(disease_path, file) | |
img = cv2.imread(img_path) | |
r, g, b = img[:, :, 0]/255, img[:, :, 1]/255, img[:, :, 2]/255 | |
img = np.dstack((r, g, b)) | |
train_images.append(img) | |
train_labels.append(label) | |
# if we are in val folder, we go through disease/healthy folders there | |
if train_test_folder == 'val': | |
test_path = os.path.join(dataset_path, train_test_folder) | |
# for each disease/healthy folder we take folder name as label and go through it to read images | |
for disease_folder in os.listdir(test_path): | |
disease_path = os.path.join(test_path, disease_folder) | |
label = disease_folder.split('___')[1] | |
# in each disease/healthy folder we read files with jpg format, i.e images and normalize them | |
for file in os.listdir(disease_path): | |
if file.endswith('jpg'): | |
img_path = os.path.join(disease_path, file) | |
img = cv2.imread(img_path) | |
r, g, b = img[:, :, 0]/255, img[:, :, 1]/255, img[:, :, 2]/255 | |
img = np.dstack((r, g, b)) | |
test_images.append(img) | |
test_labels.append(label) | |
train_images = np.array(train_images) | |
train_labels = np.array(train_labels) | |
test_images = np.array(test_images) | |
test_labels = np.array(test_labels) | |
print('Shape of the stacked train images:', train_images.shape) | |
print('Shape of the train labels:', train_labels.shape) | |
print('Shape of the stacked test images:', test_images.shape) | |
print('Shape of the test_labels:', test_labels.shape) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment