Created
November 14, 2016 02:10
-
-
Save nickmitchko/9cc0f8a8b559d256b9060567736f21e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dlib | |
import numpy | |
from skimage import io as ImageIO | |
from skimage.color import rgb2gray | |
from skimage.transform import resize | |
from scipy.ndimage import gaussian_filter | |
def extract_faces(filename, face_detector, face_size=100, padding=25, blur=True): | |
img = gaussian_filter(ImageIO.imread(filename), sigma=1) if blur else ImageIO.imread(filename) | |
# make sure the image is grayscale | |
detector = face_detector(img, 1) | |
# at a max we allocate at most 10 faces | |
if len(img.shape) == 3: | |
img = rgb2gray(img) | |
faces = numpy.zeros((10, face_size, face_size), dtype='float32') | |
# lets keep a counter so we know when to cut off our face array | |
counter = 0 | |
for i, j in enumerate(detector): | |
faces[i] = resize(img[j.top()-padding: | |
j.bottom()+padding, | |
j.left()-padding: | |
j.right()+padding], | |
output_shape=(face_size, face_size), | |
preserve_range=True) | |
counter += 1 | |
return numpy.asarray(faces[0:counter], dtype='float32') / 255 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment