RetinaFace: Deep Face Detection Library in TensorFlow for Python

Overview

RetinaFace

Downloads Stars License

RetinaFace is a deep learning based cutting-edge facial detector for Python coming with facial landmarks.

RetinaFace is the face detection module of insightface project. The original implementation is mainly based on mxnet. Then, its tensorflow based re-implementation is published by Stanislas Bertrand.

This repo is heavily inspired from the study of Stanislas Bertrand. Its source code is simplified and it is transformed to pip compatible but the main structure of the reference model and its pre-trained weights are same.

Installation

The easiest way to install retinaface is to download it from pypi.

pip install retina-face

Face Detection - Demo

RetinaFace offers a face detection function. It expects an exact path of an image as input.

from retinaface import RetinaFace
resp = RetinaFace.detect_faces("img1.jpg")

Then it returns the facial area coordinates and some landmarks (eyes, nose and mouth) with a confidence score.

{
    "face_1": {
        "score": 0.9993440508842468,
        "facial_area": [155, 81, 434, 443],
        "landmarks": {
          "right_eye": [257.82974, 209.64787],
          "left_eye": [374.93427, 251.78687],
          "nose": [303.4773, 299.91144],
          "mouth_right": [228.37329, 338.73193],
          "mouth_left": [320.21982, 374.58798]
        }
  }
}

Alignment

A modern face recognition pipeline consists of 4 common stages: detect, align, represent and verify. Experiments show that alignment increases the face recognition accuracy almost 1%. Here, retinaface can find the facial landmarks including eye coordinates. In this way, it can apply alignment to detected faces with its extract faces function.

import matplotlib.pyplot as plt
faces = RetinaFace.extract_faces(img_path = "img.jpg", align = True)
for face in faces:
  plt.imshow(face)
  plt.show()

Face Recognition - Demo

Notice that face recognition module of insightface project is ArcFace, and face detection module is RetinaFace. ArcFace and RetinaFace pair is wrapped in deepface framework. Consider to use deepface if you need an end-to-end face recognition pipeline.

#!pip install deepface
from deepface import DeepFace
obj = DeepFace.verify("img1.jpg", "img2.jpg"
          , model_name = 'ArcFace', detector_backend = 'retinaface')
print(obj["verified"])

Notice that ArcFace got 99.40% accuracy on LFW data set whereas human beings just got 97.53%.

Support

There are many ways to support a project. Starring ⭐️ the repo is just one 🙏

Acknowledgements

This work is mainly based on the insightface project and retinaface paper; and it is heavily inspired from the re-implementation of retinaface-tf2 by Stanislas Bertrand. Finally, Bertrand's implemenation uses Fast R-CNN written by Ross Girshick in the background. All of those reference studies are licensed under MIT license.

Licence

This project is licensed under the MIT License - see LICENSE for more details.

Comments
  • Bug fix: alignment_procedure args order

    Bug fix: alignment_procedure args order

    Hello.

    In the function RetinaFace.extract_faces, there is a call to postprocess.alignment_procedure. The arguments left_eye and right_eye have their order switched, which can sometimes lead to faces being aligned upside down.

    opened by RodrigoTMOLima 11
  • Error importing retineface

    Error importing retineface

    from retinaface import RetinaFace RuntimeError: module compiled against API version 0xe but this version of numpy is 0xd RuntimeError: module compiled against API version 0xe but this version of numpy is 0xd ImportError: numpy.core.multiarray_umath failed to import ImportError: numpy.core.umath failed to import Traceback (most recent call last): File "", line 1, in File "C:\Users\ROHIT\anaconda3\envs\jarvis\lib\site-packages\retinaface\RetinaFace.py", line 10, in import tensorflow as tf File "C:\Users\ROHIT\anaconda3\envs\jarvis\lib\site-packages\tensorflow_init.py", line 41, in from tensorflow.python.tools import module_util as module_util File "C:\Users\ROHIT\anaconda3\envs\jarvis\lib\site-packages\tensorflow\python_init.py", line 46, in
    from tensorflow.python import data File "C:\Users\ROHIT\anaconda3\envs\jarvis\lib\site-packages\tensorflow\python\data_init_.py", line 25, in from tensorflow.python.data import experimental File "C:\Users\ROHIT\anaconda3\envs\jarvis\lib\site-packages\tensorflow\python\data\experimental_init_.py", line 99, in File "C:\Users\ROHIT\anaconda3\envs\jarvis\lib\site-packages\tensorflow\python\data\experimental\service_init_.py", line 140, in from tensorflow.python.data.experimental.ops.data_service_ops import distribute File "C:\Users\ROHIT\anaconda3\envs\jarvis\lib\site-packages\tensorflow\python\data\experimental\ops\data_service_ops.py", line 25, in from tensorflow.python.data.experimental.ops import compression_ops File "C:\Users\ROHIT\anaconda3\envs\jarvis\lib\site-packages\tensorflow\python\data\experimental\ops\compression_ops.py", line 20, in from tensorflow.python.data.util import structure File "C:\Users\ROHIT\anaconda3\envs\jarvis\lib\site-packages\tensorflow\python\data\util\structure.py", line 26, in from tensorflow.python.data.util import nest File "C:\Users\ROHIT\anaconda3\envs\jarvis\lib\site-packages\tensorflow\python\data\util\nest.py", line 40, in from tensorflow.python.framework import sparse_tensor as _sparse_tensor File "C:\Users\ROHIT\anaconda3\envs\jarvis\lib\site-packages\tensorflow\python\framework\sparse_tensor.py", line 28, in from tensorflow.python.framework import constant_op File "C:\Users\ROHIT\anaconda3\envs\jarvis\lib\site-packages\tensorflow\python\framework\constant_op.py", line 29, in from tensorflow.python.eager import execute File "C:\Users\ROHIT\anaconda3\envs\jarvis\lib\site-packages\tensorflow\python\eager\execute.py", line 27, in from tensorflow.python.framework import dtypes File "C:\Users\ROHIT\anaconda3\envs\jarvis\lib\site-packages\tensorflow\python\framework\dtypes.py", line 32, in _np_bfloat16 = _pywrap_bfloat16.TF_bfloat16_type() TypeError: Unable to convert function return value to a Python type! The signature was () -> handle

    help wanted 
    opened by 007rohitSaini 5
  • change the order of arguments

    change the order of arguments

    When calling the postprocess.alignment_procedure function within the RetinaFace.extract_faces function, the order of the arguments is incorrect and changed.

    opened by glucose6126 4
  • Disable Keras progression bar

    Disable Keras progression bar

    It's a bit annoying that retinaface keeps showing Keras progression bar on every detection. It's especially taxing to Jupyter notebook where it starts to lag after a few hundred faces. I think there should be an option like in deepface where we can set prog_bar=True/False.

    dependency 
    opened by amar-enkhbat 3
  • AttributeError: module 'tensorflow' has no attribute 'function'

    AttributeError: module 'tensorflow' has no attribute 'function'

    !pip install tensorflow==2.2.0(1.9.0)
    !pip install h5py==2.10.0
    !pip install numpy
    !pip install pandas
    !pip install gdown
    !pip install tqdm
    !pip install Pillow
    !pip install opencv-python
    !pip install opencv-contrib-python
    !pip install keras
    !pip install Flask
    !pip install mtcnn
    !pip install lightgbm
    !pip install dlib
    !pip install retina-face
    !pip install deepface
    
    from deepface import DeepFace
    from retinaface import RetinaFace
    
    faces = RetinaFace.extract_faces("ai.jpg")
    
    print("There are ", len(faces), " faces in the image")
    

    When I try to execute the above code, I get the following error:

    AttributeError                            Traceback (most recent call last)
    <ipython-input-4-f1e5ccfe8b11> in <module>
          4 import os
          5 
    ----> 6 faces = RetinaFace.extract_faces("ai.jpg")
          7 
          8 print("There are ", len(faces), " faces in the image")
    
    /usr/local/lib/python3.6/site-packages/retinaface/RetinaFace.py in extract_faces(img_path, threshold, model, align, allow_upscaling)
        192     #---------------------------
        193 
    --> 194     obj = detect_faces(img_path = img, threshold = threshold, model = model, allow_upscaling = allow_upscaling)
        195 
        196     if type(obj) == dict:
    
    /usr/local/lib/python3.6/site-packages/retinaface/RetinaFace.py in detect_faces(img_path, threshold, model, allow_upscaling)
         66 
         67     if model is None:
    ---> 68         model = build_model()
         69 
         70     #---------------------------
    
    /usr/local/lib/python3.6/site-packages/retinaface/RetinaFace.py in build_model()
         31     if not "model" in globals():
         32 
    ---> 33         model = tf.function(
         34             retinaface_model.build_model(),
         35             input_signature=(tf.TensorSpec(shape=[None, None, None, 3], dtype=np.float32),)
    
    AttributeError: module 'tensorflow' has no attribute 'function'
    
    dependency 
    opened by zengxunli 3
  • Slow detection

    Slow detection

    Hello, very good recognition results! But very slow. My code: `from retinaface import RetinaFace import cv2 from datetime import datetime

    start_time = datetime.now()

    img_path = '/home/uba/Desktop/azs/video/test-opencv/frame0-03-56.52.jpg' faces = RetinaFace.detect_faces(img_path) print(faces) print(datetime.now() - start_time) img = cv2.imread(img_path)

    for face in faces.keys(): identity = faces[face] facial_area = identity['facial_area'] cv2.rectangle(img, (facial_area[2], facial_area[3]), (facial_area[0], facial_area[1]), (255, 255, 255), 1)

    cv2.imshow('', img) cv2.waitKey(0) cv2.destroyAllWindows()`

    Computer: AMD Ryzen 5 3600 6-Core Processor GeForce GTX 1660 ram 16+16+8 Ubuntu 20.04

    Why so slow ?

    question 
    opened by ninnnnnnr 3
  • Optional image upscaling

    Optional image upscaling

    In a project working with a colleague, we found that when images were smaller than the default size (1024), e.g., 250 x 250, the runtime was longer than expected because of the upsizing process. However, it appeared to us that the upsizing would not help in detection accuracy. We ended up monkey-patched this proposed change to resize_image, and saw significant improvement to runtime, without sacrificing accuracy. We suggest that the upsizing is probably not necessary (or perhaps making it optional).

    I also suggest fixing the order: img_h, img_w, as the first axis of a NumPy image array gives the image height. Even though this does not affect the result in this function, in the future some contributors may get the wrong idea.

    Thanks!

    opened by bhky 3
  • ValueError: The channel dimension of the inputs should be defined. Found `None`.

    ValueError: The channel dimension of the inputs should be defined. Found `None`.

    Traceback (most recent call last): File "unit-tests.py", line 10, in resp = RetinaFace.detect_faces(img, threshold = 0.5) File "D:\Face_Capture\Server33_Live\RetinaFace\retinaface-master\retinaface\RetinaFace.py", line 54, in detect_faces model = build_model() File "D:\Face_Capture\Server33_Live\RetinaFace\retinaface-master\retinaface\RetinaFace.py", line 30, in build_model retinaface_model.build_model(), File "D:\Face_Capture\Server33_Live\RetinaFace\retinaface-master\retinaface\model\retinaface_model.py", line 486, in build_model ssh_c2_aggr = Conv2D(filters = 256, kernel_size = (3, 3), name = 'ssh_c2_aggr', strides = [1, 1], padding = 'VALID', use_bias = True)(ssh_c2_aggr_pad) File "D:\python_fancp\Anaconda3.7\lib\site-packages\keras\engine\base_layer.py", line 431, in call self.build(unpack_singleton(input_shapes)) File "D:\python_fancp\Anaconda3.7\lib\site-packages\keras\layers\convolutional.py", line 132, in build raise ValueError('The channel dimension of the inputs ' ValueError: The channel dimension of the inputs should be defined. Found None.

    dependency 
    opened by fanchunpeng 3
  • retinaface is not importing. pls help

    retinaface is not importing. pls help

    Python 3.9.12 (main, Mar 24 2022, 13:02:21) [GCC 11.2.0] on linux Type "help", "copyright", "credits" or "license" for more information.

    import retinaface Traceback (most recent call last): File "", line 1, in File "/home/andy/Desktop/img check/retinaface.py", line 1, in from retinaface import RetinaFace ImportError: cannot import name 'RetinaFace' from partially initialized module 'retinaface' (most likely due to a circular import) (/home/andy/Desktop/img check/retinaface.py)

    opened by JacobTauro 2
  • Get AttributeError: 'Tensor' object has no attribute 'numpy' when RetinaFace.detect_faces(image)

    Get AttributeError: 'Tensor' object has no attribute 'numpy' when RetinaFace.detect_faces(image)

    Traceback (most recent call last):
    File "face_service.py", line 101, in face_detection    
        bboxes = RetinaFace.detect_faces(image)
    File "/usr/local/lib/python3.8/dist-packages/retinaface/RetinaFace.py", line 91, in detect_faces
        net_out = [elt.numpy() for elt in net_out]
    File "/usr/local/lib/python3.8/dist-packages/retinaface/RetinaFace.py", line 91, in <listcomp>
        net_out = [elt.numpy() for elt in net_out]
    File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/framework/ops.py", line 401, in __getattr__
        self.__getattribute__(name)
    AttributeError: 'Tensor' object has no attribute 'numpy'
    
    dependency 
    opened by madityarafip 2
  • Error when call RetinaFace.detect_faces

    Error when call RetinaFace.detect_faces

    Traceback (most recent call last): File "", line 1, in File "/home/ubuntu/anaconda3/envs/dev/lib/python3.7/site-packages/retinaface/RetinaFace.py", line 90, in detect_faces net_out = model(im_tensor) File "/home/ubuntu/anaconda3/envs/dev/lib/python3.7/site-packages/tensorflow/python/util/traceback_utils.py", line 153, in error_handler raise e.with_traceback(filtered_tb) from None File "/home/ubuntu/anaconda3/envs/dev/lib/python3.7/site-packages/tensorflow/python/eager/execute.py", line 55, in quick_execute inputs, attrs, num_outputs) tensorflow.python.framework.errors_impl.InternalError: Graph execution error:

    Detected at node 'model/bn_data/FusedBatchNormV3' defined at (most recent call last): File "", line 1, in File "/home/ubuntu/anaconda3/envs/dev/lib/python3.7/site-packages/retinaface/RetinaFace.py", line 90, in detect_faces net_out = model(im_tensor) File "/home/ubuntu/anaconda3/envs/dev/lib/python3.7/site-packages/keras/engine/base_layer.py", line 982, in error_handler def call(self, *args, **kwargs): File "/home/ubuntu/anaconda3/envs/dev/lib/python3.7/site-packages/keras/engine/base_layer.py", line 987, in error_handler **kwargs: Keyword arguments to be passed to self.call. File "/home/ubuntu/anaconda3/envs/dev/lib/python3.7/site-packages/keras/engine/base_layer.py", line 1096, in call outputs = call_fn(inputs, *args, **kwargs) File "/home/ubuntu/anaconda3/envs/dev/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 92, in error_handler return fn(*args, **kwargs) File "/home/ubuntu/anaconda3/envs/dev/lib/python3.7/site-packages/keras/engine/functional.py", line 452, in call inputs, training=training, mask=mask) File "/home/ubuntu/anaconda3/envs/dev/lib/python3.7/site-packages/keras/engine/functional.py", line 589, in _run_internal_graph outputs = node.layer(*args, **kwargs) File "/home/ubuntu/anaconda3/envs/dev/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler return fn(*args, **kwargs) File "/home/ubuntu/anaconda3/envs/dev/lib/python3.7/site-packages/keras/engine/base_layer.py", line 1096, in call outputs = call_fn(inputs, *args, **kwargs) File "/home/ubuntu/anaconda3/envs/dev/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 92, in error_handler return fn(*args, **kwargs) File "/home/ubuntu/anaconda3/envs/dev/lib/python3.7/site-packages/keras/layers/normalization/batch_normalization.py", line 767, in call outputs = self._fused_batch_norm(inputs, training=training) File "/home/ubuntu/anaconda3/envs/dev/lib/python3.7/site-packages/keras/layers/normalization/batch_normalization.py", line 624, in _fused_batch_norm training, train_op, _fused_batch_norm_inference) File "/home/ubuntu/anaconda3/envs/dev/lib/python3.7/site-packages/keras/utils/control_flow_util.py", line 106, in smart_cond pred, true_fn=true_fn, false_fn=false_fn, name=name) File "/home/ubuntu/anaconda3/envs/dev/lib/python3.7/site-packages/keras/layers/normalization/batch_normalization.py", line 613, in _fused_batch_norm_inference data_format=self._data_format) Node: 'model/bn_data/FusedBatchNormV3' cuDNN launch failure : input shape ([1,3,1024,1222]) [[{{node model/bn_data/FusedBatchNormV3}}]] [Op:__inference_function_7605]

    dependency 
    opened by hungnmai 2
  • Implement expanding the facial area

    Implement expanding the facial area

    • Fixed formatting
    • Added "expand_face_area" argument to extract_faces() in Line 206. Now, the function can return the larger area around the face by expand_face_area number of pixels. See issue #37
    opened by cansakirt 0
  • Also return original image face location when extracting faces

    Also return original image face location when extracting faces

    I wrote a few simple lines to retrieve the extracted faces' original image locations alongside the faces themselves, because when one detects a lot of faces from one image with RetinaFace and uses Deepface to verify all of these faces against some other face reference image, it is very useful to visualize where exactly the verified face was found in the original image. As far as I understand, this functionality in this specific use case was not possible before.

    Example semi-pseudo code:

    filename = "path_to_image"
    faces_and_locations = RetinaFace.extract_faces(filename, return_location = True)
    
    image = Image.open(filename)
    
    model = DeepFace.build_model("Facenet512")
    for face, face_location in faces_and_locations:
        verification = DeepFace.verify(img1_path = face, img2_path = "path_to_ref_img", model = model, detector_backend="retinaface", enforce_detection=False)
        
        if verification["verified"] == True:
            draw_rectangle_in_image(image, face_location, "green", width = 5, deepface = False)
        else:
            draw_rectangle_in_image(image, face_location, "red", width = 5, deepface = False)
            
    display(image)
    

    So in this example, where I wanted to find David Hasselhoff, we get something like this: image

    opened by IngvarBaranin 0
  • Get larger cropped, aligned faces

    Get larger cropped, aligned faces

    Currently the extracted faces are cropped to a very narrow region around the landmarks, often times cropping out the chin or the hair.

    Is is possible to set the bounding box of cropping after alignment/rotation to get larger crops?

    Specifically, I would like to have the faces, but with a bit of the surrounding for a timelapse.

    enhancement 
    opened by skjerns 3
Owner
Sefik Ilkin Serengil
👨‍💻Software Engineer 🎓GSU alumni ⌨️Blogger 🏠Istanbulite 💬Code wins arguments
Sefik Ilkin Serengil
Face Library is an open source package for accurate and real-time face detection and recognition

Face Library Face Library is an open source package for accurate and real-time face detection and recognition. The package is built over OpenCV and us

null 52 Nov 9, 2022
Swapping face using Face Mesh with TensorFlow Lite

Swapping face using Face Mesh with TensorFlow Lite

iwatake 17 Apr 26, 2022
img2pose: Face Alignment and Detection via 6DoF, Face Pose Estimation

img2pose: Face Alignment and Detection via 6DoF, Face Pose Estimation Figure 1: We estimate the 6DoF rigid transformation of a 3D face (rendered in si

Vítor Albiero 519 Dec 29, 2022
Code for HLA-Face: Joint High-Low Adaptation for Low Light Face Detection (CVPR21)

HLA-Face: Joint High-Low Adaptation for Low Light Face Detection The official PyTorch implementation for HLA-Face: Joint High-Low Adaptation for Low L

Wenjing Wang 77 Dec 8, 2022
AI Face Mesh: This is a simple face mesh detection program based on Artificial intelligence.

AI Face Mesh: This is a simple face mesh detection program based on Artificial Intelligence which made with Python. It's able to detect 468 different

Md. Rakibul Islam 1 Jan 13, 2022
A python library for face detection and features extraction based on mediapipe library

FaceAnalyzer A python library for face detection and features extraction based on mediapipe library Introduction FaceAnalyzer is a library based on me

Saifeddine ALOUI 14 Dec 30, 2022
Face Mask Detection on Image and Video using tensorflow and keras

Face-Mask-Detection Face Mask Detection on Image and Video using tensorflow and keras Train Neural Network on face-mask dataset using tensorflow and k

Nahid Ebrahimian 12 Nov 11, 2022
Face Mask Detection System built with OpenCV, TensorFlow using Computer Vision concepts

Face mask detection Face Mask Detection System built with OpenCV, TensorFlow using Computer Vision concepts in order to detect face masks in static im

Vaibhav Shukla 1 Oct 27, 2021
Python tools for 3D face: 3DMM, Mesh processing(transform, camera, light, render), 3D face representations.

face3d: Python tools for processing 3D face Introduction This project implements some basic functions related to 3D faces. You can use this to process

Yao Feng 2.3k Dec 30, 2022
Face-Recognition-Attendence-System - This face recognition Attendence system using Python

Face-Recognition-Attendence-System I have developed this face recognition Attend

Riya Gupta 4 May 10, 2022
Video-face-extractor - Video face extractor with Python

Python face extractor Setup Create the srcvideos and faces directories Put your

null 2 Feb 3, 2022
End-to-end face detection, cropping, norm estimation, and landmark detection in a single onnx model

onnx-facial-lmk-detector End-to-end face detection, cropping, norm estimation, and landmark detection in a single onnx model, model.onnx. Demo You can

atksh 42 Dec 30, 2022
Face detection using deep learning.

Face Detection Docker Solution Using Faster R-CNN Dockerface is a deep learning face detector. It deploys a trained Faster R-CNN network on Caffe thro

Nataniel Ruiz 181 Dec 19, 2022
Face Mask Detection is a project to determine whether someone is wearing mask or not, using deep neural network.

face-mask-detection Face Mask Detection is a project to determine whether someone is wearing mask or not, using deep neural network. It contains 3 scr

amirsalar 13 Jan 18, 2022
DVG-Face: Dual Variational Generation for Heterogeneous Face Recognition, TPAMI 2021

DVG-Face: Dual Variational Generation for HFR This repo is a PyTorch implementation of DVG-Face: Dual Variational Generation for Heterogeneous Face Re

null 52 Dec 30, 2022
[TIP 2021] SADRNet: Self-Aligned Dual Face Regression Networks for Robust 3D Dense Face Alignment and Reconstruction

SADRNet Paper link: SADRNet: Self-Aligned Dual Face Regression Networks for Robust 3D Dense Face Alignment and Reconstruction Requirements python

Multimedia Computing Group, Nanjing University 99 Dec 30, 2022
Face Synthetics dataset is a collection of diverse synthetic face images with ground truth labels.

The Face Synthetics dataset Face Synthetics dataset is a collection of diverse synthetic face images with ground truth labels. It was introduced in ou

Microsoft 608 Jan 2, 2023
VGGFace2-HQ - A high resolution face dataset for face editing purpose

The first open source high resolution dataset for face swapping!!! A high resolution version of VGGFace2 for academic face editing purpose

Naiyuan Liu 232 Dec 29, 2022