-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathf_my_age.py
77 lines (66 loc) · 2.89 KB
/
f_my_age.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
"""
como usar
1.instalar la libreria deepface
pip install deepface
2. instanciar el modelo
emo = f_my_emotion.Age_Model()
3. ingresar una imagen donde solo se vea un rostro (usar modelo deteccion de rostros para extraer una imagen con solo el rostro)
emo.predict_age(face_image)
"""
#from basemodels import VGGFace
from deepface.basemodels import VGGFace
import os
from pathlib import Path
import gdown
import numpy as np
import tensorflow as tf
from keras.models import Model, Sequential
from keras.layers import Convolution2D, Flatten, Activation
#from keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input
from tensorflow.keras.utils import img_to_array
import cv2
class Age_Model():
def __init__(self):
self.model = self.loadModel()
self.output_indexes = np.array([i for i in range(0, 101)])
def predict_age(self,face_image):
image_preprocesing = self.transform_face_array2age_face(face_image)
age_predictions = self.model.predict(image_preprocesing )[0,:]
result_age = self.findApparentAge(age_predictions)
return result_age
def loadModel(self):
model = VGGFace.baseModel()
#--------------------------
classes = 101
base_model_output = Sequential()
base_model_output = Convolution2D(classes, (1, 1), name='predictions')(model.layers[-4].output)
base_model_output = Flatten()(base_model_output)
base_model_output = Activation('softmax')(base_model_output)
#--------------------------
age_model = Model(inputs=model.input, outputs=base_model_output)
#--------------------------
#load weights
home = str(Path.home())
if os.path.isfile(home+'/.deepface/weights/age_model_weights.h5') != True:
print("age_model_weights.h5 will be downloaded...")
url = 'https://drive.google.com/uc?id=1YCox_4kJ-BYeXq27uUbasu--yz28zUMV'
output = home+'/.deepface/weights/age_model_weights.h5'
gdown.download(url, output, quiet=False)
age_model.load_weights(home+'/.deepface/weights/age_model_weights.h5')
return age_model
#--------------------------
def findApparentAge(self,age_predictions):
apparent_age = np.sum(age_predictions * self.output_indexes)
return apparent_age
def transform_face_array2age_face(self,face_array,grayscale=False,target_size = (224, 224)):
detected_face = face_array
if grayscale == True:
detected_face = cv2.cvtColor(detected_face, cv2.COLOR_BGR2GRAY)
detected_face = cv2.resize(detected_face, target_size)
#img_pixels = image.img_to_array(detected_face)
img_pixels = tf.keras.utils.img_to_array(detected_face)
img_pixels = np.expand_dims(img_pixels, axis = 0)
#normalize input in [0, 1]
img_pixels /= 255
return img_pixels