-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrun.py
More file actions
169 lines (132 loc) · 5.03 KB
/
run.py
File metadata and controls
169 lines (132 loc) · 5.03 KB
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
# -*- coding: utf-8 -*-
"""
Created on Fr March 23 01:21:40 2018
@author: lav solanki
"""
import os
import cv2
import dlib
import numpy as np
import argparse
from wide_resnet import WideResNet
def get_args():
parser = argparse.ArgumentParser(description="This script detects faces from web cam input, "
"and estimates age and gender for the detected faces.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--weight_file", type=str, default=None,
help="path to weight file (e.g. weights.18-4.06.hdf5)")
parser.add_argument("--depth", type=int, default=16,
help="depth of network")
parser.add_argument("--width", type=int, default=8,
help="width of network")
args = parser.parse_args()
return args
def draw_label(image, point, label, font=cv2.FONT_HERSHEY_SIMPLEX,
font_scale=1, thickness=2):
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)
def main():
args = get_args()
depth = args.depth
k = args.width
weight_file = args.weight_file
if not weight_file:
weight_file = os.path.join("data", "weights.18-4.06.hdf5")
detector = dlib.get_frontal_face_detector()
img_size = 64
model = WideResNet(img_size, depth=depth, k=k)()
model.load_weights(weight_file)
cap = cv2.VideoCapture(0)
while True:
ret, img = cap.read()
if not ret:
print("error: failed to capture image")
return -1
input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_h, img_w, _ = np.shape(input_img)
detected = detector(input_img, 1)
print(detected)
faces = np.empty((len(detected), img_size, img_size, 3))
print(faces)
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 - 0.4 * w), 0)
yw1 = max(int(y1 - 0.4 * h), 0)
xw2 = min(int(x2 + 0.4 * w), img_w - 1)
yw2 = min(int(y2 + 0.4 * h), img_h - 1)
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
faces[i,:,:,:] = cv2.resize(img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size))
if len(detected) > 0:
res = model.predict(faces)
predicted_genders = res[0]
ages = np.arange(0, 101).reshape(101, 1)
predicted_ages = res[1].dot(ages).flatten()
for i, d in enumerate(detected):
label = "Female" if predicted_genders[i][0] > 0.5 else "Male"
gender=label
age=predicted_ages +1
# draw_label(img, (d.left(), d.top()), label)
# cv2.putText(img, "Estimated Age (Years): " + str(" %6.1f " % age), (10, 210), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (251, 251, 50), 2)
# cv2.putText(img, "Gender: " + gender, (10, 230), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 0), 2)
# insert information text to video frame
font = cv2.FONT_HERSHEY_SIMPLEX
input_frame = img
cv2.rectangle(input_frame, (10, 275), (390, 350), (180, 132, 109), -1)
cv2.putText(
input_frame,
'AGE & GENDER PREDICTION - JAIR RIBEIRO',
(11, 290),
font,
0.5,
(0xFF, 0xFF, 0xFF),
1,
cv2.FONT_HERSHEY_SIMPLEX,
)
cv2.putText(
input_frame,
'Gender -----------:' + gender,
(14, 302),
font,
0.4,
(0xFF, 0xFF, 0xFF),
1,
cv2.FONT_HERSHEY_COMPLEX_SMALL,
)
cv2.putText(
input_frame,
'Estimated Age : '+ str(" %6.1f " % age),
(14, 320),
font,
0.4,
(0xFF, 0xFF, 0xFF),
1,
cv2.FONT_HERSHEY_COMPLEX_SMALL,
)
## cv2.putText(
## input_frame,
## '-Color: ',
## (14, 322),
## font,
## 0.4,
## (0xFF, 0xFF, 0xFF),
## 1,
## cv2.FONT_HERSHEY_COMPLEX_SMALL,
## )
## cv2.putText(
## input_frame,
## '-Vehicle Size/Type: ',
## (14, 332),
## font,
## 0.4,
## (0xFF, 0xFF, 0xFF),
## 1,
## cv2.FONT_HERSHEY_COMPLEX_SMALL,
## )
cv2.imshow("Age and Gender Estimation - Jair Ribeiro", img)
key = cv2.waitKey(25)
if key == 27:
break
if __name__ == '__main__':
main()