人脸检测——Faceboxes

文章介绍了如何在Python3.8环境下安装和使用FaceBoxes.PyTorch模型进行面部检测。首先通过gitclone获取代码,然后修改make.sh以适应Python3.8。接着定义了faceboxes_init函数来初始化模型,以及faceboxes_dectect函数用于执行检测。模型加载后,通过NMS处理检测结果,保留高概率的检测框。
部署运行你感兴趣的模型镜像

FaceBoxes.PyTorch

  • 安装
    在这里插入图片描述

    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)

您可能感兴趣的与本文相关的镜像

PyTorch 2.5

PyTorch 2.5

PyTorch
Cuda

PyTorch 是一个开源的 Python 机器学习库,基于 Torch 库,底层由 C++ 实现,应用于人工智能领域,如计算机视觉和自然语言处理

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值