I want to crop and save images from all the dataset which contains multi classes. but there is no output. and thank you.
def detect_face(img): detector = MTCNN() faces = detector.detect_faces(image) return faces dataset = '/content/drive/MyDrive/data_LFW/' for img in glob.glob(dataset+'/*.*'): var_img = cv2.imread(img) face = detect_face(var_img) if (len(face) == 0): continue for(ex, ey, ew, eh) in face: crop_image = var_img[ey:ey+eh, ex:ex+ew] cv2.imwrite(os.path.join("/content/drive/MyDrive/data_crop",str(img)) ,crop_image)
Advertisement
Answer
Assuming everything else are correct (since I don’t have your dataset and detector code), you may want to try to correct couple indentations to see if you can get some different results:
if (len(face) == 0): continue #correct the indentation and try again
and if you want to save every detection, correct the cv2.imwrite indentation as well. Since you have two sub folders under the dataset, then you need to change glob statement as shown below to visit all sub folders:
for img in glob.glob(dataset+'**/*.*'): var_img = cv2.imread(img) face = detect_face(var_img) if (len(face) == 0): continue for(ex, ey, ew, eh) in face: crop_image = var_img[ey:ey+eh, ex:ex+ew] cv2.imwrite(os.path.join("/content/drive/MyDrive/data_crop",str(img)),crop_image)