Skip to content

Instantly share code, notes, and snippets.

@aliwaqas333
Created June 19, 2020 09:08
Show Gist options
  • Save aliwaqas333/1cdc93265849129e7cd85780705bcf8e to your computer and use it in GitHub Desktop.
Save aliwaqas333/1cdc93265849129e7cd85780705bcf8e to your computer and use it in GitHub Desktop.
This class is used to extract images from various directories
REBUILD_DATA = True
if REBUILD_DATA: #if we are running it for the first time
data_path = Path('F:/FILES/AI/face-mask-dataset/')
maskPath = data_path/'dataset1/AFDB_masked_face_dataset'
maskPath2= data_path/'dataset2/webface_masked'
nonMaskPath = data_path/'dataset1/AFDB_face_dataset'
path_dirs = [ [maskPath,1],[nonMaskPath,0] ] #path and label
if not os.path.exists(data_path):
raise Exception("The data path doesn't exist")
class MaskvNoMask():
IMG_SIZE = 100
LABELS = {'NON_MASKED': 0, 'MASKED': 1}
training_data = [] # We will append one image at a time
def make_training_data(self):
for data_dir, label in path_dirs:
print('Reading from: ',label)
for folder in tqdm(os.listdir(data_dir)):
folder_path = os.path.join(data_dir, folder)
for imgpath in os.listdir(folder_path):
self.count += 1
img_path = os.path.join(folder_path, imgpath)
try: # putting this in try block will help to avoid corrupt image files
img = cv2.imread(img_path)
img = cv2.resize(img, (self.IMG_SIZE,self.IMG_SIZE))
self.training_data.append([np.array(img), label]) # adding image to train data
if label == 1:
self.LABELS['MASKED'] += 1 # counting number of masked images
if label == 0:
self.LABELS['NON_MASKED'] +=1
except:
# raise Exception('error: {}'.format(img_path))
pass
print(self.LABELS)
np.random.shuffle(self.training_data)
np.save("./npy/training_data.npy", self.training_data) # saving the numpy array so we dont have to read it again, its a lengthy process to read images from disk.
if REBUILD_DATA:
maskvnomask = MaskvNoMask()
maskvnomask.make_training_data()
training_data = maskvnomask.training_data
else:
training_data = np.load('./npy/training_data.npy', allow_pickle=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment