To ensure the model can detect spoofing artifacts (like screen pixels or print dots), inputs must follow specific preprocessing requirements:
- Padding: Use a 1.5x padding factor around the face bounding box. Tight crops lose the 3D structural context necessary to distinguish a face from a flat screen or paper.
- Resolution: The model resizes inputs to 128x128. However, the source face should be at least 64x64 pixels. Upscaling very small, blurry faces destroys the texture artifacts the model relies on.
- Lighting: Use even lighting. Avoid low-light environments or harsh backlighting (e.g., bright windows behind the subject), as these create noise in the Fourier Transform patterns used for analysis.
import cv2
import numpy as np
def crop_face_with_padding(image, bbox, padding_factor=1.5):
"""
Crop face with proper padding for anti-spoofing model.
Args:
image: Input image (numpy array)
bbox: Bounding box as (x, y, w, h)
padding_factor: Padding multiplier (default: 1.5)
Returns:
Cropped face image
"""
x, y, w, h = bbox
# Calculate center and expanded dimensions
center_x = x + w / 2
center_y = y + h / 2
max_dim = max(w, h)
new_size = int(max_dim * padding_factor)
# Calculate new bounding box
x1 = int(center_x - new_size / 2)
y1 = int(center_y - new_size / 2)
x2 = x1 + new_size
y2 = y1 + new_size
# Clamp to image boundaries
h_img, w_img = image.shape[:2]
x1 = max(0, x1)
y1 = max(0, y1)
x2 = min(w_img, x2)
y2 = min(h_img, y2)
# Crop and resize to 128x128
face_crop = image[y1:y2, x1:x2]
face_resized = cv2.resize(face_crop, (128, 128), interpolation=cv2.INTER_LANCZOS4)
return face_resized