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

numpy throwing error in batch mode when some images do not contain a face

Open markwillowtree opened this issue 2 months ago • 5 comments

venv/lib/python3.12/site-packages/facenet_pytorch/models/mtcnn.py", line 444, in select_boxes selected_boxes = np.array(selected_boxes) ^^^^^^^^^^^^^^^^^^^^^^^^ ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (316,) + inhomogeneous part.

I got this error message from this code

    mtcnn = MTCNN(112, device="cuda:0")

    container = av.open(video_path)
    frames = list(container.decode(video=0))
    images = [frame.to_image() for frame in frames]

    mtcnn(images)

It appears to be similar to the one described in this resolved issue:

https://github.com/timesler/facenet-pytorch/issues/206

I made the following changes to lines 444-446 in the file 'models/mtcnn.py'. These modifications ensure that when there are no faces detected in an image, the function returns None.

        if batch_mode:
            selected_boxes = np.array(selected_boxes, dtype=object)
            selected_probs = np.array(selected_probs, dtype=object)
            selected_points = np.array(selected_points, dtype=object)

However, I'm uncertain if this fix might have adverse effects elsewhere in the codebase as I'm not familiar with it.

markwillowtree avatar Apr 24 '24 23:04 markwillowtree