forked from WIKI2020/FacePose_pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dectect.py
37 lines (31 loc) · 1.42 KB
/
dectect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# -*- coding: utf-8 -*-
import os
import cv2
import math
import torch
import numpy as np
import torch.nn.functional as F
class Detection:
def __init__(self):
caffemodel = "./checkpoint/Widerface-RetinaFace.caffemodel"
deploy = "./checkpoint/deploy.prototxt"
self.detector = cv2.dnn.readNetFromCaffe(deploy, caffemodel)
self.detector_confidence = 0.7
def get_bbox(self, img):
height, width = img.shape[0], img.shape[1]
aspect_ratio = width / height
if img.shape[1] * img.shape[0] >= 192 * 192:
img = cv2.resize(img,
(int(192 * math.sqrt(aspect_ratio)),
int(192 / math.sqrt(aspect_ratio))), interpolation=cv2.INTER_LINEAR)
blob = cv2.dnn.blobFromImage(img, 1, mean=(104, 117, 123))
self.detector.setInput(blob, 'data')
out = self.detector.forward('detection_out').squeeze()
max_conf_index = np.argmax(out[:, 2])
left, top, right, bottom = out[max_conf_index, 3]*width, out[max_conf_index, 4]*height,out[max_conf_index, 5]*width, out[max_conf_index, 6]*height
bbox = [int(left), int(top), int(right-left+1), int(bottom-top+1)]
return bbox
class AntiSpoofPredict(Detection):
def __init__(self, device_id):
super(AntiSpoofPredict, self).__init__()
self.device = torch.device("cuda:{}".format(device_id) if torch.cuda.is_available() else "cpu")