# Face Spoofing Detection with Stacking Ensembles in Work Time Registration System
## 3 The Proposed Method
### Face Context Analysis  
This module enhances spoof detection by analyzing regions surrounding the face for inconsistencies. The algorithm aims to detect irregularities in the input image, such as sharp edges or objects, by comparing the area close to the detected face against more distant areas. The advantage of this approach is that it allows the system to more accurately detect spoofed faces displayed on electronic devices or printed by analyzing not just the face, but also its close surroundings.  
The area close to the face (the \"near\" context) is defined as 40% of the width and height of the face outside of the detected face rectangle in all directions. The area further away from the face (the \"far\" context) is defined as an additional 40% in all directions outside of the close area.  
For both areas, edge detection is performed using the Sobel operator. Then, for the detected edges, average pixel intensity is calculated separately for the area closer to the face and further away from it. Then, histograms are computed with the calculated intensities over 64 bins. This method could also be interpreted as a sharpness analysis of the image, as it is based on the amount and intensity of the detected edges. A sample output of this part of the algorithm is shown in Figure 8.  
Then, for the histograms generated this way-separately for the close and far areas, four metrics are calculated and compared to each histogram from the dataset already generated this way. Those four metrics are Correlation, Chi-Square, Intersection, and Bhattacharyya distance. The distance between the histograms is then defined as the sum of all four metrics. This calculation is shown in Equation (5). It is important to normalize the metrics by scaling them down to a range of [0; 1], as they all cover different ranges. For Correlation and Intersection, the calculated value is subtracted from 1, as the value  
Figure 8: An example of edges for a spoof image displayed on a smartphone.  
histograms are different. This ensures that the value of 1 represents two very different histograms for all matrices and thus a possible spoofing attempt.  
\[correlation(H_{1},H_{2})=1-norm\Bigg{(}\frac{\sum_{l}(H_{1}(I)-\hat{H_{1}})(H_{2} (I)-\hat{H_{2}})}{\sqrt{\sum_{l}(H_{1}(I)-\hat{H_{1}})^{2}\sum_{l}(H_{2}(I)- \hat{H_{2}})^{2}}}\Bigg{)} \tag{1}\]  
\[chi\_square(H_{1},H_{2})=norm\Bigg{(}\sum_{l}\frac{(H_{1}(I)-H_{2}(I))^{2}}{H_ {1}(I)}\Bigg{)} \tag{2}\]  
\[intersection(H_{1},H_{2})=1-norm\Bigg{(}\sum_{l}\min(H_{1}(I),H_{2}(I))\Bigg{)} \tag{3}\]  
\[bhattacharyya(H_{1},H_{2})=norm\Bigg{(}\sqrt{1-\frac{1}{\sqrt{\hat{H_{1}}\hat{H _{2}}N^{2}}}\sum_{l}\sqrt{H_{1}(I)\cdot H_{2}(I)}}\Bigg{)} \tag{4}\]  
\[\begin{array}{l}D(H_{1},H_{2})=(correlation(H_{1},H_{2})+chi\_square(H_{1},H_ {2}))\\ \hskip 142.26378 pt+(intersection(H_{1},H_{2})+bhattacharyya(H_{1},H_{2}))\end{array} \tag{5}\]  
where \(norm(x)=\frac{1}{1+e^{-x}}\) and \(\hat{H_{k}}=\frac{1}{N}\sum_{l}H_{k}(J)\). \(H_{1}\) and \(H_{2}\) represent the compared histograms.  
Based on these distances, a kNN algorithm is performed to select the three closest neighbors (with the lowest distance to the new histogram), and the spoofing probability is calculated depending on how many of those histograms belong to authentic images and spoof images.  
This method strengthens the system's ability to detect inconsistencies by focusing not only on the face but also on the broader context of the surrounding image. By comparing edge intensities in two areas around the face to known distributions, it is possible to detect sudden changes and edges in the image's foreground that could indicate a spoof attempt.