-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathdetect_gender.py
86 lines (64 loc) · 2.26 KB
/
detect_gender.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
# author: Arun Ponnusamy
# website: https://www.arunponnusamy.com
# import necessary packages
from keras.preprocessing.image import img_to_array
from keras.models import load_model
from keras.utils import get_file
import numpy as np
import argparse
import cv2
import os
import cvlib as cv
# handle command line arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to input image")
args = ap.parse_args()
# download pre-trained model file (one-time download)
dwnld_link = "https://github.com/arunponnusamy/cvlib/releases/download/v0.2.0/gender_detection.model"
model_path = get_file("gender_detection.model", dwnld_link,
cache_subdir="pre-trained", cache_dir=os.getcwd())
# read input image
image = cv2.imread(args.image)
if image is None:
print("Could not read input image")
exit()
# load pre-trained model
model = load_model(model_path)
# detect faces in the image
face, confidence = cv.detect_face(image)
classes = ['man','woman']
# loop through detected faces
for idx, f in enumerate(face):
# get corner points of face rectangle
(startX, startY) = f[0], f[1]
(endX, endY) = f[2], f[3]
# draw rectangle over face
cv2.rectangle(image, (startX,startY), (endX,endY), (0,255,0), 2)
# crop the detected face region
face_crop = np.copy(image[startY:endY,startX:endX])
# preprocessing for gender detection model
face_crop = cv2.resize(face_crop, (96,96))
face_crop = face_crop.astype("float") / 255.0
face_crop = img_to_array(face_crop)
face_crop = np.expand_dims(face_crop, axis=0)
# apply gender detection on face
conf = model.predict(face_crop)[0]
print(conf)
print(classes)
# get label with max accuracy
idx = np.argmax(conf)
label = classes[idx]
label = "{}: {:.2f}%".format(label, conf[idx] * 100)
Y = startY - 10 if startY - 10 > 10 else startY + 10
# write label and confidence above face rectangle
cv2.putText(image, label, (startX, Y), cv2.FONT_HERSHEY_SIMPLEX,
0.7, (0, 255, 0), 2)
# display output
cv2.imshow("gender detection", image)
# press any key to close window
cv2.waitKey()
# save output
cv2.imwrite("gender_detection.jpg", image)
# release resources
cv2.destroyAllWindows()