-
Notifications
You must be signed in to change notification settings - Fork 38
/
age_classifier.py
155 lines (121 loc) · 4.71 KB
/
age_classifier.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
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 26 18:09:56 2020
@author: Quang
"""
"""
python age_classifier.py -v test_video.mp4 -s y
"""
import numpy as np
import argparse
import cv2
import os
from keras.models import model_from_json
from efficientnet import keras
import pickle
from bounding_box import bounding_box as bb
# setup argumentparse
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", required=True,
help = "Path to video")
ap.add_argument("-f","--face", default="face_detector",
help ="Path to face detector directory")
ap.add_argument("-m", "--model", default="model",
help = "Path to model directory")
ap.add_argument("-s","--save", default="n",
help = "Save processed video (y/n)")
ap.add_argument("-c","--conf",default=0.3,
help="Threshold fitering probability")
args = vars(ap.parse_args())
def resize(frame, new_width):
(h,w) = frame.shape[:2]
ratio = w/h
new_frame = cv2.resize(frame, (new_width, int(new_width/ratio)))
return new_frame
# retrun key from predicted value
def get_key(val, labels):
for key, value in labels.items():
if value==val:
if val==7:
return "60_up"
else:
return key
break
def get_age_from_model(face, model, labels):
face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
face = cv2.resize(face,(88,88))
face = face/255
face = face[np.newaxis,:]
val = np.argmax(model.predict(face))
return get_key(val,labels)
def predict(frame,faceNet, model, labels):
(h, w) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(frame, 1.0, (300,300), (104.0, 177.0, 123.0))
faceNet.setInput(blob)
detections = faceNet.forward()
for i in range(0, detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence > args["conf"]:
box = detections[0, 0 , i, 3:7] * np.array([w, h , w, h])
(startX, startY, endX, endY) = box.astype("int")
face = frame[startY:endY, startX:endX]
age = get_age_from_model(face, model, labels)
# display the predicted age to our terminal
text = "{:s}".format(age)
# draw the bounding box of the face along with the associated
# predicted age
#y = startY - 10 if startY - 10 > 10 else startY + 10
bb.add(frame,startX,startY,endX,endY,text)
#cv2.rectangle(frame, (startX, startY), (endX, endY),(0, 0, 255), 2)
# cv2.puttext(frame, text, (startX, y),cv2.font_hershey_simplex,
# 0.45, (0, 0, 255), 2)
return frame
def main():
global record_status
record_status = False
WIDTH = 1024
# load labels from trained model (age group)
with open(os.path.join(args["model"],"labels.pkl"),"rb") as files:
labels = pickle.load(files)
# load face detector model
configPath = os.path.join(args["face"],"deploy.prototxt")
weightsPath = os.path.join(args["face"],"res10_300x300_ssd_iter_140000.caffemodel")
faceNet = cv2.dnn.readNet(configPath, weightsPath)
print("[INFO] Load face detector model successfuly... ")
# load age predictor model
with open(os.path.join(args["model"],"age_prediction_efficientnet.json"),"r") as json_file:
json_model = json_file.read()
model = model_from_json(json_model)
model.load_weights(os.path.join(args["model"],"age_prediction_efficientnet.h5"))
print("[INFO] Load age prediciting model successfuly... ")
cap = cv2.VideoCapture(args["video"])
# save video according argument parser
if args["save"] in ["y", "Y"]:
record_status = True
_, frame = cap.read()
frame = resize(frame,WIDTH)
(h,w) = frame.shape[:2]
video_size = (w,h)
writer = cv2.VideoWriter("processed_video.mp4",cv2.VideoWriter_fourcc(*'DIVX'),18,video_size)
while True:
try:
_, frame = cap.read()
if frame is None:
break
# passing each frame through faceNet and age predicting model
# then return processes frame
frame = resize(frame,WIDTH)
result = predict(frame, faceNet, model, labels)
cv2.imshow("result", result)
if record_status: writer.write(result)
key = cv2.waitKey(1) & 0xff
# press ESC to exit
if key == 27:
break
except:
pass
if record_status: writer.release()
cap.release()
cv2.destroyAllWindows()
if __name__=="__main__":
main()