Skip to content

Instantly share code, notes, and snippets.

@nickmitchko
Created November 14, 2016 02:10
Show Gist options
  • Select an option

  • Save nickmitchko/9cc0f8a8b559d256b9060567736f21e3 to your computer and use it in GitHub Desktop.

Select an option

Save nickmitchko/9cc0f8a8b559d256b9060567736f21e3 to your computer and use it in GitHub Desktop.

Revisions

  1. nickmitchko created this gist Nov 14, 2016.
    27 changes: 27 additions & 0 deletions imageIO
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    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