(Silly) masks and hats
the script the world didn't know it needed,
— Leon Eckert (@eckert_leon) June 28, 2017
but does. pic.twitter.com/bx46lcuHKl
(someone made even a twitter bot out of it!!)
I have a think for masks, wigs, hats anything you put on heads for fun both digitally and IRL. That's where this project and previous similar ones (e.g. Anonymizme or Facemash Workshop) are coming from.
Ispired by this article in The New Inquiry I wrote two scripts:
create_mask.py
and apply_mask.py
.
Using these allows to create masks from photos - that means the script cuts on faces and 'cuts' eye-holes (this part of the script is taken from the brilliant Sam Lavigne - and apply those, or self drawn masks to a photo (or folder of photos or even a video).
The algorithm uses dlib face detection in both source and target image and then morphs the mask so that it fits the face. With self-drawn masks and hats, the trick is that self-drawn masks always come with a reference face photo. This is how the code works:
1. Detect faces.
2. Calculate a Transformation matrix describing the difference.
3. Transform one face using the transformation matrix to fit onto the other.
Obviously, (silly) masks should look whatever they want, not necessarily face-like. That’s why this script works with mask files consisting of two images: a ‘back’ (.jpg) image with a reference face and a ‘front’ (transparent .png) image with the mask.
The idea then is to use the reference image to calculate whatever transformation matrix is needed for a given destination face, but to use it to morph the mask instead.
That way, masks don’t have to resemble faces or be of any specific size (for bigger masks, just add margin to the reference image, and draw whatever you feel like onto the mask png).
More info (and the code!) can be found in project’s github repository. Let me know if you have any questions!