### Setup Autoreload and Import Libraries in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/try_mtcnn_step_by_step.ipynb Enables autoreload for Jupyter notebooks and imports necessary libraries including NumPy, PyTorch, PIL, and custom modules from InsightFace for face detection. This setup prepares the environment for running the pipeline without dependencies on external installations beyond the listed imports. Inputs are none, output is the configured Python environment; it assumes the custom src modules are available and may fail if they are not in the path. ```python %load_ext autoreload %autoreload 2 import numpy as np import torch from PIL import Image from torch.autograd import Variable from src.get_nets import PNet, RNet, ONet from src.box_utils import nms, calibrate_box, get_image_boxes, convert_to_square from src.first_stage import run_first_stage from src.visualization_utils import show_bboxes ``` -------------------------------- ### Configure InsightFace PyTorch Setup Source: https://context7.com/treb1en/insightface_pytorch/llms.txt This code initializes and customizes configuration objects for training or inference modes in InsightFace PyTorch. It supports setting paths, batch sizes, learning rates, and thresholds using the config module. Inputs include training boolean, and outputs are configuration objects. ```python from config import get_config import torch # Get configuration for training mode conf = get_config(training=True) print(f"Model path: {conf.model_path}") print(f"Device: {conf.device}") print(f"Batch size: {conf.batch_size}") print(f"Learning rate: {conf.lr}") print(f"Input size: {conf.input_size}") print(f"Embedding size: {conf.embedding_size}") # Get configuration for inference mode conf_infer = get_config(training=False) print(f"Facebank path: {conf_infer.facebank_path}") print(f"Threshold: {conf_infer.threshold}") print(f"Face limit: {conf_infer.face_limit}") print(f"Min face size: {conf_infer.min_face_size}") # Customize configuration conf.net_mode = 'ir_se' # or 'ir' conf.net_depth = 50 # or 100, 152 conf.use_mobilfacenet = False conf.batch_size = 100 conf.lr = 1e-3 ``` -------------------------------- ### Detect faces and display bounding boxes on images using InsightFace PyTorch Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/test_on_images.ipynb Loads an image, runs detect_faces to obtain bounding boxes and facial landmarks, and visualizes them with show_bboxes. Includes examples with default settings, custom detection thresholds, and a minimum face size constraint. ```python img = Image.open('images/office1.jpg') bounding_boxes, landmarks = detect_faces(img) show_bboxes(img, bounding_boxes, landmarks) ``` ```python img = Image.open('images/office2.jpg') bounding_boxes, landmarks = detect_faces(img) show_bboxes(img, bounding_boxes, landmarks) ``` ```python img = Image.open('images/office4.jpg') bounding_boxes, landmarks = detect_faces(img, thresholds=[0.6, 0.7, 0.85]) show_bboxes(img, bounding_boxes, landmarks) ``` ```python img = Image.open('images/office5.jpg') bounding_boxes, landmarks = detect_faces(img, min_face_size=10.0) show_bboxes(img, bounding_boxes, landmarks) ``` -------------------------------- ### Setup IPython Environment Source: https://github.com/treb1en/insightface_pytorch/blob/master/evaluate_model.ipynb Configures the IPython environment for automatic module reloading and inline plotting. This is useful during development to see changes without restarting the kernel. ```python %load_ext autoreload %autoreload 2 %matplotlib inline ``` -------------------------------- ### Video File Face Recognition Source: https://context7.com/treb1en/insightface_pytorch/llms.txt Processes video files to detect and recognize faces, annotating the output video with identity labels. Allows specifying input/output file names, recognition threshold, confidence scores, test-time augmentation, and processing specific time segments (start time and duration). ```bash # Process video file from data/facebank/ folder python infer_on_video.py -f input_video.mp4 -s output_name # Process with custom settings python infer_on_video.py \ -f conference.mp4 \ -s conference_annotated \ --threshold 1.3 \ --score \ --tta # Process only specific time segment (10 seconds starting at 30s) python infer_on_video.py -f video.mp4 -s clip -b 30 -d 10 ``` -------------------------------- ### Get Reference Facial Points in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/refine_faces.ipynb This code retrieves a set of reference facial points, typically used for aligning faces during processing. The `default_square=True` argument suggests it obtains points suitable for a square crop. ```python reference_pts = get_reference_facial_points(default_square= True) ``` -------------------------------- ### Command-Line Interface for Model Training Source: https://context7.com/treb1en/insightface_pytorch/llms.txt Provides command-line arguments for initiating model training with customizable parameters such as network architecture, depth, batch size, learning rate, epochs, dataset, and worker threads. This script allows flexible training configurations. ```bash # Train IR-SE50 model on emore dataset python train.py -net ir_se -depth 50 -b 96 -lr 0.001 -e 20 -d emore -w 3 # Train MobileFaceNet with larger batch size python train.py -net mobilefacenet -b 200 -lr 0.001 -e 25 -w 4 # Train IR100 on concatenated VGG and MS1M datasets python train.py -net ir_se -depth 100 -b 64 -lr 0.0001 -e 30 -d concat # Parameters: # -net: Network architecture (ir, ir_se, mobilefacenet) # -depth: Network depth (50, 100, 152) # -b: Batch size # -lr: Learning rate # -e: Number of epochs # -d: Dataset mode (vgg, ms1m, emore, concat) # -w: Number of worker threads ``` -------------------------------- ### Get Image Dimensions in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/refine_faces.ipynb This simple code snippet retrieves and returns the dimensions (width and height) of a PIL Image object. This is useful for verifying image sizes after loading or processing. ```python image.size ``` -------------------------------- ### Initialize Face Learner with Configuration Source: https://github.com/treb1en/insightface_pytorch/blob/master/evaluate_model.ipynb Initializes the face learner with configuration settings. The `training=False` argument indicates that the learner is being set up for inference rather than training. ```python conf = get_config(training=False) learner = face_learner(conf, inference=True) ``` -------------------------------- ### Display bounding boxes and landmarks on image Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/try_mtcnn_step_by_step.ipynb Uses the helper function show_bboxes to render bounding boxes and associated landmarks on the provided image. Requires an image object, bounding_boxes array, and landmarks array. Returns a PIL.Image object with visual annotations. ```python show_bboxes(image, bounding_boxes, landmarks) ``` -------------------------------- ### Load Face Detection Models in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/try_mtcnn_step_by_step.ipynb Instantiates P-Net, R-Net, and O-Net models using the get_nets module and sets O-Net to evaluation mode. This initializes the neural networks required for the multi-stage detection process. It depends on the custom src.get_nets module and assumes GPU availability if needed; outputs are the model objects, but it does not load weights. ```python pnet = PNet() rnet = RNet() onet = ONet() onet.eval(); ``` -------------------------------- ### Import Core Libraries Source: https://github.com/treb1en/insightface_pytorch/blob/master/evaluate_model.ipynb Imports essential libraries for the face recognition project. This includes configuration loading, argument parsing, the face learner class, data pipeline utilities, and image transformations. ```python from config import get_config import argparse from Learner import face_learner from data.data_pipe import get_val_pair from torchvision import transforms as trans ``` -------------------------------- ### Load and Display Image in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/try_mtcnn_step_by_step.ipynb Opens an image file using PIL and displays it, serving as input for the face detection pipeline. This assumes the image file 'images/office5.jpg' exists in the directory. Outputs the PIL image object; it may raise an error if the file is not found, and it's limited to JPG format as shown. ```python image = Image.open('images/office5.jpg') image ``` -------------------------------- ### Run P-Net First Stage Detection in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/try_mtcnn_step_by_step.ipynb Executes the first detection stage using P-Net across all scales, collecting bounding boxes. This processes the image at multiple resolutions for initial face candidate detection. Inputs are the image, pnet model, scales, and threshold; outputs are stacked bounding boxes; it can be memory-intensive with many scales. ```python bounding_boxes = [] # run P-Net on different scales for s in scales: boxes = run_first_stage(image, pnet, scale=s, threshold=thresholds[0]) bounding_boxes.append(boxes) # collect boxes (and offsets, and scores) from different scales bounding_boxes = [i for i in bounding_boxes if i is not None] bounding_boxes = np.vstack(bounding_boxes) print('number of bounding boxes:', len(bounding_boxes)) ``` -------------------------------- ### Run O-Net Third Stage Detection in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/try_mtcnn_step_by_step.ipynb Executes final stage with O-Net to predict landmarks, offsets, and probabilities. This extracts 48x48 patches, runs through O-Net, and filters high-confidence boxes. Inputs bounding_boxes and image; outputs landmarks, offsets, probs, and filtered bounding_boxes; culminates detection with facial landmark estimation. ```python img_boxes = get_image_boxes(bounding_boxes, image, size=48) img_boxes = Variable(torch.FloatTensor(img_boxes), volatile=True) output = onet(img_boxes) landmarks = output[0].data.numpy() # shape [n_boxes, 10] offsets = output[1].data.numpy() # shape [n_boxes, 4] probs = output[2].data.numpy() # shape [n_boxes, 2] keep = np.where(probs[:, 1] > thresholds[2])[0] bounding_boxes = bounding_boxes[keep] bounding_boxes[:, 4] = probs[keep, 1].reshape((-1,)) offsets = offsets[keep] landmarks = landmarks[keep] ``` -------------------------------- ### Model Training and Saving in PyTorch Source: https://context7.com/treb1en/insightface_pytorch/llms.txt Demonstrates basic model training, error handling during training, and saving/loading model states using a PyTorch learner. Relies on a 'learner' object and configuration settings. ```python try: learner.train(conf, epochs=20) print("Training completed successfully") except Exception as e: print(f"Training error: {e}") # Save model statelearner.save_state(conf, accuracy=0.95, extra='checkpoint') # Load model state for resuming training # learner.load_state(conf, 'model_checkpoint.pth', model_only=False) ``` -------------------------------- ### Run R-Net Second Stage Detection in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/try_mtcnn_step_by_step.ipynb Feeds refined bounding boxes to R-Net for second-stage classification and offset prediction. This refines face candidates by extracting image patches and running them through the network. Inputs are bounding_boxes and image; outputs offsets and probabilities; depends on PyTorch Variable. ```python img_boxes = get_image_boxes(bounding_boxes, image, size=24) img_boxes = Variable(torch.FloatTensor(img_boxes), volatile=True) output = rnet(img_boxes) offsets = output[0].data.numpy() # shape [n_boxes, 4] probs = output[1].data.numpy() # shape [n_boxes, 2] keep = np.where(probs[:, 1] > thresholds[1])[0] bounding_boxes = bounding_boxes[keep] bounding_boxes[:, 4] = probs[keep, 1].reshape((-1,)) offsets = offsets[keep] print('number of bounding boxes:', len(bounding_boxes)) ``` -------------------------------- ### Model Initialization - Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/evaluate_model.ipynb Initializes a face learner object for inference using the given configuration. This generates the MobileFaceNet model and outputs the configuration and confirmation message. ```python learner = face_learner(conf, inference=True) ``` -------------------------------- ### Visualize Bounding Boxes After P-Net in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/try_mtcnn_step_by_step.ipynb Displays the image with overlaid bounding boxes using a visualization utility. This helps inspect the raw detection results from P-Net. Inputs are image and bounding_boxes; outputs a PIL image; it assumes the visualization function is available. ```python show_bboxes(image, bounding_boxes) ``` -------------------------------- ### Model Loading - Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/evaluate_model.ipynb Loads the model state from a .pth file into the learner for both training and inference modes, requiring the configuration object. This prepares the model for evaluation tasks. ```python learner.load_state(conf, 'mobilefacenet.pth', True, True) ``` -------------------------------- ### Data Preparation: MXNet Records to PyTorch Images Source: https://context7.com/treb1en/insightface_pytorch/llms.txt Converts data from MXNet record files into a PyTorch-compatible image folder structure and prepares validation sets. It requires the `pathlib`, `config`, and `data.data_pipe` modules. Outputs extracted images and validation data. ```python from pathlib import Path from config import get_config from data.data_pipe import load_bin, load_mx_rec # Setup configuration conf = get_config() rec_path = conf.data_path / 'faces_emore' # Load MXNet record files and convert to image folders # This creates a folder structure: faces_emore/imgs/{class_id}/{image_id}.jpg load_mx_rec(rec_path) print(f"Training images extracted to {rec_path / 'imgs'}") # Load validation binary files bin_files = ['agedb_30', 'cfp_fp', 'lfw', 'calfw', 'cfp_ff', 'cplfw', 'vgg2_fp'] for bin_file in bin_files: bin_path = rec_path / (bin_file + '.bin') output_path = rec_path / bin_file try: data, issame_list = load_bin(bin_path, output_path, conf.test_transform) print(f"Loaded {bin_file}: {data.shape}, pairs: {len(issame_list)}") except Exception as e: print(f"Error loading {bin_file}: {e}") # Expected directory structure after preparation: # faces_emore/ # ├── imgs/ (training images by class) # ├── agedb_30/ (validation set) # ├── lfw/ (validation set) # └── cfp_fp/ (validation set) ``` -------------------------------- ### Compute facial landmark points from bounding boxes using NumPy Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/try_mtcnn_step_by_step.ipynb Calculates width and height of each bounding box, expands coordinates, and derives landmark positions. Requires NumPy and input arrays bounding_boxes and landmarks. Outputs updated landmarks and prints the count of bounding boxes. ```python # compute landmark points width = bounding_boxes[:, 2] - bounding_boxes[:, 0] + 1.0 height = bounding_boxes[:, 3] - bounding_boxes[:, 1] + 1.0 xmin, ymin = bounding_boxes[:, 0], bounding_boxes[:, 1] landmarks[:, 0:5] = np.expand_dims(xmin, 1) + np.expand_dims(width, 1)*landmarks[:, 0:5] landmarks[:, 5:10] = np.expand_dims(ymin, 1) + np.expand_dims(height, 1)*landmarks[:, 5:10] print('number of bounding boxes:', len(bounding_boxes)) ``` -------------------------------- ### Load MXNet Indexed RecordIO in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/refine_faces.ipynb This code initializes an MXNet IndexedRecordIO object for reading data from a specified record file and its index file. It opens the dataset in read mode ('r'), preparing it for subsequent data retrieval. ```python imgrec = mx.recordio.MXIndexedRecordIO(str(idx_path), str(bin_path), 'r') ``` -------------------------------- ### Load Extensions and Imports in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/refine_faces.ipynb This snippet loads the autoreload extension for interactive development and imports necessary libraries for image processing, deep learning, and file path manipulation. It sets up the environment for the subsequent code execution. ```python %load_ext autoreload %autoreload 2 from src import detect_faces, show_bboxes from PIL import Image import cv2 import numpy as np from src.align_trans import get_reference_facial_points, warp_and_crop_face import mxnet as mx import io from pathlib import Path ``` -------------------------------- ### Apply NMS and Calibrate Boxes After R-Net in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/try_mtcnn_step_by_step.ipynb Applies another round of NMS, calibration, and squaring to R-Net outputs. Further refines detections by removing overlaps and adjusting boxes. Inputs bounding_boxes and offsets; outputs processed boxes; maintains NumPy shape [n_boxes, 5]. ```python keep = nms(bounding_boxes, nms_thresholds[1]) bounding_boxes = bounding_boxes[keep] bounding_boxes = calibrate_box(bounding_boxes, offsets[keep]) bounding_boxes = convert_to_square(bounding_boxes) bounding_boxes[:, 0:4] = np.round(bounding_boxes[:, 0:4]) print('number of bounding boxes:', len(bounding_boxes)) ``` -------------------------------- ### Enable autoreload and import detection modules in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/test_on_images.ipynb Loads the IPython autoreload extension for automatic module reloading and imports the detect_faces and show_bboxes functions from the src package along with Pillow's Image class. This prepares the environment for face detection without additional external dependencies. ```python %load_ext autoreload %autoreload 2 from src import detect_faces, show_bboxes from PIL import Image ``` -------------------------------- ### Load image and detect faces Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/get_aligned_face_from_mtcnn.ipynb Loads an image and detects faces with landmarks using InsightFace's detect_faces function. Requires PIL for image handling and numpy for array operations. Outputs bounding boxes and facial landmarks. ```python from src import detect_faces, show_bboxes from PIL import Image import cv2 import numpy as np from src.align_trans import get_reference_facial_points, warp_and_crop_face img = Image.open('images/jf.jpg') img_cv2 = np.array(img)[...,::-1] bounding_boxes, landmarks = detect_faces(img) ``` -------------------------------- ### Face Bank Preparation for Recognition Source: https://context7.com/treb1en/insightface_pytorch/llms.txt Generates embeddings for a set of known identities to create a face bank used for real-time recognition. This involves loading a trained model, using a face detector (MTCNN), and preparing the embeddings and names. It also shows how to load an existing face bank. ```python from pathlib import Path from config import get_config from mtcnn import MTCNN from Learner import face_learner from utils import prepare_facebank, load_facebank import torch # Setup conf = get_config(training=False) mtcnn = MTCNN() # Initialize learner and load trained model learner = face_learner(conf, inference=True) learner.load_state(conf, 'final.pth', from_save_folder=True, model_only=True) learner.model.eval() # Prepare face bank structure: # data/facebank/ # ├── person1/ # │ ├── photo1.jpg # │ └── photo2.jpg # ├── person2/ # │ └── photo1.jpg # Generate embeddings for all faces in facebank embeddings, names = prepare_facebank(conf, learner.model, mtcnn, tta=True) print(f"Face bank created with {len(names)} identities") print(f"Names: {names}") print(f"Embeddings shape: {embeddings.shape}") # [num_people, 512] # Load existing face bank embeddings_loaded, names_loaded = load_facebank(conf) print(f"Loaded {len(names_loaded)} identities from saved face bank") ``` -------------------------------- ### Process Detected Faces and Align in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/refine_faces.ipynb This snippet iterates through detected faces, extracts bounding box and landmark information, adjusts box coordinates, and uses `warp_and_crop_face` to align and crop each face to a standard size (112x112). It converts the processed face data back into PIL Image objects and stores them in a list. It utilizes `tqdm` for progress visualization. ```python from tqdm import tqdm faces = [] img_cv2 = np.array(image)[...,::-1] for i in tqdm(range(len(bounding_boxes))): box = bounding_boxes[i][:4].astype(np.int32).tolist() for idx, coord in enumerate(box[:2]): if coord > 1: box[idx] -= 1 if box[2] + 1 < img_cv2.shape[1]: box[2] += 1 if box[3] + 1 < img_cv2.shape[0]: box[3] += 1 face = img_cv2[box[1]:box[3],box[0]:box[2]] landmark = landmarks[i] facial5points = [[landmark[j] - box[0],landmark[j+5] - box[1]] for j in range(5)] dst_img = warp_and_crop_face(face,facial5points, crop_size=(112,112)) faces.append(Image.fromarray(dst_img[...,::-1])) ``` -------------------------------- ### Warp and Crop Face with Reference Points in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/refine_faces.ipynb This snippet performs face alignment and cropping using the `warp_and_crop_face` function. It takes the raw face image, detected facial landmarks, predefined reference points, and a target crop size to produce a standardized facial image. ```python dst_img = warp_and_crop_face(face, facial5points, reference_pts, crop_size=(112,112)) ``` -------------------------------- ### Train Face Recognition Model in PyTorch Source: https://context7.com/treb1en/insightface_pytorch/llms.txt This code sets up training for a face recognition model using ArcFace loss on prepared datasets. It requires the config and Learner modules, configuring parameters like network mode and batch size. The learner is initialized for training mode, but the snippet is incomplete. ```python from config import get_config from Learner import face_learner import argparse # Setup configuration conf = get_config(training=True) conf.net_mode = 'ir_se' # 'ir' or 'ir_se' conf.net_depth = 50 # 50, 100, or 152 conf.use_mobilfacenet = False conf.lr = 1e-3 conf.batch_size = 96 conf.num_workers = 3 conf.data_mode = 'emore' # 'vgg', 'ms1m', 'emore', or 'concat' # Initialize learner learner = face_learner(conf, inference=False) ``` -------------------------------- ### Set Face Detection Parameters in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/try_mtcnn_step_by_step.ipynb Defines minimum face size, probability thresholds for each stage, and NMS thresholds to control detection sensitivity and overlap removal. These parameters tune the algorithm's accuracy versus computational cost. Inputs are hardcoded values; outputs are the set variables; changing thresholds can affect detection recall and precision, with lower values increasing false positives. ```python # if this value is too low the algorithm will use a lot of memory min_face_size = 15.0 # for probabilities thresholds = [0.6, 0.7, 0.8] # for NMS nms_thresholds=[0.7, 0.7, 0.7] ``` -------------------------------- ### Capture Photos for Face Bank (Bash) Source: https://context7.com/treb1en/insightface_pytorch/llms.txt Captures and aligns face photos from a webcam to add to the face bank. The script uses MTCNN for real-time face detection and allows capturing multiple photos for improved accuracy. Photos are automatically aligned and saved. ```bash # Capture photos for a new person python take_pic.py -n john_doe # Example: Capture multiple photos for better accuracy python take_pic.py -n alice ``` -------------------------------- ### Evaluate Model on CFP FP Dataset Source: https://github.com/treb1en/insightface_pytorch/blob/master/evaluate_model.ipynb Tests the face learner's performance on the CFP FP (Celebrities in Frontal-Profile in the Wild) dataset, specifically focusing on frontal-to-profile matching. It computes accuracy and the best threshold. ```python cfp_fp, cfp_fp_issame = get_val_pair(conf.emore_folder, 'cfp_fp') accuracy, best_threshold, roc_curve_tensor = learner.evaluate(conf, cfp_fp, cfp_fp_issame, nrof_folds=10, tta=True) print('cfp_fp - accuray:{}, threshold:{}'.format(accuracy, best_threshold)) ``` -------------------------------- ### Prepare reference facial points Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/get_aligned_face_from_mtcnn.ipynb Defines and scales reference facial points for alignment based on image dimensions. Uses numpy arrays for point manipulation. Scales points according to image size ratios. ```python src = np.array([[30.2946, 51.6963], [65.5318, 51.5014], [48.0252, 71.7366], [33.5493, 92.3655], [62.7299, 92.2041]], dtype=np.float32) src[:,0] *= (img.size[0]/96) src[:,1] *= (img.size[1]/112) ``` -------------------------------- ### Build Image Scale Pyramid in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/try_mtcnn_step_by_step.ipynb Calculates a list of scaling factors to create an image pyramid for multi-scale face detection, targeting the minimum face size. This uses the image dimensions and predefined min_detection_size and factor to generate scales. Inputs are image size and parameters; outputs are the scales list; it's efficient for varying image sizes but assumes fixed factor and min sizes. ```python width, height = image.size min_length = min(height, width) min_detection_size = 12 factor = 0.707 # sqrt(0.5) # scales for scaling the image scales = [] # scales the image so that # minimum size that we can detect equals to # minimum face size that we want to detect m = min_detection_size/min_face_size min_length *= m factor_count = 0 while min_length > min_detection_size: scales.append(m*factor**factor_count) min_length *= factor factor_count += 1 print('scales:', ['{:.2f}'.format(s) for s in scales]) print('number of different scales:', len(scales)) ``` -------------------------------- ### Detect, Align, and Visualize Faces with MTCNN Source: https://context7.com/treb1en/insightface_pytorch/llms.txt This snippet demonstrates how to use the MTCNN detector to find multiple faces in an image, align them, and then visualize the bounding boxes on the original image. It outputs the number of detected faces, their bounding box coordinates and confidence scores, and saves aligned faces as individual images and the detection visualization as 'detections.jpg'. ```python from insightface.app import FaceAnalysis import cv2 import matplotlib.pyplot as plt import numpy as np # Initialize FaceAnalysis model (MTCNN detector) # You might need to download the model weights first app = FaceAnalysis() app.prepare(ctx_id=0, det_size=(640, 640)) # Load an image image_path = 'your_image.jpg' # Replace with your image path img = cv2.imread(image_path) if img is None: raise FileNotFoundError(f"Image not found at {image_path}") # Detect and align faces # The align_multi function returns bounding boxes and aligned faces # limit: maximum number of faces to detect # min_face_size: minimum face size to consider # The output 'faces' are cropped and aligned face images bboxes, faces = app.get(img, limit=10, min_face_size=30) print(f"Detected {len(faces)} faces") if bboxes is not None: print(f"Bounding boxes shape: {bboxes.shape}") # [num_faces, 5] (x1,y1,x2,y2,score) # Process each detected face for i, (bbox, face) in enumerate(zip(bboxes, faces)): x1, y1, x2, y2, confidence = bbox print(f"Face {i+1}: bbox=({x1:.0f},{y1:.0f},{x2:.0f},{y2:.0f}), confidence={confidence:.3f}") # Save the aligned face face_img_path = f'aligned_face_{i+1}.jpg' cv2.imwrite(face_img_path, face.image) print(f"Saved aligned face to {face_img_path}") # Visualize detections on the original image img_array = np.array(img) plt.figure(figsize=(12, 8)) plt.imshow(cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB)) if bboxes is not None: for bbox in bboxes: x1, y1, x2, y2 = bbox[:4].astype(int) plt.gca().add_patch(plt.Rectangle((x1, y1), x2-x1, y2-y1, fill=False, edgecolor='red', linewidth=2)) plt.axis('off') vis_output_path = 'detections.jpg' plt.savefig(vis_output_path, bbox_inches='tight') print(f"Saved detection visualization to {vis_output_path}") ``` -------------------------------- ### Detect faces and landmarks with MTCNN in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/README.md This code snippet demonstrates how to use the MTCNN implementation to detect faces and facial landmarks in an image. It requires the PIL library for image handling and returns bounding boxes and landmarks for detected faces. ```python from src import detect_faces from PIL import Image image = Image.open('image.jpg') bounding_boxes, landmarks = detect_faces(image) ``` -------------------------------- ### Extract and align multiple faces Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/get_aligned_face_from_mtcnn.ipynb Extracts multiple faces from image and aligns each using facial landmarks. Processes bounding boxes and landmarks for all detected faces. Includes tqdm progress bar for batch processing. ```python from tqdm import tqdm_notebook as tqdm bounding_boxes, landmarks = detect_faces(img) faces = [] img_cv2 = np.array(img)[...,::-1] for i in tqdm(range(len(bounding_boxes))): box = bounding_boxes[i][:4].astype(np.int32).tolist() for idx, coord in enumerate(box[:2]): if coord > 1: box[idx] -= 1 if box[2] + 1 < img_cv2.shape[1]: box[2] += 1 if box[3] + 1 < img_cv2.shape[0]: box[3] += 1 face = img_cv2[box[1]:box[3],box[0]:box[2]] landmark = landmarks[i] facial5points = [[landmark[j] - box[0],landmark[j+5] - box[1]] for j in range(5)] dst_img = warp_and_crop_face(face,facial5points) faces.append(Image.fromarray(dst_img[...,::-1])) ``` -------------------------------- ### Evaluate Model on CFP FF Dataset Source: https://github.com/treb1en/insightface_pytorch/blob/master/evaluate_model.ipynb Evaluates the face learner on the CFP FF (Celebrities in Frontal-Profile in the Wild) dataset. The evaluation focuses on accuracy and the optimal threshold for face comparison. ```python cfp_ff, cfp_ff_issame = get_val_pair(conf.emore_folder, 'cfp_ff') accuracy, best_threshold, roc_curve_tensor = learner.evaluate(conf, cfp_ff, cfp_ff_issame, nrof_folds=10, tta=True) print('cfp_ff - accuray:{}, threshold:{}'.format(accuracy, best_threshold)) ``` -------------------------------- ### Define Face Dataset Paths in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/refine_faces.ipynb This code snippet defines the file paths for a face dataset, specifically pointing to a directory containing training data in the MXNet recordio format (.rec) and its corresponding index file (.idx). It uses the `pathlib` module for object-oriented filesystem path manipulation. ```python face_folder = Path('/home/f/learning/Dataset/faces_vgg_112x112') bin_path = face_folder/'train.rec' idx_path = face_folder/'train.idx' ``` -------------------------------- ### Switch to MobileFaceNet Model Source: https://github.com/treb1en/insightface_pytorch/blob/master/evaluate_model.ipynb Changes the configuration to use the MobileFaceNet model architecture. This might be done to test a more lightweight or specialized model for inference. ```python conf.use_mobilfacenet = True ``` -------------------------------- ### Real-Time Face Verification with Webcam Source: https://context7.com/treb1en/insightface_pytorch/llms.txt Initiates real-time face verification using a webcam feed. It supports options for updating the face bank, using test-time augmentation (tta), saving video output, displaying confidence scores, and adjusting the recognition threshold. Requires a trained model and a prepared face bank. ```bash # Basic face verification (requires facebank and trained model) python face_verify.py # Update face bank before starting verification python face_verify.py --update # Use test-time augmentation for better accuracy python face_verify.py --tta # Save video output and show confidence scores python face_verify.py --save --score # Adjust recognition threshold (default 1.54, lower = stricter) python face_verify.py --threshold 1.2 # Complete example with all options python face_verify.py --update --tta --save --score --threshold 1.5 # Expected workflow: # 1. System loads MTCNN face detector # 2. Loads trained face recognition model # 3. Loads or updates face bank embeddings # 4. Opens webcam and detects faces in real-time # 5. Matches detected faces against face bank # 6. Displays bounding boxes with identity labels # 7. Press 'q' to quit ``` -------------------------------- ### Align face using similarity transformation Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/get_aligned_face_from_mtcnn.ipynb Performs face alignment using similarity transformation from skimage. Takes detected landmarks and reference points as input. Outputs transformation matrix for alignment. ```python dst = landmarks[0].astype(np.float32) facial5points = [[dst[j],dst[j+5]] for j in range(5)] from skimage import transform as trans tform = trans.SimilarityTransform() tform.estimate(np.array(facial5points), src) M = tform.params[0:2,:] ``` -------------------------------- ### Read and Unpack Image Record in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/refine_faces.ipynb This snippet reads a specific record (identified by index `i`) from an MXNet IndexedRecordIO dataset, unpacks the header and image data, decodes the image from a byte stream into a PIL Image object, and prints the record header information. It demonstrates how to access and process individual image samples from the dataset. ```python i =813 img_info = imgrec.read_idx(i) header, img = mx.recordio.unpack(img_info) encoded_jpg_io = io.BytesIO(img) image = Image.open(encoded_jpg_io) print(header) image ``` -------------------------------- ### Apply NMS and Calibrate Boxes After P-Net in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/try_mtcnn_step_by_step.ipynb Performs non-maximum suppression on bounding boxes, calibrates them using offsets, and converts to squares. This refines P-Net outputs by removing overlaps and adjusting positions. Inputs are bounding_boxes and NMS threshold; outputs filtered and calibrated boxes; it uses NumPy for efficiency. ```python keep = nms(bounding_boxes[:, 0:5], nms_thresholds[0]) bounding_boxes = bounding_boxes[keep] # use offsets predicted by pnet to transform bounding boxes bounding_boxes = calibrate_box(bounding_boxes[:, 0:5], bounding_boxes[:, 5:]) # shape [n_boxes, 5] bounding_boxes = convert_to_square(bounding_boxes) bounding_boxes[:, 0:4] = np.round(bounding_boxes[:, 0:4]) print('number of bounding boxes:', len(bounding_boxes)) ``` -------------------------------- ### Load Model State Source: https://github.com/treb1en/insightface_pytorch/blob/master/evaluate_model.ipynb Loads the pre-trained model weights into the initialized face learner. This step is crucial for using a model that has already been trained on a large dataset. ```python learner.load_state(conf, 'ir_se50.pth', model_only=True, from_save_folder=True) ``` -------------------------------- ### Build Backbone Networks in PyTorch Source: https://context7.com/treb1en/insightface_pytorch/llms.txt This code creates backbone networks like IR-SE and MobileFaceNet for face feature extraction in InsightFace PyTorch. It requires PyTorch and the model module, taking input tensors and producing normalized embeddings. Outputs are embedding tensors of shape [batch_size, 512]. ```python from model import Backbone, MobileFaceNet, l2_norm import torch # Create IR-SE50 backbone (default configuration) backbone_ir_se = Backbone(num_layers=50, drop_ratio=0.6, mode='ir_se') backbone_ir_se.eval() # Create standard IR backbone backbone_ir = Backbone(num_layers=50, drop_ratio=0.4, mode='ir') # Create MobileFaceNet for mobile deployment mobile_net = MobileFaceNet(embedding_size=512) mobile_net.eval() # Forward pass example with IR-SE50 input_tensor = torch.randn(4, 3, 112, 112) # batch_size=4, RGB, 112x112 with torch.no_grad(): embeddings = backbone_ir_se(input_tensor) print(f"Output embeddings shape: {embeddings.shape}") # [4, 512] print(f"Embeddings are L2 normalized: {torch.allclose(torch.norm(embeddings, dim=1), torch.ones(4))}") # MobileFaceNet forward pass with torch.no_grad(): mobile_embeddings = mobile_net(input_tensor) print(f"MobileFaceNet embeddings shape: {mobile_embeddings.shape}") # [4, 512] ``` -------------------------------- ### Face Recognition Evaluation on Datasets - Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/evaluate_model.ipynb These code blocks evaluate the face recognition model on seven benchmark datasets: VGG2-FP, AgeDB-30, CALFW, CFP-FF, CFP-FP, CPLFW, and LFW. Each block retrieves validation pairs from the EMORE folder, evaluates with 10-fold cross-validation and TTA, prints accuracy and threshold, and displays the ROC curve as a PIL image. Dependencies include pretrained learner, conf, get_val_pair function, and trans module. Limitations: Assumes pre-available datasets and model; results vary by dataset. ```python vgg2_fp, vgg2_fp_issame = get_val_pair(conf.emore_folder, 'vgg2_fp') accuracy, best_threshold, roc_curve_tensor = learner.evaluate(conf, vgg2_fp, vgg2_fp_issame, nrof_folds=10, tta=True) print('vgg2_fp - accuray:{}, threshold:{}'.format(accuracy, best_threshold)) trans.ToPILImage()(roc_curve_tensor) ``` ```python agedb_30, agedb_30_issame = get_val_pair(conf.emore_folder, 'agedb_30') accuracy, best_threshold, roc_curve_tensor = learner.evaluate(conf, agedb_30, agedb_30_issame, nrof_folds=10, tta=True) print('agedb_30 - accuray:{}, threshold:{}'.format(accuracy, best_threshold)) trans.ToPILImage()(roc_curve_tensor) ``` ```python calfw, calfw_issame = get_val_pair(conf.emore_folder, 'calfw') accuracy, best_threshold, roc_curve_tensor = learner.evaluate(conf, calfw, calfw_issame, nrof_folds=10, tta=True) print('calfw - accuray:{}, threshold:{}'.format(accuracy, best_threshold)) trans.ToPILImage()(roc_curve_tensor) ``` ```python cfp_ff, cfp_ff_issame = get_val_pair(conf.emore_folder, 'cfp_ff') accuracy, best_threshold, roc_curve_tensor = learner.evaluate(conf, cfp_ff, cfp_ff_issame, nrof_folds=10, tta=True) print('cfp_ff - accuray:{}, threshold:{}'.format(accuracy, best_threshold)) trans.ToPILImage()(roc_curve_tensor) ``` ```python cfp_fp, cfp_fp_issame = get_val_pair(conf.emore_folder, 'cfp_fp') accuracy, best_threshold, roc_curve_tensor = learner.evaluate(conf, cfp_fp, cfp_fp_issame, nrof_folds=10, tta=True) print('cfp_fp - accuray:{}, threshold:{}'.format(accuracy, best_threshold)) trans.ToPILImage()(roc_curve_tensor) ``` ```python cplfw, cplfw_issame = get_val_pair(conf.emore_folder, 'cplfw') accuracy, best_threshold, roc_curve_tensor = learner.evaluate(conf, cplfw, cplfw_issame, nrof_folds=10, tta=True) print('cplfw - accuray:{}, threshold:{}'.format(accuracy, best_threshold)) trans.ToPILImage()(roc_curve_tensor) ``` ```python lfw, lfw_issame = get_val_pair(conf.emore_folder, 'lfw') accuracy, best_threshold, roc_curve_tensor = learner.evaluate(conf, lfw, lfw_issame, nrof_folds=10, tta=True) print('lfw - accuray:{}, threshold:{}'.format(accuracy, best_threshold)) trans.ToPILImage()(roc_curve_tensor) ``` -------------------------------- ### Detect Faces and Landmarks in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/refine_faces.ipynb This code calls the `detect_faces` function, likely from a custom module, to identify faces within an input image and extract their corresponding facial landmarks. It returns bounding box coordinates and landmark points. ```python bounding_boxes, landmarks = detect_faces(image) ``` -------------------------------- ### Inference API for Face Matching (Python) Source: https://context7.com/treb1en/insightface_pytorch/llms.txt Provides an inference API for matching detected faces against a pre-loaded face bank. It utilizes MTCNN for face detection and alignment, and a trained face recognition model for identity inference. Supports both single image and batch processing. ```python from config import get_config from Learner import face_learner from mtcnn import MTCNN from utils import load_facebank from PIL import Image import torch # Setup conf = get_config(training=False) conf.threshold = 1.5 # Initialize components mtcnn = MTCNN() learner = face_learner(conf, inference=True) learner.load_state(conf, 'final.pth', from_save_folder=True, model_only=True) learner.model.eval() # Load face bank target_embeddings, names = load_facebank(conf) print(f"Face bank loaded: {names}") # Process an image image = Image.open('test_photo.jpg') bboxes, faces = mtcnn.align_multi(image, limit=10, min_face_size=30) if len(faces) > 0: # Infer identities indices, scores = learner.infer(conf, faces, target_embeddings, tta=True) for i, (idx, score) in enumerate(zip(indices, scores)): if idx == -1: identity = "Unknown" else: identity = names[idx + 1] # +1 because names[0] is 'Unknown' bbox = bboxes[i, :-1].astype(int) print(f"Face {i+1}: {identity}, confidence: {score:.4f}, bbox: {bbox}") else: print("No faces detected") # Batch inference example test_images = ['photo1.jpg', 'photo2.jpg', 'photo3.jpg'] for img_path in test_images: img = Image.open(img_path) try: _, faces = mtcnn.align_multi(img, limit=5) results, scores = learner.infer(conf, faces, target_embeddings, tta=False) print(f"{img_path}: Detected {len(results)} faces") except Exception as e: print(f"{img_path}: Error - {e}") ``` -------------------------------- ### Model Evaluation on Benchmark Datasets (Python) Source: https://context7.com/treb1en/insightface_pytorch/llms.txt Evaluates the trained face recognition model on standard benchmark datasets such as LFW, CFP-FP, and AgeDB-30. It loads validation data and computes accuracy and best thresholds for performance assessment. Test-time augmentation (tta) can be optionally used. ```python from config import get_config from Learner import face_learner from data.data_pipe import get_val_data import torch # Setup configuration conf = get_config(training=False) # Load model learner = face_learner(conf, inference=True) learner.load_state(conf, 'final.pth', from_save_folder=True, model_only=True) learner.model.eval() # Load validation datasets data_path = conf.data_path / 'faces_emore' agedb_30, cfp_fp, lfw, agedb_30_issame, cfp_fp_issame, lfw_issame = get_val_data(data_path) # Evaluate on LFW dataset print("Evaluating on LFW...") lfw_accuracy, lfw_threshold, lfw_roc = learner.evaluate( conf, lfw, lfw_issame, nrof_folds=10, tta=True ) print(f"LFW Accuracy: {lfw_accuracy:.4f}, Best Threshold: {lfw_threshold:.4f}") # Evaluate on CFP-FP dataset print("Evaluating on CFP-FP...") cfp_accuracy, cfp_threshold, cfp_roc = learner.evaluate( conf, cfp_fp, cfp_fp_issame, nrof_folds=10, tta=True ) print(f"CFP-FP Accuracy: {cfp_accuracy:.4f}, Best Threshold: {cfp_threshold:.4f}") # Evaluate on AgeDB-30 dataset print("Evaluating on AgeDB-30...") age_accuracy, age_threshold, age_roc = learner.evaluate( conf, agedb_30, agedb_30_issame, nrof_folds=10, tta=True ) print(f"AgeDB-30 Accuracy: {age_accuracy:.4f}, Best Threshold: {age_threshold:.4f}") # Expected results for IR-SE50: # LFW: ~0.9952, CFP-FP: ~0.9504, AgeDB-30: ~0.9622 ``` -------------------------------- ### Evaluate Model on LFW Dataset Source: https://github.com/treb1en/insightface_pytorch/blob/master/evaluate_model.ipynb Evaluates the face learner on the LFW (Labeled Faces in the Wild) dataset, a standard benchmark for face recognition. The evaluation calculates accuracy and the optimal threshold for verification. ```python lfw, lfw_issame = get_val_pair(conf.emore_folder, 'lfw') accuracy, best_threshold, roc_curve_tensor = learner.evaluate(conf, lfw, lfw_issame, nrof_folds=10, tta=True) print('lfw - accuray:{}, threshold:{}'.format(accuracy, best_threshold)) ``` -------------------------------- ### Apply affine transformation to align face Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/get_aligned_face_from_mtcnn.ipynb Applies affine transformation to warp image based on computed transformation matrix. Uses OpenCV for image warping. Outputs aligned face image with preserved dimensions. ```python warped = cv2.warpAffine(img_cv2,M,(img.size[0],img.size[1]), borderValue = 0.0) Image.fromarray(warped[...,::-1]) ``` -------------------------------- ### Calibrate and filter bounding boxes with NMS Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/try_mtcnn_step_by_step.ipynb Applies box calibration using offsets, then performs non‑maximum suppression to remove overlapping detections. The NMS mode is set to 'min'. After filtering, updates both bounding_boxes and landmarks and prints the remaining count. Depends on calibrate_box, nms, and nms_thresholds variables. ```python bounding_boxes = calibrate_box(bounding_boxes, offsets) keep = nms(bounding_boxes, nms_thresholds[2], mode='min') bounding_boxes = bounding_boxes[keep] landmarks = landmarks[keep] print('number of bounding boxes:', len(bounding_boxes)) ``` -------------------------------- ### Display Processed Face Image in Python Source: https://github.com/treb1en/insightface_pytorch/blob/master/mtcnn_pytorch/refine_faces.ipynb This code converts a processed image array (likely from `warp_and_crop_face`) back into a PIL Image object and displays it. The `dst_img[...,::-1]` suggests a color channel reordering (e.g., BGR to RGB) might be necessary before conversion. ```python Image.fromarray(dst_img[...,::-1]) ``` -------------------------------- ### Evaluate Model on CALFW Dataset Source: https://github.com/treb1en/insightface_pytorch/blob/master/evaluate_model.ipynb Assesses the face learner's accuracy on the CALFW (Cross-Age Large Face Wild) dataset. The evaluation includes calculating accuracy and the best threshold for face matching. ```python calfw, calfw_issame = get_val_pair(conf.emore_folder, 'calfw') accuracy, best_threshold, roc_curve_tensor = learner.evaluate(conf, calfw, calfw_issame, nrof_folds=10, tta=True) print('calfw - accuray:{}, threshold:{}'.format(accuracy, best_threshold)) ```