Skip to content

Instantly share code, notes, and snippets.

@raasoft
Created February 12, 2019 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raasoft/4a60272c422423a702d16c76d6aa56cd to your computer and use it in GitHub Desktop.
Save raasoft/4a60272c422423a702d16c76d6aa56cd to your computer and use it in GitHub Desktop.
import dlib
import Image
from skimage import io
import matplotlib.pyplot as plt
def detect_faces(image):
# Create a face detector
face_detector = dlib.get_frontal_face_detector()
# Run detector and get bounding boxes of the faces on image.
detected_faces = face_detector(image, 1)
face_frames = [(x.left(), x.top(),
x.right(), x.bottom()) for x in detected_faces]
return face_frames
# Load image
img_path = 'test.jpg'
image = io.imread(img_path)
# Detect faces
detected_faces = detect_faces(image)
# Crop faces and plot
for n, face_rect in enumerate(detected_faces):
face = Image.fromarray(image).crop(face_rect)
plt.subplot(1, len(detected_faces), n+1)
plt.axis('off')
plt.imshow(face)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment