├── CNN_compile.py ├── CNN_fit.py ├── CNN_initialize.py ├── LBP.py ├── README.md ├── cnn.py ├── get_pixel.py ├── lbp_pixel_value.py └── output_list.py /CNN_compile.py: -------------------------------------------------------------------------------- 1 | classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy']) 2 | -------------------------------------------------------------------------------- /CNN_fit.py: -------------------------------------------------------------------------------- 1 | from keras.preprocessing.image import ImageDataGenerator 2 | 3 | train_datagen = ImageDataGenerator(rescale = 1./255,shear_range = 0.2,zoom_range = 0.2,horizontal_flip = True) 4 | 5 | test_datagen = ImageDataGenerator(rescale = 1./255) 6 | 7 | training_set = train_datagen.flow_from_directory('dataset/training_set',target_size = (64, 64),batch_size = 32,class_mode = 'binary') 8 | 9 | test_set = test_datagen.flow_from_directory('dataset/test_set',target_size = (64, 64),batch_size = 32,class_mode = 'binary') 10 | 11 | classifier.fit_generator(training_set,samples_per_epoch = 8000,nb_epoch = 25,validation_data = test_set,nb_val_samples = 2000) 12 | -------------------------------------------------------------------------------- /CNN_initialize.py: -------------------------------------------------------------------------------- 1 | from keras.models import Sequential 2 | from keras.layers import Convolution2D 3 | from keras.layers import MaxPooling2D 4 | from keras.layers import Flatten 5 | from keras.layers import Dense 6 | 7 | 8 | classifier = Sequential() 9 | 10 | classifier.add(Convolution2D(32, 3, 3, input_shape = (64, 64, 3), activation = 'relu')) 11 | 12 | classifier.add(MaxPooling2D(pool_size = (2, 2))) 13 | 14 | classifier.add(Convolution2D(32, 3, 3, activation = 'relu')) 15 | 16 | classifier.add(MaxPooling2D(pool_size = (2, 2))) 17 | 18 | classifier.add(Flatten()) 19 | 20 | classifier.add(Dense(output_dim = 128, activation = 'relu')) 21 | 22 | classifier.add(Dense(output_dim = 1, activation = 'sigmoid')) 23 | -------------------------------------------------------------------------------- /LBP.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | import os 5 | import get_pixel 6 | import lbp_calculate 7 | 8 | 9 | def main(): 10 | imageDir = "/home/ubuntu/Desktop/Face Spoofing/data_normal/test_normal/001" #specify your path here 11 | image_path_list = [] 12 | for file in os.listdir(imageDir): 13 | image_path_list.append(os.path.join(imageDir, file)) 14 | out_list=[] 15 | outDir = "/home/ubuntu/Desktop/Face Spoofing/dataset/test_lbp/001" #specify the directory where the images should be stored 16 | d=1 17 | 18 | #loop through image_path_list to open each image 19 | for imagePath in image_path_list: 20 | img_bgr = cv2.imread(imagePath) 21 | 22 | height, width, channel = img_bgr.shape 23 | img_gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY) 24 | 25 | img_lbp = np.zeros((height, width,3), np.uint8) 26 | for i in range(0, height): 27 | for j in range(0, width): 28 | img_lbp[i, j] = lbp_calculated_pixel(img_gray, i, j) 29 | hist_lbp = cv2.calcHist([img_lbp], [0], None, [256], [0, 256]) 30 | output_list = [] 31 | ''' 32 | For printing the LBP and hist images from show_output function 33 | output_list.append({ 34 | "img": img_lbp, 35 | "xlabel": "", 36 | "ylabel": "", 37 | "xtick": [], 38 | "ytick": [], 39 | "title": "LBP Image", 40 | "type": "gray" 41 | }) 42 | ''' 43 | 44 | cv2.imwrite(os.path.join(outDir,'001_%d.jpg'%d),img_lbp) #numbering the images 45 | d+=1 46 | 47 | if __name__ == '__main__': 48 | main() 49 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Face-Spoofing-Detection 2 | Deep Texture feature extraction and implementing Local Binary Pattern(LBP)-based Convolutional Neural Network 3 | -------------------------------------------------------------------------------- /cnn.py: -------------------------------------------------------------------------------- 1 | from keras.models import Sequential 2 | from keras.layers import Convolution2D 3 | from keras.layers import MaxPooling2D 4 | from keras.layers import Flatten 5 | from keras.layers import Dense 6 | 7 | 8 | classifier = Sequential() 9 | 10 | classifier.add(Convolution2D(32, 3, 3, input_shape = (64, 64, 3), activation = 'relu')) 11 | 12 | classifier.add(MaxPooling2D(pool_size = (2, 2))) 13 | 14 | classifier.add(Convolution2D(32, 3, 3, activation = 'relu')) 15 | classifier.add(MaxPooling2D(pool_size = (2, 2))) 16 | 17 | classifier.add(Flatten()) 18 | 19 | classifier.add(Dense(output_dim = 128, activation = 'relu')) 20 | classifier.add(Dense(output_dim = 1, activation = 'sigmoid')) 21 | 22 | classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy']) 23 | 24 | 25 | from keras.preprocessing.image import ImageDataGenerator 26 | 27 | train_datagen = ImageDataGenerator(rescale = 1./255, 28 | shear_range = 0.2, 29 | zoom_range = 0.2, 30 | horizontal_flip = True) 31 | 32 | test_datagen = ImageDataGenerator(rescale = 1./255) 33 | 34 | training_set = train_datagen.flow_from_directory('dataset/training_set', 35 | target_size = (64, 64), 36 | batch_size = 32, 37 | class_mode = 'binary') 38 | 39 | test_set = test_datagen.flow_from_directory('dataset/test_set', 40 | target_size = (64, 64), 41 | batch_size = 32, 42 | class_mode = 'binary') 43 | 44 | classifier.fit_generator(training_set, 45 | samples_per_epoch = 8000, 46 | nb_epoch = 25, 47 | validation_data = test_set, 48 | nb_val_samples = 2000) -------------------------------------------------------------------------------- /get_pixel.py: -------------------------------------------------------------------------------- 1 | def get_pixel(img, center, x, y): 2 | new_value = 0 3 | try: 4 | if img[x][y] >= center: 5 | new_value = 1 6 | except: 7 | pass 8 | return new_value 9 | -------------------------------------------------------------------------------- /lbp_pixel_value.py: -------------------------------------------------------------------------------- 1 | import os 2 | import get_pixel 3 | 4 | def lbp_calculate(img, x, y): 5 | 6 | center = img[x][y] 7 | val_ar = [] 8 | val_ar.append(get_pixel(img, center, x-1, y+1)) # top_right 9 | val_ar.append(get_pixel(img, center, x, y+1)) # right 10 | val_ar.append(get_pixel(img, center, x+1, y+1)) # bottom_right 11 | val_ar.append(get_pixel(img, center, x+1, y)) # bottom 12 | val_ar.append(get_pixel(img, center, x+1, y-1)) # bottom_left 13 | val_ar.append(get_pixel(img, center, x, y-1)) # left 14 | val_ar.append(get_pixel(img, center, x-1, y-1)) # top_left 15 | val_ar.append(get_pixel(img, center, x-1, y)) # top 16 | 17 | power_val = [1, 2, 4, 8, 16, 32, 64, 128] 18 | val = 0 19 | for i in range(len(val_ar)): 20 | val += val_ar[i] * power_val[i] 21 | return val 22 | -------------------------------------------------------------------------------- /output_list.py: -------------------------------------------------------------------------------- 1 | def show_output(output_list): 2 | ''' 3 | For showing the histogram and lbp image 4 | ''' 5 | output_list_len = len(output_list) 6 | figure = plt.figure() 7 | for i in range(output_list_len): 8 | current_dict = output_list[i] 9 | current_img = current_dict["img"] 10 | current_xlabel = current_dict["xlabel"] 11 | current_ylabel = current_dict["ylabel"] 12 | current_xtick = current_dict["xtick"] 13 | current_ytick = current_dict["ytick"] 14 | current_title = current_dict["title"] 15 | current_type = current_dict["type"] 16 | current_plot = figure.add_subplot(1, output_list_len, i+1) 17 | if current_type == "gray": 18 | current_plot.imshow(current_img, cmap = plt.get_cmap('gray')) 19 | current_plot.set_title(current_title) 20 | current_plot.set_xticks(current_xtick) 21 | current_plot.set_yticks(current_ytick) 22 | current_plot.set_xlabel(current_xlabel) 23 | current_plot.set_ylabel(current_ylabel) 24 | elif current_type == "histogram": 25 | current_plot.plot(current_img, color = "black") 26 | current_plot.set_xlim([0,260]) 27 | current_plot.set_title(current_title) 28 | current_plot.set_xlabel(current_xlabel) 29 | current_plot.set_ylabel(current_ylabel) 30 | ytick_list = [int(i) for i in current_plot.get_yticks()] 31 | current_plot.set_yticklabels(ytick_list,rotation = 90) 32 | 33 | plt.show() 34 | --------------------------------------------------------------------------------