项目作者: mowshon

项目描述 :
Predict Age and Gender of people from images | Determination of gender and age
高级语言: C++
项目地址: git://github.com/mowshon/age-and-gender.git
创建时间: 2020-06-17T10:14:29Z
项目社区:https://github.com/mowshon/age-and-gender

开源协议:MIT License

下载


Predict Age and Gender using Python

This module will help you determine the gender and age of people from the image. The predict method returns a list of faces of people who were found in the image with a possible age and gender of the person.

Available for Python 2.7, 3.4, 3.5, 3.6, 3.7, 3.8

img

© Bill Gates family

Instalation

  1. git clone git@github.com:mowshon/age-and-gender.git
  2. cd age-and-gender
  3. python3 setup.py install --user

Download the pre-trained models

We use already trained models. Thanks for the provided models from: https://github.com/davisking/dlib-models

Author: Davis E. King

  1. shape_predictor_5_face_landmarks.dat.bz2 Download

This is a 5 point landmarking model which identifies the corners of the eyes and bottom of the nose. It is trained on the dlib 5-point face landmark dataset, which consists of 7198 faces. @davisking created this dataset by downloading images from the internet and annotating them with dlib’s imglab tool.

  1. dnn_age_predictor_v1.dat.bz2 Download

The initial source for the model’s creation came from the document of Z. Qawaqneh et al.: “Deep Convolutional Neural Network for Age Estimation based on VGG-Face Model”. However, our research has led us to significant improvements in the CNN model, allowing us to estimate the age of a person outperforming the state-of-the-art results in terms of the exact accuracy and for 1-off accuracy.

This model is thus an age predictor leveraging a ResNet-10 architecture and trained using a private dataset of about 110k different labelled images. During the training, we used an optimization and data augmentation pipeline and considered several sizes for the entry image.

This age predictor model is provided for free by Cydral Technology and is licensed under the Creative Commons Zero v1.0 Universal.

  1. dnn_gender_classifier_v1.dat.bz2 Download

This model is a gender classifier trained using a private dataset of about 200k different face images and was generated according to the network definition and settings given in Minimalistic CNN-based ensemble model for gender prediction from face images. Even if the dataset used for the training is different from that used by G. Antipov et al, the classification results on the LFW evaluation are similar overall (± 97.3%). To take up the authors’ proposal to join the results of three networks, a simplification was made by finally presenting RGB images, thus simulating three “grayscale” networks via the three image planes. Better results could be probably obtained with a more complex and deeper network, but the performance of the classification is nevertheless surprising compared to the simplicity of the network used and thus its very small size.

This gender model is provided for free by Cydral Technology and is licensed under the Creative Commons Zero v1.0 Universal.

  1. Unpack the *.bz2 archives, you need only the .dat file.

Folder structure

  1. test_example
  2. -- shape_predictor_5_face_landmarks.dat
  3. -- dnn_age_predictor_v1.dat
  4. -- dnn_gender_classifier_v1.dat
  5. -- test-image.jpg
  6. -- example.py

Example

  1. from age_and_gender import AgeAndGender
  2. from PIL import Image
  3. data.load_shape_predictor('shape_predictor_5_face_landmarks.dat')
  4. data.load_dnn_gender_classifier('dnn_gender_classifier_v1.dat')
  5. data.load_dnn_age_predictor('dnn_age_predictor_v1.dat')
  6. image = Image.open('test-image.jpg').convert("RGB")
  7. result = data.predict(image)
  8. print(result)

Result:

  1. [{'age': {'confidence': 85, 'value': 26},
  2. 'face': [414, 265, 504, 355],
  3. 'gender': {'confidence': 100, 'value': 'female'}},
  4. {'age': {'confidence': 58, 'value': 62},
  5. 'face': [223, 199, 330, 307],
  6. 'gender': {'confidence': 99, 'value': 'female'}},
  7. {'age': {'confidence': 73, 'value': 19},
  8. 'face': [593, 128, 700, 235],
  9. 'gender': {'confidence': 99, 'value': 'male'}},
  10. {'age': {'confidence': 50, 'value': 24},
  11. 'face': [342, 534, 450, 641],
  12. 'gender': {'confidence': 100, 'value': 'female'}},
  13. {'age': {'confidence': 92, 'value': 61},
  14. 'face': [782, 116, 872, 206],
  15. 'gender': {'confidence': 99, 'value': 'male'}}]

Examples of determining the gender and age of people from the image

Code: https://github.com/mowshon/age-and-gender/tree/master/example

How to increase efficiency with face_recognition ?

The module will try to determine where the faces of people are on the image. But, it is better for us to provide a variable with people’s faces using the library face_recognition and method face_locations().

  1. python -m pip install numpy --user
  2. python -m pip install face_recognition --user

Code:

  1. from age_and_gender import *
  2. from PIL import Image
  3. import face_recognition
  4. import numpy
  5. data = AgeAndGender()
  6. data.load_shape_predictor('models/shape_predictor_5_face_landmarks.dat')
  7. data.load_dnn_gender_classifier('models/dnn_gender_classifier_v1.dat')
  8. data.load_dnn_age_predictor('models/dnn_age_predictor_v1.dat')
  9. filename = 'test-image-2.jpg'
  10. img = Image.open(filename).convert("RGB")
  11. face_bounding_boxes = face_recognition.face_locations(
  12. numpy.asarray(img), # Convert to numpy array
  13. model='hog' # 'hog' for CPU | 'cnn' for GPU (NVIDIA with CUDA)
  14. )
  15. result = data.predict(img, face_bounding_boxes)

Module age-and-gender without face_recognition

img

Module age-and-gender with face_recognition and face_bounding_boxes

img

Full example of code: https://github.com/mowshon/age-and-gender/blob/master/example/example-with-face-recognition.py

Changelog

Version 1.0.1

  • The method predict(pillow_img) now require a PIL.Image object. Thanks to @arrufat for the piece of code that successfully performs the matrix conversion.
  • The method predict(pillow_img, face_bounding_boxes) takes another argument face_bounding_boxes with a list of faces in the image. Check out this example.
  • If the method predict(pillow_img) does not get the second argument face_bounding_boxes with a list of faces, then the module will try to find the faces in the image itself.

Version 1.0.0

  • Initial commit and code