FaceLib: Face Analysis
Used for face detection, facial expression, AgeGender estimation and recognition with PyTorch.
- Instalation:
pip install git+https://github.com/sajjjadayobi/FaceLib.git
Check this example_notebook or take a look at the following sections
You can use these backbone networks: Resnet50, mobilenet. Default model is mobilenet and it will be automatically downloaded.
- The following example illustrates the ease of use of this package on your webcam:
fromfacelibimportWebcamFaceDetectordetector=WebcamFaceDetector()
detector.run()- Low-level access to bounding boxes and face landmarks
fromfacelibimportFaceDetectordetector=FaceDetector()
boxes, scores, landmarks=detector.detect_faces(image)For face aligment always use the detect_align function it gives you better performance.
- Face detection and aligment using the
detect_alignfunction.
fromfacelibimportFaceDetectordetector=FaceDetector()
faces, boxes, scores, landmarks=detector.detect_align(image)| Original | Aligned & Resized | Original | Aligned & Resized |
|---|---|---|---|
![]() | ![]() | ![]() | ![]() |
ShufflenetFull is the default model, and it will be automatically downloaded.
- Age and gender estimation live on your webcam (or any camera)
fromfacelibimportWebcamAgeGenderEstimatorestimator=WebcamAgeGenderEstimator()
estimator.run()- Low-lvel access to ages and genders
fromfacelibimportFaceDetector, AgeGenderEstimatorface_detector=FaceDetector()
age_gender_detector=AgeGenderEstimator()
faces, boxes, scores, landmarks=face_detector.detect_align(image)
genders, ages=age_gender_detector.detect(faces)
print(genders, ages)The default model is densnet121 and it will be automatically downloaded. Note that face size must be (224, 224).
- Emotion detector live on your webcam
fromfacelibimportWebcamEmotionDetectordetector=WebcamEmotionDetector()
detector.run()- Emotions as an array with their probabilities
fromfacelibimportFaceDetector, EmotionDetectorface_detector=FaceDetector(face_size=(224, 224))
emotion_detector=EmotionDetector()
faces, boxes, scores, landmarks=face_detector.detect_align(image)
emotions, probab=emotion_detector.detect_emotion(faces)- This module is a pytorch reimplementation of Arcface(paper), or Insightface(Github)
- IR-SE50
| LFW(%) | CFP-FF(%) | CFP-FP(%) | AgeDB-30(%) | calfw(%) | cplfw(%) | vgg2_fp(%) |
|---|---|---|---|---|---|---|
| 0.9952 | 0.9962 | 0.9504 | 0.9622 | 0.9557 | 0.9107 | 0.9386 |
- Mobilefacenet
| LFW(%) | CFP-FF(%) | CFP-FP(%) | AgeDB-30(%) | calfw(%) | cplfw(%) | vgg2_fp(%) |
|---|---|---|---|---|---|---|
| 0.9918 | 0.9891 | 0.8986 | 0.9347 | 0.9402 | 0.866 | 0.9100 |
Save the images of the faces you want to detect in this folder
Insightface/models/data/facebank/
---> person_1/
---> img_1.jpg
---> img_2.jpg
---> person_2/
---> img_1.jpg
---> img_2.jpg
You can save a new preson in facebank with 2 ways:
- Use
add_from_webcam: it takes 4 images and saves them on facebank.
fromfacelibimportadd_from_webcamadd_from_webcam(person_name='sajjad')- use
add_from_folder: it takes a path with some images from just a person.
fromfacelibimportadd_from_folderadd_from_folder(folder_path='./', person_name='sajjad')The default model is mobilenet and it will be automatically downloaded
- Face Recognition live on your webcam
fromfacelibimportWebcamVerifyverifier=WebcamVerify(update=True)
verifier.run()- Low-level access to your images
importcv2fromfacelibimportFaceRecognizer, FaceDetectorfromfacelibimportupdate_facebank, load_facebank, special_draw, get_configconf=get_config()
# conf.use_mobilenet=False # if you want to use the bigger modeldetector=FaceDetector(device=conf.device)
face_rec=FaceRecognizer(conf)
# set True when you add someone new to the facebankupdate_facebank_for_add_new_person=Falseifupdate_facebank_for_add_new_person:
targets, names=update_facebank(conf, face_rec.model, detector)
else:
targets, names=load_facebank(conf)
image=cv2.imread(your_path)
faces, boxes, scores, landmarks=detector.detect_align(image)
results, score=face_rec.infer(faces, targets)
print(names[results.cpu()])
foridx, bboxinenumerate(boxes):
special_draw(image, bbox, landmarks[idx], names[results[idx]+1], score[idx])Reference: InsightFace





