Path: blob/master/Face-Recognition-with-ArcFace/align/detector.py
3142 views
import numpy as np1import torch2from torch.autograd import Variable3from align.get_nets import PNet, RNet, ONet4from align.box_utils import nms, calibrate_box, get_image_boxes, convert_to_square5from align.first_stage import run_first_stage678def detect_faces(image, min_face_size = 20.0,9thresholds=[0.6, 0.7, 0.8],10nms_thresholds=[0.7, 0.7, 0.7]):11"""12Arguments:13image: an instance of PIL.Image.14min_face_size: a float number.15thresholds: a list of length 3.16nms_thresholds: a list of length 3.1718Returns:19two float numpy arrays of shapes [n_boxes, 4] and [n_boxes, 10],20bounding boxes and facial landmarks.21"""2223# LOAD MODELS24pnet = PNet()25rnet = RNet()26onet = ONet()27onet.eval()2829# BUILD AN IMAGE PYRAMID30width, height = image.size31min_length = min(height, width)3233min_detection_size = 1234factor = 0.707 # sqrt(0.5)3536# scales for scaling the image37scales = []3839# scales the image so that40# minimum size that we can detect equals to41# minimum face size that we want to detect42m = min_detection_size/min_face_size43min_length *= m4445factor_count = 046while min_length > min_detection_size:47scales.append(m*factor**factor_count)48min_length *= factor49factor_count += 15051# STAGE 15253# it will be returned54bounding_boxes = []5556# run P-Net on different scales57for s in scales:58boxes = run_first_stage(image, pnet, scale = s, threshold = thresholds[0])59bounding_boxes.append(boxes)6061# collect boxes (and offsets, and scores) from different scales62bounding_boxes = [i for i in bounding_boxes if i is not None]63bounding_boxes = np.vstack(bounding_boxes)6465keep = nms(bounding_boxes[:, 0:5], nms_thresholds[0])66bounding_boxes = bounding_boxes[keep]6768# use offsets predicted by pnet to transform bounding boxes69bounding_boxes = calibrate_box(bounding_boxes[:, 0:5], bounding_boxes[:, 5:])70# shape [n_boxes, 5]7172bounding_boxes = convert_to_square(bounding_boxes)73bounding_boxes[:, 0:4] = np.round(bounding_boxes[:, 0:4])7475# STAGE 27677img_boxes = get_image_boxes(bounding_boxes, image, size = 24)78img_boxes = Variable(torch.FloatTensor(img_boxes), volatile = True)79output = rnet(img_boxes)80offsets = output[0].data.numpy() # shape [n_boxes, 4]81probs = output[1].data.numpy() # shape [n_boxes, 2]8283keep = np.where(probs[:, 1] > thresholds[1])[0]84bounding_boxes = bounding_boxes[keep]85bounding_boxes[:, 4] = probs[keep, 1].reshape((-1, ))86offsets = offsets[keep]8788keep = nms(bounding_boxes, nms_thresholds[1])89bounding_boxes = bounding_boxes[keep]90bounding_boxes = calibrate_box(bounding_boxes, offsets[keep])91bounding_boxes = convert_to_square(bounding_boxes)92bounding_boxes[:, 0:4] = np.round(bounding_boxes[:, 0:4])9394# STAGE 39596img_boxes = get_image_boxes(bounding_boxes, image, size = 48)97if len(img_boxes) == 0:98return [], []99img_boxes = Variable(torch.FloatTensor(img_boxes), volatile = True)100output = onet(img_boxes)101landmarks = output[0].data.numpy() # shape [n_boxes, 10]102offsets = output[1].data.numpy() # shape [n_boxes, 4]103probs = output[2].data.numpy() # shape [n_boxes, 2]104105keep = np.where(probs[:, 1] > thresholds[2])[0]106bounding_boxes = bounding_boxes[keep]107bounding_boxes[:, 4] = probs[keep, 1].reshape((-1, ))108offsets = offsets[keep]109landmarks = landmarks[keep]110111# compute landmark points112width = bounding_boxes[:, 2] - bounding_boxes[:, 0] + 1.0113height = bounding_boxes[:, 3] - bounding_boxes[:, 1] + 1.0114xmin, ymin = bounding_boxes[:, 0], bounding_boxes[:, 1]115landmarks[:, 0:5] = np.expand_dims(xmin, 1) + np.expand_dims(width, 1)*landmarks[:, 0:5]116landmarks[:, 5:10] = np.expand_dims(ymin, 1) + np.expand_dims(height, 1)*landmarks[:, 5:10]117118bounding_boxes = calibrate_box(bounding_boxes, offsets)119keep = nms(bounding_boxes, nms_thresholds[2], mode = 'min')120bounding_boxes = bounding_boxes[keep]121landmarks = landmarks[keep]122123return bounding_boxes, landmarks124125126