serengil / deepface

A Lightweight Face Recognition and Facial Attribute Analysis (Age, Gender, Emotion and Race) Library for Python
https://www.youtube.com/watch?v=WnUVYQP4h44&list=PLsS_1RYmYQQFdWqxQggXHynP1rqaYXv_E&index=1
MIT License
14.31k stars 2.2k forks source link

Retinaface/MTCNN very slow start when calling "DeepFace.detectFace" method #308

Closed yvs997 closed 3 years ago

yvs997 commented 3 years ago

Hello Serengil, Thanks to develop deepface library which is very useful to face recognition tools.

I try to using your DeepFace.detectFace method since i want to store embedding record to other database and augmented detected face into several version to enhance the dataset.

However on my observation, i got a problem when calling a function detectFace with my own wrapper function:

def face_detector(image):
    latest_image = A.LongestMaxSize (max_size=1024, interpolation=cv2.INTER_AREA, always_apply=True, p=1)
    pipeline = A.Compose([latest_image])
    image = pipeline(image=np.array(image))['image']
    start_time = time.time()
    detected_face = DeepFace.detectFace(image,'retinaface',False)
    end_time = time.time()
    print("Time to extract face: ",end_time-start_time)
    resizer = A.Resize (122,122, interpolation=cv2.INTER_AREA,always_apply=True, p=1)
    augmented_image = image_conversion(detected_face)
    return augmented_image,(end_time-start_time)

when i try to simulate that call inside a loop, at the first iteration the processing time is about 10 sec: Time to extract face: 10.532280206680298 , but drastically reduced for the next iteration (this is the output recorded for picture number 2-5:

Time to extract face:  3.656264305114746
Time to extract face:  5.784324884414673
Time to extract face:  5.470204830169678
Time to extract face:  3.2954962253570557

seconds which i consider normal due the CPU. I want to know whether there is a solution for this time consumption issue, especially if i want to use only detectFace as an API call in the future?

serengil commented 3 years ago

Hello, that is totally normal because face detector model (e.g. mtcnn, retinaface) is building in the first iteration of the for loop. Then, it's going to use the pre-built face detector in the following iterations.

In the API perspective, similar to previous scenario, it will return fast if the model is already built. You may build the model in initialization of the API.