#coding=utf-8
#性别识别

import cv2
from keras.models import load_model
import numpy as np
import chineseText

img = cv2.imread("img/gather.png")
face_classifier = cv2.CascadeClassifier(
    "d:\Python36\Lib\site-packages\opencv-master\data\haarcascades\haarcascade_frontalface_default.xml"
)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(
    gray, scaleFactor=1.2, minNeighbors=3, minSize=(140, 140))

gender_classifier = load_model(
    "classifier/gender_models/simple_CNN.81-0.96.hdf5")
gender_labels = {0: '女', 1: '男'}
color = (255, 255, 255)

for (x, y, w, h) in faces:
    face = img[(y - 60):(y + h + 60), (x - 30):(x + w + 30)]
    face = cv2.resize(face, (48, 48))
    face = np.expand_dims(face, 0)
    face = face / 255.0
    gender_label_arg = np.argmax(gender_classifier.predict(face))
    gender = gender_labels[gender_label_arg]
    cv2.rectangle(img, (x, y), (x + h, y + w), color, 2)
    img = chineseText.cv2ImgAddText(img, gender, x + h, y, color, 30)

cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

[(9, 'cv2.imread', 'cv2.imread', False, 'import cv2\n'), (10, 'cv2.CascadeClassifier', 'cv2.CascadeClassifier', False, 'import cv2\n'), (13, 'cv2.cvtColor', 'cv2.cvtColor', False, 'import cv2\n'), (17, 'keras.models.load_model', 'load_model', False, 'from keras.models import load_model\n'), (32, 'cv2.imshow', 'cv2.imshow', False, 'import cv2\n'), (33, 'cv2.waitKey', 'cv2.waitKey', False, 'import cv2\n'), (34, 'cv2.destroyAllWindows', 'cv2.destroyAllWindows', False, 'import cv2\n'), (24, 'cv2.resize', 'cv2.resize', False, 'import cv2\n'), (25, 'numpy.expand_dims', 'np.expand_dims', True, 'import numpy as np\n'), (29, 'cv2.rectangle', 'cv2.rectangle', False, 'import cv2\n'), (30, 'chineseText.cv2ImgAddText', 'chineseText.cv2ImgAddText', False, 'import chineseText\n')]