Skip to content

Instantly share code, notes, and snippets.

@moshfiqur
Created June 22, 2018 10:22
Show Gist options
  • Save moshfiqur/3f388d10b7aed3a3c810f8c8d2f4aa07 to your computer and use it in GitHub Desktop.
Save moshfiqur/3f388d10b7aed3a3c810f8c8d2f4aa07 to your computer and use it in GitHub Desktop.
Cropped faces after detection in an image using face_detection module and save them.
import face_recognition
from PIL import Image
np_image = face_recognition.load_image_file("avengers2.jpg")
# face_locations returns a list of locations for each face
# detected in the picture, each list item contains a tuple
# of 4 items in the format of (top, right, bottom, left)
face_locations = face_recognition.face_locations(np_image)
image_obj = Image.open("avengers2.jpg")
seq = 0
for face_location in face_locations:
# Be careful in ordering top, right, bottom, left
top, right, bottom, left = face_location
cropped_image = image_obj.crop((left, top, right, bottom))
cropped_image.save('avengers2_cropped_'+str(seq)+'.jpg')
cropped_image.show()
seq += 1
@moshfiqur
Copy link
Author

The face recognition module: https://github.com/ageitgey/face_recognition

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment