2

The function detect_faces() fails in JupiterLab:

image = Image.open(filename)
imageRGB = image.convert('RGB')
pixels = asarray(imageRGB)
detector = MTCNN()
results = detector.detect_faces(pixels)

mtcnn version 0.1.0

The error:

AbortedError: Operation received an exception:Status: 2, message: could not create a descriptor for a softmax forward propagation primitive, in file tensorflow/core/kernels/mkl/mkl_softmax_op.cc:306
[[node model/softmax/Softmax (defined at /home/rikkatti/anaconda3/envs/poi/lib/python3.9/site-packages/mtcnn/mtcnn.py:342) ]] [Op:__inference_predict_function_828]

Function call stack: predict_function

rikkatti
  • 452
  • 4
  • 10

1 Answers1

0

Try it this way using cv2

from mtcnn import MTCNN
import os
import cv2
detector = MTCNN()
dest_dir=r'C:\Temp\people\storage\cropped' # specify where to save the images
filename=r'C:\Temp\people\storage\34.png' # specify the file name full path
try:
    img=cv2.imread(filename) # filename must be full path to the image
    shape=img.shape # will cause an exception if image was not read properly
    data=detector.detect_faces(img)                
    if data ==[]:
        print ('no faces were detected for file ', filename)
    else:
        for i, faces  in enumerate(data):
            box= faces['box']
            if box != []:
                box[0]= 0 if box[0]<0 else box[0]
                box[1]= 0 if box[1]<0 else box[1]            
                cropped_img=img[box[1]: box[1]+box[3],box[0]: box[0]+ box[2]]               
                fname=os.path.split(filename)[1]
                index=fname.rfind('.')
                fileid=fname[:index]
                fext=fname[index:]
                fname=fileid + '-' +str(i) + fext                
                save_path=os.path.join(dest_dir,fname )                
                cv2.imwrite(save_path, cropped_img)
except:
    print(' an error occurred')

This will detect all faces in the image and store them as cropped images in the dest_dir. Tested it with an image with multiple faces and it works fine

Gerry P
  • 4,258
  • 2
  • 5
  • 15