Skip to content

Commit 4e8197b

Browse files
committed
update gender detection tutorial
1 parent 608e722 commit 4e8197b

File tree

2 files changed

+192
-9
lines changed

2 files changed

+192
-9
lines changed

machine-learning/face-gender-detection/predict_gender.py

+35-9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
# Load gender prediction model
2525
gender_net = cv2.dnn.readNetFromCaffe(GENDER_MODEL, GENDER_PROTO)
2626

27+
# Initialize frame size
28+
frame_width = 1280
29+
frame_height = 720
30+
2731

2832
def get_faces(frame, confidence_threshold=0.5):
2933
# convert the frame into a blob to be ready for NN input
@@ -74,26 +78,48 @@ def get_optimal_font_scale(text, width):
7478
return scale/10
7579
return 1
7680

81+
# from: https://stackoverflow.com/questions/44650888/resize-an-image-without-distortion-opencv
82+
def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
83+
# initialize the dimensions of the image to be resized and
84+
# grab the image size
85+
dim = None
86+
(h, w) = image.shape[:2]
87+
# if both the width and height are None, then return the
88+
# original image
89+
if width is None and height is None:
90+
return image
91+
# check to see if the width is None
92+
if width is None:
93+
# calculate the ratio of the height and construct the
94+
# dimensions
95+
r = height / float(h)
96+
dim = (int(w * r), height)
97+
# otherwise, the height is None
98+
else:
99+
# calculate the ratio of the width and construct the
100+
# dimensions
101+
r = width / float(w)
102+
dim = (width, int(h * r))
103+
# resize the image
104+
return cv2.resize(image, dim, interpolation = inter)
105+
77106

78107
def predict_gender(input_path: str):
79108
"""Predict the gender of the faces showing in the image"""
80-
# Initialize frame size
81-
# frame_width = 1280
82-
# frame_height = 720
83109
# Read Input Image
84110
img = cv2.imread(input_path)
85111
# resize the image, uncomment if you want to resize the image
86112
# img = cv2.resize(img, (frame_width, frame_height))
87113
# Take a copy of the initial image and resize it
88114
frame = img.copy()
115+
if frame.shape[1] > frame_width:
116+
frame = image_resize(frame, width=frame_width)
89117
# predict the faces
90118
faces = get_faces(frame)
91119
# Loop over the faces detected
92120
# for idx, face in enumerate(faces):
93121
for i, (start_x, start_y, end_x, end_y) in enumerate(faces):
94122
face_img = frame[start_y: end_y, start_x: end_x]
95-
face_img = cv2.rectangle(
96-
frame, (start_x, start_y), (end_x, end_y), (0, 255, 0), 2)
97123
# image --> Input image to preprocess before passing it through our dnn for classification.
98124
# scale factor = After performing mean substraction we can optionally scale the image by some factor. (if 1 -> no scaling)
99125
# size = The spatial size that the CNN expects. Options are = (224*224, 227*227 or 299*299)
@@ -116,15 +142,15 @@ def predict_gender(input_path: str):
116142
# get the font scale for this image size
117143
optimal_font_scale = get_optimal_font_scale(label,((end_x-start_x)+25))
118144
box_color = (255, 0, 0) if gender == "Male" else (147, 20, 255)
119-
cv2.rectangle(face_img, (start_x, start_y), (end_x, end_y), box_color, 2)
145+
cv2.rectangle(frame, (start_x, start_y), (end_x, end_y), box_color, 2)
120146
# Label processed image
121-
cv2.putText(face_img, label, (start_x, yPos),
147+
cv2.putText(frame, label, (start_x, yPos),
122148
cv2.FONT_HERSHEY_SIMPLEX, optimal_font_scale, box_color, 2)
123149

124150
# Display processed image
125-
display_img("Gender Estimator", face_img)
151+
display_img("Gender Estimator", frame)
126152
# uncomment if you want to save the image
127-
cv2.imwrite("output.jpg", face_img)
153+
cv2.imwrite("output.jpg", frame)
128154
# Cleanup
129155
cv2.destroyAllWindows()
130156

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# Import Libraries
2+
import cv2
3+
import numpy as np
4+
5+
6+
# The gender model architecture
7+
# https://drive.google.com/open?id=1W_moLzMlGiELyPxWiYQJ9KFaXroQ_NFQ
8+
GENDER_MODEL = 'weights/deploy_gender.prototxt'
9+
# The gender model pre-trained weights
10+
# https://drive.google.com/open?id=1AW3WduLk1haTVAxHOkVS_BEzel1WXQHP
11+
GENDER_PROTO = 'weights/gender_net.caffemodel'
12+
# Each Caffe Model impose the shape of the input image also image preprocessing is required like mean
13+
# substraction to eliminate the effect of illunination changes
14+
MODEL_MEAN_VALUES = (78.4263377603, 87.7689143744, 114.895847746)
15+
# Represent the gender classes
16+
GENDER_LIST = ['Male', 'Female']
17+
# https://raw.githubusercontent.com/opencv/opencv/master/samples/dnn/face_detector/deploy.prototxt
18+
FACE_PROTO = "weights/deploy.prototxt.txt"
19+
# https://raw.githubusercontent.com/opencv/opencv_3rdparty/dnn_samples_face_detector_20180205_fp16/res10_300x300_ssd_iter_140000_fp16.caffemodel
20+
FACE_MODEL = "weights/res10_300x300_ssd_iter_140000_fp16.caffemodel"
21+
22+
# load face Caffe model
23+
face_net = cv2.dnn.readNetFromCaffe(FACE_PROTO, FACE_MODEL)
24+
# Load gender prediction model
25+
gender_net = cv2.dnn.readNetFromCaffe(GENDER_MODEL, GENDER_PROTO)
26+
27+
# Initialize frame size
28+
frame_width = 1280
29+
frame_height = 720
30+
31+
32+
def get_faces(frame, confidence_threshold=0.5):
33+
# convert the frame into a blob to be ready for NN input
34+
blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300), (104, 177.0, 123.0))
35+
# set the image as input to the NN
36+
face_net.setInput(blob)
37+
# perform inference and get predictions
38+
output = np.squeeze(face_net.forward())
39+
# initialize the result list
40+
faces = []
41+
# Loop over the faces detected
42+
for i in range(output.shape[0]):
43+
confidence = output[i, 2]
44+
if confidence > confidence_threshold:
45+
box = output[i, 3:7] * \
46+
np.array([frame.shape[1], frame.shape[0],
47+
frame.shape[1], frame.shape[0]])
48+
# convert to integers
49+
start_x, start_y, end_x, end_y = box.astype(np.int)
50+
# widen the box a little
51+
start_x, start_y, end_x, end_y = start_x - \
52+
10, start_y - 10, end_x + 10, end_y + 10
53+
start_x = 0 if start_x < 0 else start_x
54+
start_y = 0 if start_y < 0 else start_y
55+
end_x = 0 if end_x < 0 else end_x
56+
end_y = 0 if end_y < 0 else end_y
57+
# append to our list
58+
faces.append((start_x, start_y, end_x, end_y))
59+
return faces
60+
61+
62+
def get_optimal_font_scale(text, width):
63+
"""Determine the optimal font scale based on the hosting frame width"""
64+
for scale in reversed(range(0, 60, 1)):
65+
textSize = cv2.getTextSize(text, fontFace=cv2.FONT_HERSHEY_DUPLEX, fontScale=scale/10, thickness=1)
66+
new_width = textSize[0][0]
67+
if (new_width <= width):
68+
return scale/10
69+
return 1
70+
71+
# from: https://stackoverflow.com/questions/44650888/resize-an-image-without-distortion-opencv
72+
def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
73+
# initialize the dimensions of the image to be resized and
74+
# grab the image size
75+
dim = None
76+
(h, w) = image.shape[:2]
77+
# if both the width and height are None, then return the
78+
# original image
79+
if width is None and height is None:
80+
return image
81+
# check to see if the width is None
82+
if width is None:
83+
# calculate the ratio of the height and construct the
84+
# dimensions
85+
r = height / float(h)
86+
dim = (int(w * r), height)
87+
# otherwise, the height is None
88+
else:
89+
# calculate the ratio of the width and construct the
90+
# dimensions
91+
r = width / float(w)
92+
dim = (width, int(h * r))
93+
# resize the image
94+
return cv2.resize(image, dim, interpolation = inter)
95+
96+
97+
def predict_gender():
98+
"""Predict the gender of the faces showing in the image"""
99+
# create a new cam object
100+
cap = cv2.VideoCapture(0)
101+
102+
while True:
103+
_, img = cap.read()
104+
# resize the image, uncomment if you want to resize the image
105+
# img = cv2.resize(img, (frame_width, frame_height))
106+
# Take a copy of the initial image and resize it
107+
frame = img.copy()
108+
if frame.shape[1] > frame_width:
109+
frame = image_resize(frame, width=frame_width)
110+
# predict the faces
111+
faces = get_faces(frame)
112+
# Loop over the faces detected
113+
# for idx, face in enumerate(faces):
114+
for i, (start_x, start_y, end_x, end_y) in enumerate(faces):
115+
face_img = frame[start_y: end_y, start_x: end_x]
116+
# image --> Input image to preprocess before passing it through our dnn for classification.
117+
# scale factor = After performing mean substraction we can optionally scale the image by some factor. (if 1 -> no scaling)
118+
# size = The spatial size that the CNN expects. Options are = (224*224, 227*227 or 299*299)
119+
# mean = mean substraction values to be substracted from every channel of the image.
120+
# swapRB=OpenCV assumes images in BGR whereas the mean is supplied in RGB. To resolve this we set swapRB to True.
121+
blob = cv2.dnn.blobFromImage(image=face_img, scalefactor=1.0, size=(
122+
227, 227), mean=MODEL_MEAN_VALUES, swapRB=False, crop=False)
123+
# Predict Gender
124+
gender_net.setInput(blob)
125+
gender_preds = gender_net.forward()
126+
i = gender_preds[0].argmax()
127+
gender = GENDER_LIST[i]
128+
gender_confidence_score = gender_preds[0][i]
129+
# Draw the box
130+
label = "{}-{:.2f}%".format(gender, gender_confidence_score*100)
131+
print(label)
132+
yPos = start_y - 15
133+
while yPos < 15:
134+
yPos += 15
135+
# get the font scale for this image size
136+
optimal_font_scale = get_optimal_font_scale(label,((end_x-start_x)+25))
137+
box_color = (255, 0, 0) if gender == "Male" else (147, 20, 255)
138+
cv2.rectangle(frame, (start_x, start_y), (end_x, end_y), box_color, 2)
139+
# Label processed image
140+
cv2.putText(frame, label, (start_x, yPos),
141+
cv2.FONT_HERSHEY_SIMPLEX, optimal_font_scale, box_color, 2)
142+
143+
# Display processed image
144+
145+
# frame = cv2.resize(frame, (frame_height, frame_width))
146+
cv2.imshow("Gender Estimator", frame)
147+
if cv2.waitKey(1) == ord("q"):
148+
break
149+
# uncomment if you want to save the image
150+
# cv2.imwrite("output.jpg", frame)
151+
# Cleanup
152+
cv2.destroyAllWindows()
153+
154+
155+
156+
if __name__ == '__main__':
157+
predict_gender()

0 commit comments

Comments
 (0)