richmondu / libfaceid

libfaceid is a research framework for prototyping of face recognition solutions. It seamlessly integrates multiple detection, recognition and liveness models w/ speech synthesis and speech recognition.
MIT License
489 stars 158 forks source link

bad performance for age and gender detection #18

Closed shartoo closed 5 years ago

shartoo commented 5 years ago

a photo below is a female while the result is Male with age [38,43] 00004

My detection code is

import cv2
from libfaceid.detector import FaceDetectorModels, FaceDetector
from libfaceid.encoder  import FaceEncoderModels, FaceEncoder
from libfaceid.pose import FacePoseEstimatorModels, FacePoseEstimator
from libfaceid.age import FaceAgeEstimatorModels, FaceAgeEstimator
from libfaceid.gender import FaceGenderEstimatorModels, FaceGenderEstimator
from libfaceid.emotion import FaceEmotionEstimatorModels, FaceEmotionEstimator

# Set the input directories
INPUT_DIR_MODEL_DETECTION  = "models/detection/"
INPUT_DIR_MODEL_ENCODING= "models/encoding/"
INPUT_DIR_MODEL_ESTIMATION = "models/estimation/"

def label_face(frame, face_rect, face_id, confidence):
    (x, y, w, h) = face_rect
    cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 255, 255), 1)
    if face_id is not None:
        cv2.putText(frame, "{} {:.2f}%".format(face_id, confidence),
            (x+5,y+h-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)

def detect_face_age_gender(img_path):
    frame = cv2.imread(img_path)
    detector = FaceDetectorModels.MTCNN
    encoder = FaceEncoderModels.LBPH
    ageestimator = FaceAgeEstimatorModels.CV2CAFFE
    genderestimator = FaceGenderEstimatorModels.CV2CAFFE
    ages = []
    genders = []
    try:
        # Initialize face detection
        face_detector = FaceDetector(model=detector, path=INPUT_DIR_MODEL_DETECTION, minfacesize=120)
        # Initialize face pose/age/gender estimation
        face_age_estimator = FaceAgeEstimator(model=ageestimator, path=INPUT_DIR_MODEL_ESTIMATION)
        face_gender_estimator = FaceGenderEstimator(model=genderestimator, path=INPUT_DIR_MODEL_ESTIMATION)
        faces = face_detector.detect(frame)
        for (index, face) in enumerate(faces):
            (x, y, w, h) = face
            # Detect age, gender
            face_image = frame[y:y + h, h:h + w]
            age = face_age_estimator.estimate(frame, face_image)
            gender = face_gender_estimator.estimate(frame, face_image)
            ages.append(age)
            genders.append(gender)
    except:
        print("Warning, check if models and trained dataset models exists!")
    return ages,genders

test_img = "./00004.jpg"
ages,genders = detect_face_age_gender(test_img)
for i in enumerate(ages):
    print("face id %d ,age: %d ,gender: %s"%(i,int(ages[i]),str(genders[i])))
richmondu commented 5 years ago

Nice pic.

These stuffs need to be tuned based on the dataset. There is no one-size fits all face recognition. Otherwise, Amazon, Google or Microsoft would have release it already.

Tune the parameters based on the datasets, based on how far is the face from the camera, based on the background lighting. I have provided the code. Now, go spread your wings. Dont just download and test. Fine tune based on your conditions. Go deeper.