#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()

[((113, 141), 'cv2.imread', 'cv2.imread', (['"""img/gather.png"""'], {}), "('img/gather.png')\n", (123, 141), False, 'import cv2\n'), ((160, 299), 'cv2.CascadeClassifier', 'cv2.CascadeClassifier', (['"""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    )\n", (181, 299), False, 'import cv2\n'), ((296, 333), 'cv2.cvtColor', 'cv2.cvtColor', (['img', 'cv2.COLOR_BGR2GRAY'], {}), '(img, cv2.COLOR_BGR2GRAY)\n', (308, 333), False, 'import cv2\n'), ((460, 522), 'keras.models.load_model', 'load_model', (['"""classifier/gender_models/simple_CNN.81-0.96.hdf5"""'], {}), "('classifier/gender_models/simple_CNN.81-0.96.hdf5')\n", (470, 522), False, 'from keras.models import load_model\n'), ((1010, 1034), 'cv2.imshow', 'cv2.imshow', (['"""Image"""', 'img'], {}), "('Image', img)\n", (1020, 1034), False, 'import cv2\n'), ((1035, 1049), 'cv2.waitKey', 'cv2.waitKey', (['(0)'], {}), '(0)\n', (1046, 1049), False, 'import cv2\n'), ((1050, 1073), 'cv2.destroyAllWindows', 'cv2.destroyAllWindows', ([], {}), '()\n', (1071, 1073), False, 'import cv2\n'), ((685, 711), 'cv2.resize', 'cv2.resize', (['face', '(48, 48)'], {}), '(face, (48, 48))\n', (695, 711), False, 'import cv2\n'), ((723, 746), 'numpy.expand_dims', 'np.expand_dims', (['face', '(0)'], {}), '(face, 0)\n', (737, 746), True, 'import numpy as np\n'), ((886, 938), 'cv2.rectangle', 'cv2.rectangle', (['img', '(x, y)', '(x + h, y + w)', 'color', '(2)'], {}), '(img, (x, y), (x + h, y + w), color, 2)\n', (899, 938), False, 'import cv2\n'), ((949, 1008), 'chineseText.cv2ImgAddText', 'chineseText.cv2ImgAddText', (['img', 'gender', '(x + h)', 'y', 'color', '(30)'], {}), '(img, gender, x + h, y, color, 30)\n', (974, 1008), False, 'import chineseText\n')]