def main(args): checkpoint = torch.load(args.model_path) plfd_backbone = PFLDInference().cuda() plfd_backbone.load_state_dict(checkpoint['plfd_backbone']) plfd_backbone.eval() plfd_backbone = plfd_backbone.cuda() transform = transforms.Compose([transforms.ToTensor()]) cap = cv2.VideoCapture(0) while True: ret, img = cap.read() if not ret: break height, width = img.shape[:2] bounding_boxes, landmarks = detect_faces(img) for box in bounding_boxes: score = box[4] x1, y1, x2, y2 = (box[:4] + 0.5).astype(np.int32) w = x2 - x1 + 1 h = y2 - y1 + 1 size = int(max([w, h]) * 1.1) cx = x1 + w // 2 cy = y1 + h // 2 x1 = cx - size // 2 x2 = x1 + size y1 = cy - size // 2 y2 = y1 + size dx = max(0, -x1) dy = max(0, -y1) x1 = max(0, x1) y1 = max(0, y1) edx = max(0, x2 - width) edy = max(0, y2 - height) x2 = min(width, x2) y2 = min(height, y2) cropped = img[y1:y2, x1:x2] if (dx > 0 or dy > 0 or edx > 0 or edy > 0): cropped = cv2.copyMakeBorder(cropped, dy, edy, dx, edx, cv2.BORDER_CONSTANT, 0) cropped = cv2.resize(cropped, (112, 112)) input = cv2.resize(cropped, (112, 112)) input = cv2.cvtColor(input, cv2.COLOR_BGR2RGB) input = transform(input).unsqueeze(0).cuda() _, landmarks = plfd_backbone(input) pre_landmark = landmarks[0] pre_landmark = pre_landmark.cpu().detach().numpy().reshape( -1, 2) * [size, size] for (x, y) in pre_landmark.astype(np.int32): cv2.circle(img, (x1 + x, y1 + y), 1, (0, 0, 255)) cv2.imshow('0', img) if cv2.waitKey(10) == 27: break
def __init__(self, model_path): checkpoint = torch.load(model_path) plfd_backbone = PFLDInference().cuda() plfd_backbone.load_state_dict(checkpoint['plfd_backbone']) plfd_backbone.eval() self.plfd_backbone = plfd_backbone.cuda() self.transform = transforms.Compose([transforms.ToTensor()])
def main(args): checkpoint = torch.load(args.model_path) plfd_backbone = PFLDInference().cuda() plfd_backbone.load_state_dict(checkpoint['plfd_backbone']) plfd_backbone.eval() plfd_backbone = plfd_backbone.cuda() transform = transforms.Compose([transforms.ToTensor()]) cap = cv2.VideoCapture(0) while True: ret, img = cap.read() if not ret: break bounding_boxes, landmarks = detect_faces(img) maxval_index = 0 if (len(bounding_boxes) > 0): maxval_index = np.argmax(bounding_boxes[:, 4]) if (len(bounding_boxes) > maxval_index - 1 and len(bounding_boxes) > 0): face = img[int(bounding_boxes[maxval_index][1] ):int(bounding_boxes[maxval_index][3]), int(bounding_boxes[maxval_index][0] ):int(bounding_boxes[maxval_index][2])] if face.size == 0: continue face_resized = cv2.resize(face, dsize=(112, 112), interpolation=cv2.INTER_LINEAR) face_resized = cv2.cvtColor(face_resized, cv2.COLOR_BGR2RGB) face_resized = transform(face_resized).unsqueeze(0).cuda() _, landmarks = plfd_backbone(face_resized) pre_landmark = landmarks.cpu().detach().numpy()[0].reshape( -1, 2) * [ int(bounding_boxes[maxval_index][2]) - int(bounding_boxes[maxval_index][0]), int(bounding_boxes[maxval_index][3]) - int(bounding_boxes[maxval_index][1]) ] for (x, y) in pre_landmark.astype(np.int32): cv2.circle(face, (x, y), 1, (255, 0, 0), -1) cv2.imshow("xxxx", face) if cv2.waitKey(10) == 27: break
def main(args): checkpoint = torch.load(args.model_path) plfd_backbone = PFLDInference().cuda() plfd_backbone.load_state_dict(checkpoint['plfd_backbone']) plfd_backbone.eval() plfd_backbone = plfd_backbone.cuda() transform = transforms.Compose([transforms.ToTensor()]) img = cv2.imread(args.image_path) height, width = img.shape[:2] bounding_boxes, landmarks = detect_faces(img) for box in bounding_boxes: #score = box[4] x1, y1, x2, y2 = (box[:4] + 0.5).astype(np.int32) w = x2 - x1 + 1 h = y2 - y1 + 1 size = int(max([w, h]) * 1.1) cx = x1 + w // 2 cy = y1 + h // 2 x1 = cx - size // 2 x2 = x1 + size y1 = cy - size // 2 y2 = y1 + size dx = max(0, -x1) dy = max(0, -y1) x1 = max(0, x1) y1 = max(0, y1) edx = max(0, x2 - width) edy = max(0, y2 - height) x2 = min(width, x2) y2 = min(height, y2) cropped = img[y1:y2, x1:x2] if (dx > 0 or dy > 0 or edx > 0 or edy > 0): cropped = cv2.copyMakeBorder(cropped, dy, edy, dx, edx, cv2.BORDER_CONSTANT, 0) cv2.imwrite("cropped.jpg", cropped) print("cropped shape:", cropped.shape) #cropped shape: (668, 668, 3) cropped = cv2.resize(cropped, (112, 112)) input = cv2.resize(cropped, (112, 112)) input = cv2.cvtColor(input, cv2.COLOR_BGR2RGB) input = transform(input).unsqueeze(0).cuda() _, landmarks = plfd_backbone(input) pre_landmark = landmarks[0] print("size:", size) #size: 668 pre_landmark = pre_landmark.cpu().detach().numpy().reshape( -1, 2) * [size, size] for idx, (x, y) in enumerate(pre_landmark.astype(np.int32)): cv2.circle(img, (x1 + x, y1 + y), 1, (0, 0, 255)) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(img, str(idx + 1), (x1 + x, y1 + y), font, 0.4, (0, 0, 255), 1, cv2.LINE_AA) points = transfer(pre_landmark) for point in points: cv2.circle(img, (int(round(x1 + point[0])), int(round(y1 + point[1]))), 5, (255, 0, 0), 2) cv2.imwrite("result.jpg", img)
# trained_model = './weights/Resnet50_epoch_95.pth' retina_trained_model = './weights/mobilenet0.25_epoch_245.pth' mask_trained_model = './weights/net_21.pth' pfld_trained_model = './weights/checkpoint_epoch_500.pth.tar' # net and model retina_net = RetinaFace(cfg=cfg, phase='test') mask_net = resnet50(num_classes=2) pfld_backbone = PFLDInference() # load pre-trained model retina_net.load_state_dict(torch.load(retina_trained_model)) mask_net.load_state_dict(torch.load(mask_trained_model)) pfld_backbone.load_state_dict(torch.load(pfld_trained_model)['plfd_backbone']) retina_net = retina_net.cuda(0) mask_net = mask_net.cuda(0) pfld_net = pfld_backbone.cuda(0) retina_net.eval() mask_net.eval() pfld_net.eval() resize = 1 top_k = 5000 keep_top_k = 750 nms_threshold = 0.5 cudnn.benchmark = True transform1 = T.Compose([ T.Resize(size=(256, 256)), T.ToTensor(), T.Normalize([0.56687369, 0.44000871, 0.39886727],