#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, 6, 9, 34), 'cv2.imread', 'cv2.imread', ({(9, 17, 9, 33): '"""img/gather.png"""'}, {}), "('img/gather.png')", False, 'import cv2\n'), ((10, 18, 12, 1), 'cv2.CascadeClassifier', 'cv2.CascadeClassifier', ({(11, 4, 11, 103): '"""d:\\\\Python36\\\\Lib\\\\site-packages\\\\opencv-master\\\\data\\\\haarcascades\\\\haarcascade_frontalface_default.xml"""'}, {}), "(\n    'd:\\\\Python36\\\\Lib\\\\site-packages\\\\opencv-master\\\\data\\\\haarcascades\\\\haarcascade_frontalface_default.xml'\n    )", False, 'import cv2\n'), ((13, 7, 13, 44), 'cv2.cvtColor', 'cv2.cvtColor', ({(13, 20, 13, 23): 'img', (13, 25, 13, 43): 'cv2.COLOR_BGR2GRAY'}, {}), '(img, cv2.COLOR_BGR2GRAY)', False, 'import cv2\n'), ((17, 20, 18, 55), 'keras.models.load_model', 'load_model', ({(18, 4, 18, 54): '"""classifier/gender_models/simple_CNN.81-0.96.hdf5"""'}, {}), "('classifier/gender_models/simple_CNN.81-0.96.hdf5')", False, 'from keras.models import load_model\n'), ((32, 0, 32, 24), 'cv2.imshow', 'cv2.imshow', ({(32, 11, 32, 18): '"""Image"""', (32, 20, 32, 23): 'img'}, {}), "('Image', img)", False, 'import cv2\n'), ((33, 0, 33, 14), 'cv2.waitKey', 'cv2.waitKey', ({(33, 12, 33, 13): '(0)'}, {}), '(0)', False, 'import cv2\n'), ((34, 0, 34, 23), 'cv2.destroyAllWindows', 'cv2.destroyAllWindows', ({}, {}), '()', False, 'import cv2\n'), ((24, 11, 24, 37), 'cv2.resize', 'cv2.resize', ({(24, 22, 24, 26): 'face', (24, 28, 24, 36): '(48, 48)'}, {}), '(face, (48, 48))', False, 'import cv2\n'), ((25, 11, 25, 34), 'numpy.expand_dims', 'np.expand_dims', ({(25, 26, 25, 30): 'face', (25, 32, 25, 33): '0'}, {}), '(face, 0)', True, 'import numpy as np\n'), ((29, 4, 29, 56), 'cv2.rectangle', 'cv2.rectangle', ({(29, 18, 29, 21): 'img', (29, 23, 29, 29): '(x, y)', (29, 31, 29, 45): '(x + h, y + w)', (29, 47, 29, 52): 'color', (29, 54, 29, 55): '(2)'}, {}), '(img, (x, y), (x + h, y + w), color, 2)', False, 'import cv2\n'), ((30, 10, 30, 69), 'chineseText.cv2ImgAddText', 'chineseText.cv2ImgAddText', ({(30, 36, 30, 39): 'img', (30, 41, 30, 47): 'gender', (30, 49, 30, 54): 'x + h', (30, 56, 30, 57): 'y', (30, 59, 30, 64): 'color', (30, 66, 30, 68): '30'}, {}), '(img, gender, x + h, y, color, 30)', False, 'import chineseText\n')]