### Install Project Dependencies Source: https://github.com/facenox/face-antispoof-onnx/blob/main/README.md Install all necessary Python packages listed in the requirements.txt file. Ensure you have Python 3.8.0 or higher. ```bash pip install -r requirements.txt ``` -------------------------------- ### Python Version Compatibility Error Example Source: https://github.com/facenox/face-antispoof-onnx/blob/main/README.md This example shows a typical error message encountered when attempting to install dependencies on an incompatible Python version (e.g., 3.7.x). ```text ERROR: Ignored the following versions that require a different python version: 0.1.0 Requires-Python >=3.9; ... ERROR: Could not find a version that satisfies the requirement puccinialin ERROR: No matching distribution found for puccinialin ``` -------------------------------- ### Run Face Antispoof Demo (Single Image) Source: https://github.com/facenox/face-antispoof-onnx/blob/main/README.md Use the demo script to analyze a single image for face anti-spoofing detection. Provide the path to the image file. ```bash python demo.py --image ``` -------------------------------- ### Run Face Antispoof Demo (Webcam) Source: https://github.com/facenox/face-antispoof-onnx/blob/main/README.md Execute the demo script to perform real-time face anti-spoofing detection using your webcam. You can specify a camera index if needed. ```bash python demo.py # or python demo.py --camera ``` -------------------------------- ### Resume Training Source: https://github.com/facenox/face-antispoof-onnx/blob/main/README.md Continue training from a specific checkpoint. Provide the cropped directory and the path to the checkpoint file. ```bash python scripts/train.py \ --crop_dir \ --resume ``` -------------------------------- ### Prepare Face Dataset Source: https://github.com/facenox/face-antispoof-onnx/blob/main/README.md Run the data preparation script to crop, resize, and organize face images for training. Specify original directory, cropped directory, target size, bounding box expansion factor, and spoof types. ```bash python scripts/prepare_data.py \ --orig_dir \ --crop_dir \ --size \ --bbox_expansion_factor \ --spoof_types [ ...] ``` -------------------------------- ### Create and Activate Virtual Environment (Conda) Source: https://github.com/facenox/face-antispoof-onnx/blob/main/README.md Use Conda to create and activate a new virtual environment for the project. This is recommended for managing dependencies. ```bash conda create -n face-antispoof python conda activate face-antispoof ``` -------------------------------- ### Create and Activate Virtual Environment (venv) Source: https://github.com/facenox/face-antispoof-onnx/blob/main/README.md Use Python's built-in venv module to create and activate a virtual environment. This is an alternative to Conda. ```bash python -m venv venv # Linux/macOS source venv/bin/activate # Windows venv\Scripts\activate ``` -------------------------------- ### Prepare Clean Model Weights Source: https://github.com/facenox/face-antispoof-onnx/blob/main/README.md Extract clean, inference-ready model weights from a checkpoint. This removes optimizer states and unnecessary prefixes. Specify the epoch checkpoint, output path, and input size. ```bash python scripts/prepare_best_model.py \ --output \ --input_size ``` -------------------------------- ### Train Face Anti-Spoofing Model Source: https://github.com/facenox/face-antispoof-onnx/blob/main/README.md Execute the training script using prepared cropped face data. Specify input size, batch size, and output directory for checkpoints and logs. ```bash python scripts/train.py \ --crop_dir \ --input_size \ --batch_size \ --output_dir ``` -------------------------------- ### Export Model to Quantized ONNX Source: https://github.com/facenox/face-antispoof-onnx/blob/main/README.md Convert a trained model checkpoint to a quantized ONNX format. This can reduce model size and improve inference speed. Specify the checkpoint path, input size, and output path. ```bash python scripts/quantize_onnx.py \ --input_size \ --output ``` -------------------------------- ### Export Model to Regular ONNX Source: https://github.com/facenox/face-antispoof-onnx/blob/main/README.md Convert a trained model checkpoint to the ONNX format for inference. Provide the checkpoint path, input size, and desired output path. ```bash python scripts/export_onnx.py \ --input_size \ --output ``` -------------------------------- ### Image Resizing Interpolation Logic Source: https://github.com/facenox/face-antispoof-onnx/blob/main/docs/DATA_PREPARATION.md Selects the interpolation method for resizing based on whether the crop size is smaller or larger than the target size. LANCZOS4 is used for upscaling, and AREA for downscaling. ```python interp = cv2.INTER_LANCZOS4 if crop_size < target_size else cv2.INTER_AREA ``` -------------------------------- ### Temporal Filtering for Video Anti-Spoofing Source: https://github.com/facenox/face-antispoof-onnx/blob/main/docs/LIMITATIONS.md This class implements a temporal filter that requires a specified number of consecutive 'real' predictions before accepting a frame as genuine. Use this for video streams to mitigate issues with motion blur and transient spoofing attempts. ```python from collections import deque class TemporalFilter: """Require N consecutive 'real' predictions before accepting.""" def __init__(self, required_frames=3): self.required_frames = required_frames self.history = deque(maxlen=required_frames) def update(self, is_real: bool) -> bool: """Update filter and return final decision.""" self.history.append(is_real) if len(self.history) < self.required_frames: return False # Not enough frames yet return all(self.history) # All must be 'real' ``` -------------------------------- ### Crop Face with Padding for Anti-Spoofing Source: https://github.com/facenox/face-antispoof-onnx/blob/main/docs/LIMITATIONS.md This function crops a face from an image with a specified padding factor, ensuring sufficient context for the anti-spoofing model. It resizes the cropped face to 128x128 pixels. Use this when the input face bounding box needs to be expanded to include surrounding facial features. ```python 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 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.