How to crop and save extracted faces from all the dataset?

Viewed 31

can someone help me? My dataset contains two folders: train and test (with other classes), I want to extract, crop, and save them to new folders with the same classes. and thanks in advance.

image_path = glob.glob('/content/drive/MyDrive/dataset_pose/data_pose/train/*.BMP')

def extract_face_from_image(image_path, required_size=(224, 224)):
# load image and detect faces
    image = cv2.imread(image_path)
    detector = MTCNN()
    faces = detector.detect_faces(image)

face_images = []

for face in faces:
# extract the bounding box from the requested face
  x1, y1, width, height = face['box']
  x2, y2 = x1 + width, y1 + height

# extract the face
  face_boundary = image[y1:y2, x1:x2]


  face_image = Image.fromarray(face_boundary)
  face_image = face_image.resize(required_size)
  face_array = asarray(face_image)
  face_images.append(face_array)

extracted_face = 
extract_face_from_image('/content/drive/MyDrive/dataset_pose/data_pose/train/')  
cv2.imwrite(os.path.join ("/content/drive/MyDrive/dataset_pose/data_pose/train/cropped_train/",str(face)),face_boundary)
0 Answers
Related