facenet-pytorch icon indicating copy to clipboard operation
facenet-pytorch copied to clipboard

How to handle no face in image

Open Joaquin-aliaga opened this issue 4 years ago • 2 comments

Hi, I'm developing a face verification system, so I'm using mtcnn to detect faces and then do the verification step.

The code I have is something like:

face_detect, prob = mtcnn.detect(img)
if (face_detect is not None):
    face_cropped = mtcnn(img)
   ...
else:
   return None

However, I'm wondering if there is a way to handle the "no face in image" case without use mtcnn twice, because I assume mtcnn.detect() and mtcnn() use almost the same code.

If I just use face_cropped = mtcnn(img) and img has no face, I get an error.

line 408, in select_boxes
    if len(boxes) == 0:
TypeError: len() of unsized object

Thanks in advance

Joaquin-aliaga avatar Jan 03 '21 02:01 Joaquin-aliaga

@Joaquin-aliaga I've already find a solution for not using mtccn twice and post here if anyone faces the same issue. Since mtcnn.detect() returns bounding boxes and probs, just use:

bbx, prob = mtcnn.detect(img)
if (bbx is not None):
    face_cropped = mtcnn.extract(img,bbx,None)
   ...
else:
   return None

In that way you don't have to pass your image twice through the CNN.

However it would be nice if mtcnn() returns None when no face was detected.

Joaquin-aliaga avatar Jan 04 '21 00:01 Joaquin-aliaga

The problem has been solved.

if type(boxes) == numpy.ndarray:
    for bbox in boxes:

justDoIt1314 avatar May 07 '21 03:05 justDoIt1314