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

MTCNN is failing to save cropped faces, but throws no errors.

Open ctivanovich opened this issue 4 years ago • 2 comments

for i, (img, label) in enumerate(loader):
    parts = label[0].split('/')
    user_path = os.path.join(CROPPED_DIR, *parts[:-1])
    if not os.path.exists(user_path):
        os.makedirs(os.path.join(user_path))
    mtcnn(img[0], save_path =  os.path.join(user_path, parts[-1]))
    print('\rImage {} of {}'.format(i + 1, len(loader)), end='')

where a save path is of form './cropped/<gender>/<user_id>/<filename>.jpg'.

It was working without issue until the last 2 updates of the library, and I am not able to find anything wrong with my code. I've looked into the way MTCNN module does saving, and I couldn't see any obvious issues. My batch loader is set to 1, and I've confirmed the path is fine and dandy, and the os.makedir call correctly builds the directories. The images are of type PIL.Image.Image.

ctivanovich avatar Sep 11 '20 08:09 ctivanovich

The save function used by MTCNN via the detect_face module is thus:

def save_img(img, path):
    if isinstance(img, np.ndarray):
        cv2.imwrite(path, cv2.cvtColor(img, cv2.COLOR_RGB2BGR))
    else:
        img.save(path)

I have confirmed I am passing in save-able PIL images, and that if this function is invoked directly, it saves without issue. So, there may be an issue with how this function gets called during the face detection and cropping process within MTCNN.

ctivanovich avatar Sep 16 '20 08:09 ctivanovich

I have not been able to reproduce this issue. Can you provide a complete, self-contained example?

timesler avatar Oct 01 '20 05:10 timesler