Learn practical skills, build real-world projects, and advance your career
Created 4 years ago
Dataset Preprocessing
import cv2
import os
path = './dataset'
categories = os.listdir(path)
labels = [i for i in range(len(categories))]
print(categories, labels)
dic = dict(zip(categories,labels))
print(dic)
['without_mask', 'with_mask'] [0, 1]
{'without_mask': 0, 'with_mask': 1}
data = []
target = []
for i in categories:
folder_path = os.path.join(path,i)
img_names = os.listdir(folder_path)
for img_name in img_names:
img_path = os.path.join(folder_path,img_name)
img = cv2.imread(img_path,0)
try:
resized = cv2.resize(img,(100,100))
data.append(resized)
target.append(dic[i])
except Exception as e:
print("Exception:",e)
import numpy as np
data = np.array(data)/255 # Normalizing
#CNN takes input as (n,(x_size,y_size,channels))
# n -> Number of images
# channels -> 3 for coloured and 1 for BandW
data = np.reshape(data,(data.shape[0],100,100,1))
target = np.array(target)
from keras.utils import np_utils
new_targets = np_utils.to_categorical(target)