-
Notifications
You must be signed in to change notification settings - Fork 86
/
demo.py
173 lines (136 loc) · 6.15 KB
/
demo.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import argparse
import better_exceptions
from pathlib import Path
from contextlib import contextmanager
import urllib.request
import numpy as np
import cv2
import dlib
import torch
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim
import torch.utils.data
import torch.nn.functional as F
from model import get_model
from defaults import _C as cfg
def get_args():
parser = argparse.ArgumentParser(description="Age estimation demo",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--resume", type=str, default=None,
help="Model weight to be tested")
parser.add_argument("--margin", type=float, default=0.4,
help="Margin around detected face for age-gender estimation")
parser.add_argument("--img_dir", type=str, default=None,
help="Target image directory; if set, images in image_dir are used instead of webcam")
parser.add_argument("--output_dir", type=str, default=None,
help="Output directory to which resulting images will be stored if set")
parser.add_argument("opts", default=[], nargs=argparse.REMAINDER,
help="Modify config options using the command-line")
args = parser.parse_args()
return args
def draw_label(image, point, label, font=cv2.FONT_HERSHEY_SIMPLEX,
font_scale=0.8, thickness=1):
size = cv2.getTextSize(label, font, font_scale, thickness)[0]
x, y = point
cv2.rectangle(image, (x, y - size[1]), (x + size[0], y), (255, 0, 0), cv2.FILLED)
cv2.putText(image, label, point, font, font_scale, (255, 255, 255), thickness, lineType=cv2.LINE_AA)
@contextmanager
def video_capture(*args, **kwargs):
cap = cv2.VideoCapture(*args, **kwargs)
try:
yield cap
finally:
cap.release()
def yield_images():
with video_capture(0) as cap:
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
while True:
ret, img = cap.read()
if not ret:
raise RuntimeError("Failed to capture image")
yield img, None
def yield_images_from_dir(img_dir):
img_dir = Path(img_dir)
for img_path in img_dir.glob("*.*"):
img = cv2.imread(str(img_path), 1)
if img is not None:
h, w, _ = img.shape
r = 640 / max(w, h)
yield cv2.resize(img, (int(w * r), int(h * r))), img_path.name
def main():
args = get_args()
if args.opts:
cfg.merge_from_list(args.opts)
cfg.freeze()
if args.output_dir is not None:
if args.img_dir is None:
raise ValueError("=> --img_dir argument is required if --output_dir is used")
output_dir = Path(args.output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
# create model
print("=> creating model '{}'".format(cfg.MODEL.ARCH))
model = get_model(model_name=cfg.MODEL.ARCH, pretrained=None)
device = "cuda" if torch.cuda.is_available() else "cpu"
model = model.to(device)
# load checkpoint
resume_path = args.resume
if resume_path is None:
resume_path = Path(__file__).resolve().parent.joinpath("misc", "epoch044_0.02343_3.9984.pth")
if not resume_path.is_file():
print(f"=> model path is not set; start downloading trained model to {resume_path}")
url = "https://github.com/yu4u/age-estimation-pytorch/releases/download/v1.0/epoch044_0.02343_3.9984.pth"
urllib.request.urlretrieve(url, str(resume_path))
print("=> download finished")
if Path(resume_path).is_file():
print("=> loading checkpoint '{}'".format(resume_path))
checkpoint = torch.load(resume_path, map_location="cpu")
model.load_state_dict(checkpoint['state_dict'])
print("=> loaded checkpoint '{}'".format(resume_path))
else:
raise ValueError("=> no checkpoint found at '{}'".format(resume_path))
if device == "cuda":
cudnn.benchmark = True
model.eval()
margin = args.margin
img_dir = args.img_dir
detector = dlib.get_frontal_face_detector()
img_size = cfg.MODEL.IMG_SIZE
image_generator = yield_images_from_dir(img_dir) if img_dir else yield_images()
with torch.no_grad():
for img, name in image_generator:
input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_h, img_w, _ = np.shape(input_img)
# detect faces using dlib detector
detected = detector(input_img, 1)
faces = np.empty((len(detected), img_size, img_size, 3))
if len(detected) > 0:
for i, d in enumerate(detected):
x1, y1, x2, y2, w, h = d.left(), d.top(), d.right() + 1, d.bottom() + 1, d.width(), d.height()
xw1 = max(int(x1 - margin * w), 0)
yw1 = max(int(y1 - margin * h), 0)
xw2 = min(int(x2 + margin * w), img_w - 1)
yw2 = min(int(y2 + margin * h), img_h - 1)
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 255, 255), 2)
cv2.rectangle(img, (xw1, yw1), (xw2, yw2), (255, 0, 0), 2)
faces[i] = cv2.resize(img[yw1:yw2 + 1, xw1:xw2 + 1], (img_size, img_size))
# predict ages
inputs = torch.from_numpy(np.transpose(faces.astype(np.float32), (0, 3, 1, 2))).to(device)
outputs = F.softmax(model(inputs), dim=-1).cpu().numpy()
ages = np.arange(0, 101)
predicted_ages = (outputs * ages).sum(axis=-1)
# draw results
for i, d in enumerate(detected):
label = "{}".format(int(predicted_ages[i]))
draw_label(img, (d.left(), d.top()), label)
if args.output_dir is not None:
output_path = output_dir.joinpath(name)
cv2.imwrite(str(output_path), img)
else:
cv2.imshow("result", img)
key = cv2.waitKey(-1) if img_dir else cv2.waitKey(30)
if key == 27: # ESC
break
if __name__ == '__main__':
main()