def nua_data_create(): count = 0 X_nua = [] y_nua = [] X_gray = np.empty((1, 256, 256, 1)) X_dog = np.empty((1, 256, 256, 1)) X_lbp = np.empty((1, 256, 256, 1)) for root, dirs, filenames in os.walk('NUA'): for file in filenames: if file.endswith('.jpg'): if count % 10 == 0: print('file ' + str(file)) path = os.path.join(root, file) img = cv2.imread(path) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB) # cb = ycrcb[0] # print('converted to gray') dog = calc_dog(gray) # print('dog') lbp = calc_lbp(gray) # print('lbped') # gray_img = cv2.resize(gray, (256, 256)) gray_img = cv2.resize(gray, (256, 256)) gray_img = np.expand_dims(gray_img, axis=-1) dog = np.expand_dims(dog, axis=-1) lbp = np.expand_dims(lbp, axis=-1) # gray_img = np.concatenate((gray_img, gray_img, gray_img), axis=-1) # dog = np.concatenate((dog, dog, dog), axis=-1) # lbp = np.concatenate((lbp, lbp, lbp), axis=-1) X_gray[0] = gray_img.astype('float32') / 255 X_dog[0] = dog.astype('float32') / 255 X_lbp[0] = lbp.astype('float32') / 255 intermediate_layer_model = Model( inputs=model.input, outputs=model.get_layer('conv2d_7').output) intermediate_output = intermediate_layer_model.predict( [X_gray, X_dog, X_lbp]) print('appending output') X_nua.append(intermediate_output) if path.strip('.jpg').split('_')[-1] == 'live': print('live image') y_nua.append(1) else: print('spoof image') y_nua.append(0) count += 1 print('saving numpy arrays') np.save('X_nua_2.npy', np.array(X_nua)) np.save('y_nua.npy', np.array(y_nua))
def __data_generation(self, list_IDs_temp): 'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels) # Initialization # X_gray = np.empty((self.batch_size, 256, 256, 1)) # X_dog = np.empty((self.batch_size, 256, 256, 1)) # X_lbp = np.empty((self.batch_size, 256, 256, 1)) # for resnet X = np.empty((self.batch_size, 256, 256, 1)) y = np.empty((self.batch_size), dtype=int) # Generate data for i, ID in enumerate(list_IDs_temp): # Store sample img = cv2.imread(ID) # img = cv2.resize(img, (256, 256)) # img = cv2.resize(img, (224, 224)) # resnet # img = np.expand_dims(img, axis=-1) idx = self.list_IDs.index(ID) # print('id' + str(ID) +'label ' + str(self.labels[idx])) try: gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) lbp = calc_lbp(gray) # print('lbped') lbp = np.expand_dims(lbp, axis=-1) # Store class X[i] = lbp.astype('float32') / 255 y[i] = self.labels[idx] # with open('log.txt', 'a') as lf: # lf.write('ID ' + str(ID) + ' label ' + str(y[i])+'\n') # print('y[' + str(i) + ']= ' + str(y[i])) except cv2.error as e: print(e) # print('skipping id') continue # print('yyy = ' +str(keras.utils.to_categorical(y, num_classes=self.n_classes))) # print('X_gray = ' + str(X_gray)) # print('X_dog = ' + str(X_dog)) # print('X_lbp = ' + str(X_lbp)) return X, y
def siw_data_create(): X_siw_paths = [] X_siw = [] y_siw = [] X_gray = np.empty((1, 256, 256, 1)) X_dog = np.empty((1, 256, 256, 1)) X_lbp = np.empty((1, 256, 256, 1)) count = 0 print('collecting validation and test data') for root, dirnames, filenames in os.walk(TEST_DIR): for filename in fnmatch.filter(filenames, "*.jpg"): path = os.path.join(root, filename) if path.split('/')[-3] == 'live': X_siw_paths.append(path) y_siw.append(1) elif path.split('/')[-3] == 'spoof': X_siw_paths.append(path) y_siw.append(0) count += 1 X_siw_paths, y_siw = shuffle(X_siw_paths, y_siw) pairs = list(zip(X_siw_paths, y_siw)) # make pairs out of the two lists pairs = random.sample(pairs, 1500) # pick 3 random pairs filepaths, labels = zip(*pairs) for image_path in filepaths: img = cv2.imread(image_path) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB) # cb = ycrcb[0] # print('converted to gray') dog = calc_dog(gray) # print('dog') lbp = calc_lbp(gray) # print('lbped') # gray_img = cv2.resize(gray, (256, 256)) gray_img = cv2.resize(gray, (256, 256)) gray_img = np.expand_dims(gray_img, axis=-1) dog = np.expand_dims(dog, axis=-1) lbp = np.expand_dims(lbp, axis=-1) # gray_img = np.concatenate((gray_img, gray_img, gray_img), axis=-1) # dog = np.concatenate((dog, dog, dog), axis=-1) # lbp = np.concatenate((lbp, lbp, lbp), axis=-1) X_gray[0] = gray_img.astype('float32') / 255 X_dog[0] = dog.astype('float32') / 255 X_lbp[0] = lbp.astype('float32') / 255 intermediate_layer_model = Model( inputs=model.input, outputs=model.get_layer('conv2d_7').output) intermediate_output = intermediate_layer_model.predict( [X_gray, X_dog, X_lbp]) print('appending output') X_siw.append(intermediate_output) # # if path.strip('.jpg').split('_')[-1] == 'live': # print('live image') # y_siw.append(1) # else: # print('spoof image') # y_nua.append(0) print('saving numpy arrays') np.save('X_siw_2.npy', np.array(X_siw)) np.save('y_siw.npy', np.array(labels))
# roi = cv2.imread(file) # roi = cv2.resize(roi, (224, 224)) # roi = roi.astype('float32')/255 # X[0] = roi.astype('float32')/255 # roi = cv2.resize(roi, (224, 224)) # cv2.imshow('frame', roi) # cv2.imshow('roi', roi) # cv2.imwrite('roi3.jpg', roi) gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY) # ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB) # cb = ycrcb[0] # print('converted to gray') dog = calc_dog(gray) # print('dog') lbp = calc_lbp(gray) # print('lbped') # gray_img = cv2.resize(gray, (256, 256)) height, width = roi.shape[:2] # print('height ' + str(height)) # print('width' + str(width)) if height > 96 and width > 96: for j in range(16): # print('j = ' + str(j)) rH = random.uniform(0, height - 96) rW = random.uniform(0, width - 96) x, y = int(rH), int(rW) gray_roi = gray[x:x + 96, y:y + 96] with open('log.txt', 'a') as fw: fw.write('gray roi shape ' + str(gray_roi.shape) + '\n')
def __data_generation(self, list_IDs_temp): 'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels) # Initialization X_gray = np.empty((self.batch_size, 96, 96, 16)) X_dog = np.empty((self.batch_size, 96, 96, 16)) X_lbp = np.empty((self.batch_size, 96, 96, 16)) # for resnet # X = np.empty((self.batch_size, 256, 256, 3)) label = np.empty((self.batch_size), dtype=int) # Generate data for i, ID in enumerate(list_IDs_temp): # Store sample gray_patchs = [] lbp_patchs = [] dog_patchs = [] img = cv2.imread(ID) # img = cv2.resize(img, (256, 256)) # img = cv2.resize(img, (256, 256)) # resnet # img = np.expand_dims(img, axis=-1)` idx = self.list_IDs.index(ID) # print('id' + str(ID) +'label ' + str(self.labels[idx])) try: gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB) # cb = ycrcb[0] # print('converted to gray') dog = calc_dog(gray) # print('dog') lbp = calc_lbp(gray) # print('lbped') # gray_img = cv2.resize(gray, (256, 256)) height, width = img.shape[:2] # print('height ' + str(height)) # print('width' + str(width)) if height > 96 and width > 96: for j in range(16): # print('j = ' + str(j)) rH = random.uniform(0, height - 96) rW = random.uniform(0, width - 96) x, y = int(rH), int(rW) gray_roi = gray[x:x + 96, y:y + 96] with open('log.txt', 'a') as fw: fw.write('gray roi shape ' + str(gray_roi.shape) + '\n') dog_roi = dog[x:x + 96, y:y + 96] lbp_roi = lbp[x:x + 96, y:y + 96] gray_patchs.append(gray_roi) dog_patchs.append(dog_roi) lbp_patchs.append(lbp_roi) with open('log.txt', 'a') as fw: fw.write('gray patches shape ' + str(np.array(gray_patchs).shape)) # print('shape ' + str(np.moveaxis(np.array(gray_patchs), 0, -1).shape)) X_gray[i] = np.moveaxis(np.array(gray_patchs), 0, -1).astype('float32') / 255 X_dog[i] = np.moveaxis(np.array(dog_patchs), 0, -1).astype('float32') / 255 X_lbp[i] = np.moveaxis(np.array(lbp_patchs), 0, -1).astype('float32') / 255 label[i] = self.labels[idx] # with open('log.txt', 'a') as lf: # lf.write('ID ' + str(ID) + ' label ' + str(y[i])+'\n') # print('y[' + str(i) + ']= ' + str(y[i])) except cv2.error as e: print(e) # print('skipping id') continue # print('yyy = ' +str(keras.utils.to_categorical(y, num_classes=self.n_classes))) # print('X_gray = ' + str(X_gray)) # print('X_dog = ' + str(X_dog)) # print('X_lbp = ' + str(X_lbp)) return [X_gray, X_dog, X_lbp], label