-
安装
git clone https://github.com/zisianw/FaceBoxes.PyTorch.git sh ./make.sh
我的系统中使用的Python3.8,直接
sh ./make.sh
使用的是python3.6,所以修改make.sh
为:
-
模型文件
需要用到的文件,nms中的编译文件为utils->nms
中的编译后文件。
-
使用
#faceboxes detector initialize
def faceboxes_init(model_dir):
net = FaceBoxes(phase='test', size=None, num_classes=2)
net.load_state_dict(torch.load(model_dir))
cuda = torch.cuda.is_available()
if cuda:
net.cuda()
net.eval()
return net, cuda
def faceboxes_dectect(model, cuda, image):
img = np.float32(image)
h, w = img.shape[:2]
scale = torch.Tensor([img.shape[1], img.shape[0], img.shape[1], img.shape[0]])
img -= (104, 117, 123)
img = img.transpose(2, 0, 1)
img = torch.from_numpy(img).unsqueeze(0)
if cuda:
img = img.cuda()
scale = scale.cuda()
loc, conf = model(img)
priorbox = PriorBox(cfg, image_size=(h, w))
priors = priorbox.forward()
if cuda:
priors = priors.cuda()
prior_data = priors.data
boxes = decode(loc.data.squeeze(0), prior_data, cfg['variance'])
boxes = boxes * scale
boxes = boxes.cpu().numpy()
scores = conf.squeeze(0).data.cpu().numpy()[:, 1]
#ignore low scores
inds = np.where(scores > 0.7)[0]
boxes = boxes[inds]
scores = scores[inds]
#keep top-K before NMS
order = scores.argsort()[::-1][:5000]
boxes = boxes[order]
scores = scores[order]
#do NMS
dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False)
keep = nms(dets, 0.3, force_cpu=False)
dets = dets[keep, :]
#keep top-K faster NMS
dets = dets[: 750, :]
return dets
faceboxes_model, faceboxes_cuda = faceboxes_init(args.faceboxes_detect_model_dir)
result_bboxes = faceboxes_dectect(faceboxes_model, faceboxes_cuda, image)