-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·55 lines (41 loc) · 1.73 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
from argparse import ArgumentParser, BooleanOptionalAction
import cv2 as cv
from face_detector import FaceDetector
def main():
parser: ArgumentParser = ArgumentParser()
parser.add_argument('--source', type=str, default='0', help='The source file or the camera id')
parser.add_argument('--model', type=str, default='weights/best_age_estimator_resnet.h5', help='The model file path')
parser.add_argument('--draw', type=bool, action=BooleanOptionalAction, default=True,
help='Draw bounding boxes on the image')
opt = parser.parse_args()
try:
opt.source = int(opt.source)
except ValueError:
pass
detector = FaceDetector(opt.model)
cap = cv.VideoCapture(opt.source)
while True:
success, frame = cap.read()
if success:
result_frame, bboxs = detector.detect_face(frame, draw=opt.draw)
if len(bboxs) > 0:
for bbox in bboxs:
x1, y1, x2, y2 = bbox
face = frame[y1: y2, x1: x2]
age = detector.estimate_age(face)
if not opt.draw:
org = ((x1 + x2) // 2, (y1 + y2) // 2)
else:
org = (x1, y2 + 20)
cv.putText(result_frame, f'Age: {age}', org,
cv.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 0), 2)
cv.imshow('webcam', result_frame)
if cv.waitKey(1) & 0xFF == 27:
break
else:
# raise Exception('Could not read frames from source file or camera.')
print('Could not read frames from source file or camera.')
break
cap.release()
if __name__ == '__main__':
main()