-
Notifications
You must be signed in to change notification settings - Fork 303
/
main.py
135 lines (93 loc) · 3.29 KB
/
main.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
import common
import numpy as np
import torch
import torch.nn.functional as F
import torch.nn as nn
import cv2
from model.DBFace import DBFace
HAS_CUDA = torch.cuda.is_available()
print(f"HAS_CUDA = {HAS_CUDA}")
def nms(objs, iou=0.5):
if objs is None or len(objs) <= 1:
return objs
objs = sorted(objs, key=lambda obj: obj.score, reverse=True)
keep = []
flags = [0] * len(objs)
for index, obj in enumerate(objs):
if flags[index] != 0:
continue
keep.append(obj)
for j in range(index + 1, len(objs)):
if flags[j] == 0 and obj.iou(objs[j]) > iou:
flags[j] = 1
return keep
def detect(model, image, threshold=0.4, nms_iou=0.5):
mean = [0.408, 0.447, 0.47]
std = [0.289, 0.274, 0.278]
image = common.pad(image)
image = ((image / 255.0 - mean) / std).astype(np.float32)
image = image.transpose(2, 0, 1)
torch_image = torch.from_numpy(image)[None]
if HAS_CUDA:
torch_image = torch_image.cuda()
hm, box, landmark = model(torch_image)
hm_pool = F.max_pool2d(hm, 3, 1, 1)
scores, indices = ((hm == hm_pool).float() * hm).view(1, -1).cpu().topk(1000)
hm_height, hm_width = hm.shape[2:]
scores = scores.squeeze()
indices = indices.squeeze()
ys = list((indices / hm_width).int().data.numpy())
xs = list((indices % hm_width).int().data.numpy())
scores = list(scores.data.numpy())
box = box.cpu().squeeze().data.numpy()
landmark = landmark.cpu().squeeze().data.numpy()
stride = 4
objs = []
for cx, cy, score in zip(xs, ys, scores):
if score < threshold:
break
x, y, r, b = box[:, cy, cx]
xyrb = (np.array([cx, cy, cx, cy]) + [-x, -y, r, b]) * stride
x5y5 = landmark[:, cy, cx]
x5y5 = (common.exp(x5y5 * 4) + ([cx]*5 + [cy]*5)) * stride
box_landmark = list(zip(x5y5[:5], x5y5[5:]))
objs.append(common.BBox(0, xyrb=xyrb, score=score, landmark=box_landmark))
return nms(objs, iou=nms_iou)
def detect_image(model, file):
image = common.imread(file)
objs = detect(model, image)
for obj in objs:
common.drawbbox(image, obj)
common.imwrite("detect_result/" + common.file_name_no_suffix(file) + ".draw.jpg", image)
def image_demo():
dbface = DBFace()
dbface.eval()
if HAS_CUDA:
dbface.cuda()
dbface.load("model/dbface.pth")
detect_image(dbface, "datas/selfie.jpg")
detect_image(dbface, "datas/12_Group_Group_12_Group_Group_12_728.jpg")
def camera_demo():
dbface = DBFace()
dbface.eval()
if HAS_CUDA:
dbface.cuda()
dbface.load("model/dbface.pth")
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
ok, frame = cap.read()
while ok:
objs = detect(dbface, frame)
for obj in objs:
common.drawbbox(frame, obj)
cv2.imshow("demo DBFace", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
ok, frame = cap.read()
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
image_demo()
camera_demo()