-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathyxl-s3fd.py
154 lines (120 loc) · 4.47 KB
/
yxl-s3fd.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#-*- coding:utf-8 -*-
from __future__ import division
from __future__ import absolute_import
from __future__ import print_function
import os
import torch
import argparse
import torch.nn as nn
import torch.utils.data as data
import torch.backends.cudnn as cudnn
import torchvision.transforms as transforms
import cv2
import time
import numpy as np
from PIL import Image
import _add_path
import sys
from S3FD.data.config import cfg
from S3FD.s3fd import build_s3fd
from S3FD.utils.augmentations import to_chw_bgr
parser = argparse.ArgumentParser(description='s3df demo')
parser.add_argument('--model', type=str,
default='S3FD/weights/s3fd.pth', help='trained model')
parser.add_argument('--thresh', default=0.6, type=float,
help='Final confidence threshold')
parser.add_argument('--with_draw', default='True')
args = parser.parse_args()
use_cuda = torch.cuda.is_available()
if use_cuda:
torch.set_default_tensor_type('torch.cuda.FloatTensor')
else:
torch.set_default_tensor_type('torch.FloatTensor')
def detect_image(net, img_orig, thresh):
t1 = cv2.getTickCount()
img = cv2.cvtColor(img_orig.copy(), cv2.COLOR_BGR2RGB)
height, width, _ = img.shape
max_im_shrink = np.sqrt(
1700 * 1200 / (img.shape[0] * img.shape[1]))
image = cv2.resize(img, None, None, fx=max_im_shrink,
fy=max_im_shrink, interpolation=cv2.INTER_LINEAR)
# image = cv2.resize(img, (640, 640))
image = cv2.resize(image, None, fx=1/8, fy=1/8)
# print (image.shape)
x = to_chw_bgr(image)
x = x.astype('float32')
x -= cfg.img_mean
x = x[[2, 1, 0], :, :]
x = torch.from_numpy(x).unsqueeze(0)
if use_cuda:
x = x.cuda()
with torch.no_grad():
y = net(x)
detections = y.data
time = (cv2.getTickCount() - t1) / cv2.getTickFrequency() * 1000
# print('time:{:.2f}ms'.format(time))
img = img_orig.copy()
scale = torch.Tensor([img.shape[1], img.shape[0],
img.shape[1], img.shape[0]])
list_bbox_tlbr = []
for i in range(detections.size(1)):
j = 0
while detections[0, i, j, 0] >= thresh:
pt = (detections[0, i, j, 1:] * scale).cpu().numpy()
score = detections[0, i, j, 0].cpu().numpy()
# left_up, right_bottom = (pt[0], pt[1]), (pt[2], pt[3])
list_bbox_tlbr.append([pt[1], pt[0], pt[3], pt[2], float(score)])
j += 1
return list_bbox_tlbr
if __name__ == '__main__':
net = build_s3fd('test', cfg.NUM_CLASSES)
if use_cuda:
net.load_state_dict(torch.load(args.model))
else:
net.load_state_dict(torch.load(args.model, map_location=torch.device('cpu')))
net.eval()
if use_cuda:
net.cuda()
cudnn.benckmark = True
# vc = cv2.VideoCapture(args.input_video)
# i = 0
# while True:
# i += 1
# img = vc.read()[1]
# if img is None:
# break
# if i%2 == 0:
# continue
# show = img.copy()
# list_bbox_tlbr = detect_image(net, img, args.thresh)
# for bbox in list_bbox_tlbr:
# t,l,b,r,conf = bbox
# cv2.rectangle(show, (l,t), (r,b), (0, 0, 255), 2)
# conf = "{:.2f}".format(conf)
# point = (int(l), int(t - 5))
# cv2.putText(show, conf, point, cv2.FONT_HERSHEY_SIMPLEX,
# 0.6, (0, 255, 0), 1, lineType=cv2.LINE_AA)
# cv2.imshow('show', show)
# key = cv2.waitKey(1)
# if key == 27:
# break
bgr_img = cv2.imread('./test.jpg', 1)
print (bgr_img.shape)
### detection
list_time = []
for idx in range(10):
start = cv2.getTickCount()
(h, w) = bgr_img.shape[:2]
list_bbox_tlbr = detect_image(net, bgr_img, args.thresh)
time = (cv2.getTickCount() - start) / cv2.getTickFrequency() * 1000
list_time.append(time)
# print ('elapsed time: %.3fms'%time)
print ('s3fd average time: %.3f ms'%np.array(list_time[1:]).mean())
### draw rectangle bbox
if args.with_draw == 'True':
for bb in list_bbox_tlbr:
(t, l, b, r, conf) = bb
cv2.rectangle(bgr_img, (l, t), (r, b), (0, 255, 0), 2)
cv2.namedWindow('show', 0)
cv2.imshow('show', bgr_img)
cv2.waitKey()