Giter VIP home page Giter VIP logo

serengil / retinaface Goto Github PK

View Code? Open in Web Editor NEW
957.0 4.0 136.0 13.75 MB

RetinaFace: Deep Face Detection Library for Python

Home Page: https://www.youtube.com/watch?v=Wm1DucuQk70&list=PLsS_1RYmYQQFdWqxQggXHynP1rqaYXv_E&index=3

License: MIT License

Python 99.56% Makefile 0.13% Shell 0.31%
face-detection retinaface retinaface-detector python tensorflow face-recognition face-alignment insightface face-detector

retinaface's Introduction

RetinaFace

PyPI Downloads Conda Downloads Stars License Tests

Blog YouTube Twitter Support me on Patreon GitHub Sponsors

DOI DOI

RetinaFace is a deep learning based cutting-edge facial detector for Python coming with facial landmarks. Its detection performance is amazing even in the crowd as shown in the following illustration.

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. So, 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.


The Yellow Angels - Fenerbahce Women's Volleyball Team

Installation PyPI Conda

The easiest way to install retinaface is to download it from PyPI. It's going to install the library itself and its prerequisites as well.

$ pip install retina-face

RetinaFace is also available at Conda. You can alternatively install the package via conda.

$ conda install -c conda-forge retina-face

Then, you will be able to import the library and use its functionalities.

from retinaface import RetinaFace

Face Detection - Demo

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

resp = RetinaFace.detect_faces("img1.jpg")

Then, it will return 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 - Tutorial, Demo

A modern face recognition pipeline consists of 4 common stages: detect, align, normalize, 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 extracting 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()

If you prefer to prioritize alignment before detection, you may opt to set the align_first parameter to True. By following this approach, you will eliminate the black pixel areas that arise as a result of alignment following detection. This functionality is applicable only when the provided image contains a single face.

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 library for Python. 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 have 97.53% confidence.

Contribution Tests

Pull requests are more than welcome! You should run the unit tests and linting locally before creating a PR. Commands make test and make lint will help you to run it locally. Once a PR created, GitHub test workflow will be run automatically and unit test results will be available in GitHub actions before approval.

Support

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

You can also support this work on Patreon

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 implementation uses Fast R-CNN written by Ross Girshick in the background. All of those reference studies are licensed under MIT license.

Citation

If you are using RetinaFace in your research, please consider to cite its original research paper. Besides, if you are using this re-implementation of retinaface, please consider to cite the following research papers as well. Here are examples of BibTeX entries:

@inproceedings{serengil2020lightface,
  title        = {LightFace: A Hybrid Deep Face Recognition Framework},
  author       = {Serengil, Sefik Ilkin and Ozpinar, Alper},
  booktitle    = {2020 Innovations in Intelligent Systems and Applications Conference (ASYU)},
  pages        = {23-27},
  year         = {2020},
  doi          = {10.1109/ASYU50717.2020.9259802},
  url          = {https://doi.org/10.1109/ASYU50717.2020.9259802},
  organization = {IEEE}
}
@inproceedings{serengil2021lightface,
  title        = {HyperExtended LightFace: A Facial Attribute Analysis Framework},
  author       = {Serengil, Sefik Ilkin and Ozpinar, Alper},
  booktitle    = {2021 International Conference on Engineering and Emerging Technologies (ICEET)},
  pages        = {1-4},
  year         = {2021},
  doi          = {10.1109/ICEET53442.2021.9659697},
  url          = {https://doi.org/10.1109/ICEET53442.2021.9659697},
  organization = {IEEE}
}

Finally, if you use this RetinaFace re-implementation in your GitHub projects, please add retina-face dependency in the requirements.txt.

Licence

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

retinaface's People

Contributors

bhky avatar bit-guber avatar cansakirt avatar csmailis avatar dobiasd avatar erwannmillon avatar ibarbech avatar nopdotcom avatar seaotocinclus avatar serengil avatar umutdeniz26 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

retinaface's Issues

postprocess line 28

    if False:

what is supposed to happen here, I am bit of beginner but how it is supposed to enter since False will never be True

thanks

P.P. wasnt this condition supposed to be a check with the between the eyes point compared to the nose,

Image resolution for inference

Hi,
What is the default image resolution on which the model operates? I tried with 360p and 1080p and it took the same time, so I guess the middleware code is rescaling all the images to a particular resolution before doing inference. Can you please let me know.
Also, is the default model based on resnet-50 or resnet-152?
I would like to experiment with different models. Is it possible to download models based on different resnet backbone and mobilenet backbone?
Thanks!

Image normalization

I understand your code is based Stanislas Bertrand's but please take a look at the following line in the preprocessor:

im_tensor[0, :, :, i] = (img[:, :, 2 - i] / pixel_scale - pixel_means[2 - i]) / pixel_stds[2 - i]

Under low light conditions this would result in same image contrast as with bright images as you remove the average image intensity and divide by the average fluctuations in the image.

However both Stansilas and you define pixel means to be 0 and stds to be 1 and you don't compute them for each image.
Basically your code subtracts 0 and divides each pixel by 1 which is unnecessary.
If people want to use retinaface on dark images it might be useful to provide option to compute means with np.mean(image) and np.std(image).

Simplified preprocessor:
def preprocess_image(img, allow_upscaling):
scales = [1024, 1980]
img, img_scale = resize_image(img, scales, allow_upscaling)
img = img.astype(np.float32)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_tensor = np.expand_dims(img_rgb, axis=0)
return img_tensor, img.shape[0:2], img_scale

Unfortunately most of computation time is used in the tensor computation and this changes has little impact on performance.

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'

Doubt: Inside the Preprocessfunction , not changing the dimension to (N,C,H,W) ?

Hey , seregil,
In the preprocess_image function ,you described changing the image dim from (N,H,W,C) to (N,C,H,W) , but the below code only reversing the channels only (FROM BGR to RGB)

`# Make image scaling + BGR2RGB conversion + transpose (N,H,W,C) to (N,C,H,W)

for i in range(3):
    im_tensor[0, :, :, i] = (img[:, :, 2 - i] / pixel_scale - pixel_means[2 - i]) / pixel_stds[2 - i]`

Is it a mistake or written purposely ?
Kindly correct me if i am wrong.

Performance

I assume once a face detector model is trained its difficult to adapt it to a different input tensor size.

Retinaface image scales input images to 1024...1980 size which likely is useful to detect any face in a large image.
However Retianface is also accurate when the face is obstructed by facemask.
Using your deepface repository and measuring performance for all the provided face detectors I get something like this:

Retinaface: 172-229ms works with glasses, obstruction and facemask
OpenCV: 40-50ms does not work with glasses or obstructions
SSD: 19-30ms works with glasses and obstruction, but not with facemask
MTCNN: 945-3125ms works with glasses and obstruction, but not with facemask
DLIB: 346-350ms works with glasses, but not with obstruction or facemask

As you can see retinaface is not the fastest but most reliable detector, in your series of implementations.
If there is option to use smaller input tensor or a different tensor platform, like suggested by other user (TensorRT), it would be worth the effort for real-time applications.

detect_faces returns int64 or something

I've been stuck on this bug in my code for a few hours now.

This is my current code:
(RetinaFace was installed by a DeepFace installation.)

detectedFacesDict = RetinaFace.detect_faces(fileName)
print(detectedFacesDict)
print(detectedFacesDict.keys())
print(type(detectedFacesDict))
print(detectedFacesDict.__class__)
return json.dumps(detectedFacesDict)

The output:

{'face_1': {'score': 0.998822033405304, 'facial_area': [285, 77, 536, 416], 'landmarks': {'right_eye': [369.45465, 200.24005], 'left_eye': [484.59116, 217.69958], 'nose': [427.13742, 286.44733], 'mouth_right': [361.27505, 326.2066], 'mouth_left': [456.24017, 341.10242]}}}

dict_keys(['face_1'])

<class 'dict'>

<class 'dict'>

TypeError: Object of type int64 is not JSON serializable

Stack of the error:
image

Now I'm not quite sure on where to post this error, since it has clearly been produced by the json.dumps() method, but I've never heard of any issues related with the function.
Also, pasting this in the python command line (interpreted by the same venv) works just fine and outputs, as expected, a string with the fine json data in it.
I've also changed the variable name from detectedFaces to detectedFacesDict, which did not change anything.

Am I missing something obvious?
Thanks

Using GPU; tensorflow-gpu deprecated

Hello,

The tensorflow-gpu dependency is out-dated and cannot be installed as easily, thereby making the use of a GPU difficult. Sorry for the trouble, but is there a way this can be more streamlined and up-to-date (one of the major advantages of the retinaface package is its ease of use and installation)?

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]

face bounding box info in RetinaFace.extract_faces(fname, threshold=0.9, model = retina_model, align = True)

Is the bounding box info also available in the result of RetinaFace.extract_faces? Need bounding box to combine and visualize both face detection(RetinaFace.detect_faces) and face analysis (RetinaFace.extract_faces, DeepFace.analyze) result. But it seems that the face_id returned from RetinaFace.detect_faces can be different order from the one from RetinaFace.extract_faces. This will mess up the face_id linkage between the visual of the detected face and the emotion/race analysis from RetinaFace.extract_faces and DeepFace.analyze. Thanks

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

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)

from retinaface import RetinaFace

ConnectionError: HTTPSConnectionPool(host='drive.google.com', port=443): Max retries exceeded with url: /uc?id=1K3Eq2k1b9dpKkucZjPAiCCnNzfCMosK4 (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fa088499f28>: Failed to establish a new connection: [Errno 101] Network is unreachable',))

Question regarding verify function

Hi,

I use RetinaFace for face detections. So,, I have the face coordinates in a particular frame. Now, I want to use Verify() function to see if it matches the other stored face. I have the face coordinates from both the frames - meaning to say that I don't want the verify function to detect the face again but I only want the function to verify if both the faces are matching or not. How can I achieve it? I am trying to do this to increase the speed of the verify function call. Can you please suggest.

Thanks!

Wrong alignment direction issue when "align" set to True with DeepFace detectface using Retinaface detector

Dear Serengil,
We're encounter an issue according to align parameters in DeepFace detectface function, and we're using RetinaFace to achive the accuracy.

Here how we call the detectface function from DeepFace:

def face_detect(image):
    face = DeepFace.detectFace(img_path=image, detector_backend='retinaface', align=True)
    return face

and call it by this codes:

def detection(path):
    print (deepface.DeepFace)
    image = Image.open(path)
    image.show()
    face = DeepFace.detectFace(img_path=np.array(image),detector_backend='retinaface',align=True)
    imface = Image.fromarray((face * 255).astype(np.uint8))
    imface.show()

we do small experiment with rotated potrait (90 degree both clockwise and counter clockwise). We expect when align set to true, the face will be aligned. When we test both clockwise and counter clockwise, the output just rotating to other side (180 degree).
here is the sample image we get from Google search randomly and we rotate it in PS:
1st:
counter90
2nd
clockwise90

TensorRT implementation

Can you speed it up with TensorRT ? May be with fp16 or int8 optimizations ?
It can be 4-5 times faster.

Face Alignment - 1st, Extraction - 2nd

My challenge with RetinaFace is that I haven't found a way yet how to first detect the face, second, align the image based on the eyes line, and third, only after that to extract the image and export the face as an separate image file.

Only in this way it can be avoided the data lost (black areas, as a result of extraction first following by alignment).

Thanks you for your project. It's awesome and it's pretty straight forward.

Train data

What was used as the training dataset to train the model?

How to ensure that model weights are only downloaded once

I was using your library to detect all the faces in my dataset.
When iterating through the folder,I recieved the following stack trace

File "/usr/local/lib/python3.7/dist-packages/retinaface/RetinaFace.py", line 68, in detect_faces
    model = build_model()
  File "/usr/local/lib/python3.7/dist-packages/retinaface/RetinaFace.py", line 34, in build_model
    retinaface_model.build_model(),
  File "/usr/local/lib/python3.7/dist-packages/retinaface/model/retinaface_model.py", line 678, in build_model
    model = load_weights(model)
  File "/usr/local/lib/python3.7/dist-packages/retinaface/model/retinaface_model.py", line 31, in load_weights
    os.mkdir(home+"/.deepface/weights")
FileExistsError: [Errno 17] File exists: '/root/.deepface/weight

I am assuming that when you call

resp = RetinaFace.detect_faces("img1.jpg") 

it causes the weights to be downloaded,however when the next image comes it will again try to create the directories and try downloading the file.Since it already exists,the error is thrown.

Also,Rather than calling load_model multiple times,is it possible to just call it once.

Since the weights were only downloaded once,is there a way to ensure these are not downloaded again for the next batch of images?
Thanks

Use Case and High-Level Description

Is there any document that describe the output of model that human can read? Or please help me a method to understand the output. I found one document on the internet, but I am not sure whether it is correct or not for model in this repo: https://docs.openvino.ai/2021.2/omz_models_public_retinaface_resnet50_retinaface_resnet50.html

My target is run this model in mobile devices. I can covert and get the net_out. I would like to parse net_out to get the score, facial_area, landmarks. I am not familiar with Python so I don't understand clearly what it does to parse net_out in detect_faces function.

Performance

Does the RetinaFace implementation of this repo have the same performance as the original insightface implementation? It seems this Retina implementation performs even worse than MTCNN.

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.

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.

detect_faces(image) return ndarray

detect_faces(image) return ndarray instead of dict like that
{ "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] } } }

That is happend at

return np.zeros( (0,5) ), landmarks

What is mean?

Use of GPU

Hi!
I wrote the following statement in the Python code for the use of GPU

os.environ["CUDA_VISIBLE_DEVICES"]="0"
resp = RetinaFace.detect_faces(image)

I can ensure that the environment running GPU is normal, but the program still can't call GPU.

(Not issue) need some help

Hi! I need some help about face extraction. Currently i use installed from pip retinaface and use command provided in manual with some my modification:

resp = RetinaFace.detect_faces(frame)
faces = RetinaFace.extract_faces(img_path = frame, align = True)

i=1
for face in faces:
  filename = dir + 'face_'+str(i)+'.png'
  cv2.imwrite(filename, face)
  im_cv = cv2.imread(filename)
  im_rgb = cv2.cvtColor(im_cv, cv2.COLOR_BGR2RGB)
  cv2.imwrite(filename, im_rgb)
  i+=1

Question - can i extract and crop faces without some face parts, like without mouth? Something like that.
I'm new in python, if someone can help me or provide code example it will be really great! Thanks!

IMG_20220110_122516

pip install retina-face

$ pip install retina-face
Requirement already satisfied: retina-face in c:\users\michaell\appdata\local\programs\python\python310\lib\site-packages\retina_face-0.0.9-py3.10.egg (0.0.9)
Collecting Pillow>=5.2.0
Using cached Pillow-9.0.0-cp310-cp310-win_amd64.whl (3.2 MB)
Collecting gdown>=3.10.1
Using cached gdown-4.2.0.tar.gz (13 kB)
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing metadata (pyproject.toml) ... done
Collecting numpy>=1.14.0
Using cached numpy-1.22.1-cp310-cp310-win_amd64.whl (14.7 MB)
Collecting opencv-python>=3.4.4
Using cached opencv_python-4.5.5.62-cp36-abi3-win_amd64.whl (35.4 MB)
Collecting retina-face
Downloading retina_face-0.0.8-py3-none-any.whl (15 kB)
Downloading retina_face-0.0.7-py3-none-any.whl (15 kB)
Downloading retina_face-0.0.6-py3-none-any.whl (15 kB)
Downloading retina_face-0.0.5-py3-none-any.whl (14 kB)
Downloading retina_face-0.0.4-py3-none-any.whl (14 kB)
Downloading retina_face-0.0.3-py3-none-any.whl (4.3 kB)
Downloading retina_face-0.0.2-py3-none-any.whl (4.1 kB)
Downloading retina_face-0.0.1-py3-none-any.whl (3.9 kB)
ERROR: Cannot install retina-face==0.0.1, retina-face==0.0.2, retina-face==0.0.3, retina-face==0.0.4, retina-face==0.0.5, retina-face==0.0.6, retina-face==0.0.7, retina-face==0.0.8 and retina-face==0.0.9 because these package versions have conflicting dependencies.

The conflict is caused by:
retina-face 0.0.9 depends on tensorflow>=1.9.0
retina-face 0.0.8 depends on tensorflow>=1.9.0
retina-face 0.0.7 depends on tensorflow>=1.9.0
retina-face 0.0.6 depends on tensorflow>=1.9.0
retina-face 0.0.5 depends on tensorflow>=1.9.0
retina-face 0.0.4 depends on tensorflow>=1.9.0
retina-face 0.0.3 depends on tensorflow>=1.9.0
retina-face 0.0.2 depends on tensorflow>=1.9.0
retina-face 0.0.1 depends on tensorflow>=1.9.0

To fix this you could try to:

  1. loosen the range of package versions you've specified
  2. remove package versions to allow pip attempt to solve the dependency conflict

ERROR: ResolutionImpossible: for help visit https://pip.pypa.io/en/latest/user_guide/#fixing-conflicting-dependencies

Add URL Input like Deepface

First of all, this is such a great repo, the examples are well written and easy to read. I just have a small caveat for input.

I am working on a snippet using both retinaface and deepface, and I'm having some consistency issues with the input. I was wondering if we could add URL input to retinaface the same way it's implemented in deepface? I copied the snippet in deepface and changed load_image to get_image to stay consistent with retinaface. If this doesn't work, I'd love for url input to be added to retinaface soon.

import base64
import PIL.Image
import requests

def loadBase64Img(uri):
    encoded_data = uri.split(",")[1]
    nparr = np.fromstring(base64.b64decode(encoded_data), np.uint8)
    img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    return img

def get_image(img):
    exact_image = False
    base64_img = False
    url_img = False

    if type(img).__module__ == np.__name__:
        exact_image = True

    elif img.startswith("data:image/"):
        base64_img = True

    elif img.startswith("http"):
        url_img = True

    # ---------------------------

    if base64_img is True:
        img = loadBase64Img(img)

    elif url_img is True:
        img = np.array(PIL.Image.open(requests.get(img, stream=True, timeout=60).raw).convert("RGB"))

    elif exact_image is not True:  # image path passed as input
        if os.path.isfile(img) is not True:
            raise ValueError(f"Confirm that {img} exists")

        img = cv2.imread(img)

    return img

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 ?

Drawing a Bounding Box Around the Detected Face

I just need som information on what the function RetinaFace.detect_faces return.

{'face_1': {'score': 0.9993057250976562,
  'facial_area': [751, 294, 765, 312],
  'landmarks': {'right_eye': [757.8388, 301.72882],
   'left_eye': [763.83203, 301.46323],
   'nose': [762.1515, 305.3767],
   'mouth_right': [758.4781, 308.56125],
   'mouth_left': [763.1256, 308.4889]}}}

So if my image is 512x1024 (heightxwidth), what does these numbers (coordinates?) represent?

And my target is to draw a box around that detection, say using cv2.rectangle (either need top-left and bottom-right coordinates, or top-left and height and width), how would I do so? I tried playing around with the facial area key, but couldn't get the box around the face.

Also, landmarks is [757.8388, 301.72882], if this is a coordinate, 757 is already out of my image canvas (512)?

Just some basic doubts, thanks :)

get image from raw inputs

Hi, guys
can you add a option in the face detection methods to allow read from raw image instead of your image path?

get_image

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.

rotated faces

In my experience, retinaface cannot precisely locate the 5 landmarks on rotated faces, especially for 90 degrees and more rotation. How to solve that problem without rotating the input image? Or can we find that the face is rotated thats why the landmarks are not good?
Obama
obm
obmR

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'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.