-
Notifications
You must be signed in to change notification settings - Fork 0
/
age_gender_predictor.py
351 lines (281 loc) · 14.3 KB
/
age_gender_predictor.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
"""
Author: Jacob Pitsenberger
Program: main.py
Version: 1.0
Project: Face Age and Gender Detection
Date: 12/7/2023
Uses: age_gender_predictor.py
Purpose: This module defines the AgeGenderPredictor class, which encapsulates the functionality for predicting
age and gender from webcam feeds, image files, and video files. It utilizes pre-trained models for
face detection, gender prediction, and age prediction.
The class includes methods for processing webcam feeds, image files, and video files, as well as
helper functions for face detection and prediction.
Note: Make sure to have the required model files in the 'weights' directory before using this module.
"""
from pathlib import Path
from typing import List, Tuple
import cv2
import numpy as np
import os
class AgeGenderPredictor:
"""
Age and gender prediction class using OpenCV and pre-trained models.
"""
DRAWING_COLOR = (0, 0, 255)
FONT_SCALE = 0.4
FONT_THICKNESS = 1
def __init__(self):
"""
Initialize the AgeGenderPredictor with pre-trained models.
"""
# Define gender and age prediction model mean values
self.MODEL_MEAN_VALUES = (78.4263377603, 87.7689143744, 114.895847746)
# Define gender labels
self.GENDER_LIST = ['Male', 'Female']
# Define age labels
self.AGE_INTERVALS = ['(0, self.FONT_THICKNESS)', '(4, 6)', '(8, 12)', '(15, 20)',
'(25, 32)', '(38, 43)', '(48, 53)', '(60, 100)']
# Paths for the face detection model
self.FACE_PROTO = "weights/deploy.prototxt.txt"
self.FACE_MODEL = "weights/res10_300x300_ssd_iter_140000_fp16.caffemodel"
# Load the face detection model
self.face_net = cv2.dnn.readNetFromCaffe(self.FACE_PROTO, self.FACE_MODEL)
# Paths for gender and age prediction models
self.GENDER_MODEL = 'weights/deploy_gender.prototxt'
self.GENDER_PROTO = 'weights/gender_net.caffemodel'
self.AGE_MODEL = 'weights/deploy_age.prototxt'
self.AGE_PROTO = 'weights/age_net.caffemodel'
# Load the gender and age prediction models
self.gender_net = cv2.dnn.readNetFromCaffe(self.GENDER_MODEL, self.GENDER_PROTO)
self.age_net = cv2.dnn.readNetFromCaffe(self.AGE_MODEL, self.AGE_PROTO)
def get_faces(self, frame: np.ndarray, confidence_threshold: float = 0.5) -> List[Tuple[int, int, int, int]]:
"""
Detect faces in the given frame.
Parameters:
- frame (np.ndarray): Input frame for face detection.
- confidence_threshold (float): Confidence threshold for face detection.
Returns:
- List[Tuple[int, int, int, int]]: List of faces represented as (start_x, start_y, end_x, end_y) tuples.
"""
# convert the frame into a blob to be ready for NN input
blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300), (104, 177.0, 123.0))
# set the image as input to the NN
self.face_net.setInput(blob)
# perform inference and get predictions
output = np.squeeze(self.face_net.forward())
# initialize the result list
faces = []
# Loop over the faces detected
for i in range(output.shape[0]):
confidence = output[i, 2]
if confidence > confidence_threshold:
box = output[i, 3:7] * \
np.array([frame.shape[1], frame.shape[0],
frame.shape[1], frame.shape[0]])
# convert to integers
start_x, start_y, end_x, end_y = box.astype(int)
# widen the box a little
start_x, start_y, end_x, end_y = start_x - \
10, start_y - 10, end_x + 10, end_y + 10
start_x = 0 if start_x < 0 else start_x
start_y = 0 if start_y < 0 else start_y
end_x = 0 if end_x < 0 else end_x
end_y = 0 if end_y < 0 else end_y
# append to our list
faces.append((start_x, start_y, end_x, end_y))
return faces
def get_gender_predictions(self, face_img: np.ndarray) -> np.ndarray:
"""
Get gender predictions for the given face image.
Parameters:
- face_img (np.ndarray): Input face image.
Returns:
- np.ndarray: Gender predictions.
"""
blob = cv2.dnn.blobFromImage(
image=face_img, scalefactor=1.0, size=(227, 227),
mean=self.MODEL_MEAN_VALUES, swapRB=False, crop=False
)
self.gender_net.setInput(blob)
return self.gender_net.forward()
def get_age_predictions(self, face_img: np.ndarray) -> np.ndarray:
"""
Get age predictions for the given face image.
Parameters:
- face_img (np.ndarray): Input face image.
Returns:
- np.ndarray: Age predictions.
"""
blob = cv2.dnn.blobFromImage(
image=face_img, scalefactor=1.0, size=(227, 227),
mean=self.MODEL_MEAN_VALUES, swapRB=False
)
self.age_net.setInput(blob)
return self.age_net.forward()
def process_webcam_feed(self, webcam_index: int = 0) -> None:
"""
Process the webcam feed for age and gender detection.
Parameters:
- webcam_index (int): Index of the webcam to use.
"""
cap = cv2.VideoCapture(webcam_index)
try:
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# Get faces from the frame
faces = self.get_faces(frame)
for face in faces:
# Extract face region
start_x, start_y, end_x, end_y = face
face_img = frame[start_y:end_y, start_x:end_x]
# Predict age and gender
age_preds = self.get_age_predictions(face_img)
gender_preds = self.get_gender_predictions(face_img)
# Find the indices with the highest prediction scores
i = gender_preds[0].argmax()
gender = self.GENDER_LIST[i] # 'Male' or 'Female'
gender_confidence_score = gender_preds[0][i] # Confidence score (e.g., 0.85)
i = age_preds[0].argmax()
age = self.AGE_INTERVALS[i] # e.g., '(25, 32)'
age_confidence_score = age_preds[0][i] # Confidence score (e.g., 0.75)
# Display the result on the frame
cv2.putText(frame, f"Age: {age} ({age_confidence_score:.2f})", (start_x, start_y - 5),
cv2.FONT_HERSHEY_SIMPLEX, self.FONT_SCALE, self.DRAWING_COLOR, self.FONT_THICKNESS)
cv2.putText(frame, f"Gender: {gender} ({gender_confidence_score:.2f})", (start_x, end_y + 20),
cv2.FONT_HERSHEY_SIMPLEX, self.FONT_SCALE, self.DRAWING_COLOR, self.FONT_THICKNESS)
# Draw rectangle around the face
cv2.rectangle(frame, (start_x, start_y), (end_x, end_y), self.DRAWING_COLOR, self.FONT_THICKNESS)
# Display the resulting frame
cv2.imshow('Webcam - Age and Gender Detection', frame)
# Break the loop if 'q' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except Exception as webcam_capture_error:
print(f"Error capturing from webcam: {webcam_capture_error}")
finally:
# When everything is done, release the capture
cap.release()
cv2.destroyAllWindows()
def process_image_file(self, image_path: str) -> None:
"""
Process a single image file for age and gender detection.
Parameters:
- image_path (str): Path to the input image file.
"""
try:
# Load image
frame = cv2.imread(image_path)
if frame is None:
print(f"Error: Couldn't read the image from {image_path}.")
return
# Get faces from the frame
faces = self.get_faces(frame)
for face in faces:
try:
# Extract face region
start_x, start_y, end_x, end_y = face
face_img = frame[start_y:end_y, start_x:end_x]
# Predict age and gender
age_preds = self.get_age_predictions(face_img)
gender_preds = self.get_gender_predictions(face_img)
# Find the indices with the highest prediction scores
i = gender_preds[0].argmax()
gender = self.GENDER_LIST[i] # 'Male' or 'Female'
gender_confidence_score = gender_preds[0][i] # Confidence score (e.g., 0.85)
i = age_preds[0].argmax()
age = self.AGE_INTERVALS[i] # e.g., '(25, 32)'
age_confidence_score = age_preds[0][i] # Confidence score (e.g., 0.75)
# Display the result on the frame
cv2.putText(frame, f"Age: {age} ({age_confidence_score:.2f})", (start_x, start_y - 5),
cv2.FONT_HERSHEY_SIMPLEX, self.FONT_SCALE, self.DRAWING_COLOR, self.FONT_THICKNESS)
cv2.putText(frame, f"Gender: {gender} ({gender_confidence_score:.2f})", (start_x, end_y + 20),
cv2.FONT_HERSHEY_SIMPLEX, self.FONT_SCALE, self.DRAWING_COLOR, self.FONT_THICKNESS)
# Draw rectangle around the face
cv2.rectangle(frame, (start_x, start_y), (end_x, end_y), self.DRAWING_COLOR, self.FONT_THICKNESS)
except Exception as face_processing_error:
print(f"Error processing face: {face_processing_error}")
# Display the resulting frame
cv2.imshow('Image - Age and Gender Detection', frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Save output image with predictions
output_filename = os.path.join('output_files', os.path.basename(image_path) + '_preds.jpg')
cv2.imwrite(output_filename, frame)
print(f"Output saved to: {output_filename}")
except Exception as image_processing_error:
print(f"Error processing image file: {image_processing_error}")
def get_video_detections(self, faces: List[Tuple[int, int, int, int]], frame: np.ndarray) -> np.ndarray:
"""
Get video frame with age and gender detections drawn on it.
Parameters:
- faces (List[Tuple[int, int, int, int]]): List of faces.
- frame (np.ndarray): Input video frame.
Returns:
- np.ndarray: Output video frame with detections.
"""
try:
for face in faces:
# Extract face region
start_x, start_y, end_x, end_y = face
face_img = frame[start_y:end_y, start_x:end_x]
# Predict age and gender
age_preds = self.get_age_predictions(face_img)
gender_preds = self.get_gender_predictions(face_img)
# Find the indices with the highest prediction scores
i = gender_preds[0].argmax()
gender = self.GENDER_LIST[i] # 'Male' or 'Female'
gender_confidence_score = gender_preds[0][i] # Confidence score (e.g., 0.85)
i = age_preds[0].argmax()
age = self.AGE_INTERVALS[i] # e.g., '(25, 32)'
age_confidence_score = age_preds[0][i] # Confidence score (e.g., 0.75)
# Display the result on the frame
cv2.putText(frame, f"Age: {age} ({age_confidence_score:.2f})", (start_x, start_y - 5),
cv2.FONT_HERSHEY_SIMPLEX, self.FONT_SCALE, self.DRAWING_COLOR, self.FONT_THICKNESS)
cv2.putText(frame, f"Gender: {gender} ({gender_confidence_score:.2f})", (start_x, end_y + 20),
cv2.FONT_HERSHEY_SIMPLEX, self.FONT_SCALE, self.DRAWING_COLOR, self.FONT_THICKNESS)
# Draw rectangle around the face
cv2.rectangle(frame, (start_x, start_y), (end_x, end_y), self.DRAWING_COLOR, self.FONT_THICKNESS)
# Return the frame with detections drawn on it.
return frame
except Exception as e:
print(f"Error getting video file detection: {e}")
raise
def process_video_file(self, video_path: str) -> None:
cap = cv2.VideoCapture(video_path)
try:
# Get the dimensions of the video frames.
ret, frame = cap.read()
if not ret:
print("Error: Couldn't read the first frame from the video.")
return
H, W, _ = frame.shape
# Specify the path to save the video with found detections.
base_filename = Path(video_path).stem
output_filename = os.path.join("output_files", f"{base_filename}_preds.mp4")
# Initialize our video writer for saving the output video.
out = cv2.VideoWriter(output_filename, cv2.VideoWriter_fourcc(*'mp4v'), int(cap.get(cv2.CAP_PROP_FPS)),
(W, H))
while ret:
# Get faces from the frame
faces = self.get_faces(frame)
out.write(frame)
# Keep reading the frames from the video file until they have all been processed.
ret, frame = cap.read()
# Handle the case when frame is None
if frame is None:
break
# Draw detections on the video frames
frame = self.get_video_detections(faces, frame)
# Display the resulting frame
cv2.imshow('Video - Age and Gender Detection', frame)
# Break the loop if 'q' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except Exception as video_capture_error:
print(f"Error capturing from video file: {video_capture_error}")
finally:
# Release the video capture and writer objects
cap.release()
out.release()
cv2.destroyAllWindows()