Skip to content

Commit c9aa617

Browse files
committed
.
1 parent abeebbe commit c9aa617

File tree

5 files changed

+51
-368
lines changed

5 files changed

+51
-368
lines changed

demo_detect.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,21 @@ def plot_figures(figures, nrows=1, ncols=1):
4040

4141
# S3FD returns bboxes.
4242
t = time.time()
43-
bboxes = DET3.detect_faces(img, conf_th=0.8, scales=[0.5])
43+
bboxes = DET3.detect_faces(img, conf_th=0.9, scales=[0.5])
4444
print('S3FD : %d faces in %.4f seconds.' % (len(bboxes), time.time() - t))
4545
img3 = draw_bboxes(img, bboxes)
4646

4747
# DSFD returns bboxes.
4848
t = time.time()
49-
bboxes = DET4.detect_faces(img, conf_th=0.8, scales=[0.5])
49+
bboxes = DET4.detect_faces(img, conf_th=0.9, scales=[0.5])
5050
print('DSFD : %d faces in %.4f seconds.' % (len(bboxes), time.time() - t))
5151
img4 = draw_bboxes(img, bboxes)
5252

5353
# plot results.
5454
results = {
55-
'MTCNN (conf_th=0.9, scales=[1])': img1,
55+
'MTCNN (conf_th=0.9, scales=[0.125])': img1,
5656
'Tiny Face (conf_th=0.9, scales=[1])': img2,
57-
'S3FD (conf_th=0.9, scales=[1])': img3,
58-
'DSFD (conf_th=0.9, scales=[1])': img4
57+
'S3FD (conf_th=0.9, scales=[0.5])': img3,
58+
'DSFD (conf_th=0.9, scales=[0.5])': img4
5959
}
6060
plot_figures(results, 2, 2)

detectors/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from .mtcnn import MTCNN
22
from .tinyface import TinyFace
33
from .s3fd import S3FD
4-
from .dsfd import DSFD, DSFD_
4+
from .dsfd import DSFD

detectors/dsfd/__init__.py

-50
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
import torch
55
from torchvision import transforms
66
from .nets import DSFDNet
7-
from .nets2 import DSFDNet2
87
from .box_utils import nms_
98

109
PATH_WEIGHT = './detectors/dsfd/weights/dsfd_vgg_0.880.pth'
11-
PATH_WEIGHT2 = './detectors/dsfd/weights/WIDERFace_DSFD_RES152.pth'
1210
img_mean = np.array([104., 117., 123.])[:, np.newaxis, np.newaxis].astype('float32')
1311

1412

@@ -61,51 +59,3 @@ def detect_faces(self, image, conf_th=0.8, scales=[1]):
6159
bboxes = bboxes[keep]
6260

6361
return bboxes
64-
65-
66-
class DSFD_():
67-
68-
def __init__(self, device='cuda'):
69-
70-
tstamp = time.time()
71-
self.device = device
72-
73-
print('[DSFD] loading with', self.device)
74-
self.net = DSFDNet2(device=self.device).to(self.device)
75-
state_dict = torch.load(PATH_WEIGHT2, map_location=self.device)
76-
self.net.load_state_dict(state_dict)
77-
self.net.eval()
78-
print('[DSFD] finished loading (%.4f sec)' % (time.time() - tstamp))
79-
80-
def detect_faces(self, image, conf_th=0.8, scales=[1]):
81-
82-
w, h = image.shape[1], image.shape[0]
83-
84-
bboxes = np.empty(shape=(0, 5))
85-
86-
with torch.no_grad():
87-
for s in scales:
88-
scaled_img = cv2.resize(image, dsize=(0, 0), fx=s, fy=s, interpolation=cv2.INTER_LINEAR)
89-
90-
scaled_img = np.swapaxes(scaled_img, 1, 2)
91-
scaled_img = np.swapaxes(scaled_img, 1, 0)
92-
scaled_img = scaled_img[[2, 1, 0], :, :]
93-
scaled_img = scaled_img.astype('float32')
94-
scaled_img -= img_mean
95-
scaled_img = scaled_img[[2, 1, 0], :, :]
96-
x = torch.from_numpy(scaled_img).unsqueeze(0).to(self.device)
97-
y = self.net(x)
98-
99-
detections = y.data
100-
scale = torch.Tensor([w, h, w, h])
101-
102-
for i in range(detections.size(1)):
103-
j = 0
104-
while detections[0, i, j, 0] > conf_th:
105-
score = detections[0, i, j, 0]
106-
pt = (detections[0, i, j, 1:] * scale).cpu().numpy()
107-
bbox = (pt[0], pt[1], pt[2], pt[3], score)
108-
bboxes = np.vstack((bboxes, bbox))
109-
j += 1
110-
111-
return bboxes

0 commit comments

Comments
 (0)