• Stars
    star
    865
  • Rank 50,847 (Top 2 %)
  • Language
    Python
  • Created about 9 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

👼 Morph faces with Python, Numpy, Scipy

Face Morpher

Warp, average and morph human faces!
Scripts will automatically detect frontal faces and skip images if none is detected.

Built with Python, dlib, Numpy, Scipy, dlib.

Supported on Python 2.7, Python 3.6+
Tested on macOS Mojave and 64bit Linux (dockerized).

Requirements

Either:

Use as local command-line utility

$ git clone https://github.com/alyssaq/face_morpher

Morphing Faces

Morph from a source to destination image:

python facemorpher/morpher.py --src=<src_imgpath> --dest=<dest_imgpath> --plot

Morph through a series of images in a folder:

python facemorpher/morpher.py --images=<folder> --out_video=out.avi

All options listed in morpher.py (pasted below):

Morph from source to destination face or
Morph through all images in a folder

Usage:
    morpher.py (--src=<src_path> --dest=<dest_path> | --images=<folder>)
            [--width=<width>] [--height=<height>]
            [--num=<num_frames>] [--fps=<frames_per_second>]
            [--out_frames=<folder>] [--out_video=<filename>]
            [--plot] [--background=(black|transparent|average)]

Options:
    -h, --help              Show this screen.
    --src=<src_imgpath>     Filepath to source image (.jpg, .jpeg, .png)
    --dest=<dest_imgpath>   Filepath to destination image (.jpg, .jpeg, .png)
    --images=<folder>       Folderpath to images
    --width=<width>         Custom width of the images/video [default: 500]
    --height=<height>       Custom height of the images/video [default: 600]
    --num=<num_frames>      Number of morph frames [default: 20]
    --fps=<fps>             Number frames per second for the video [default: 10]
    --out_frames=<folder>   Folder path to save all image frames
    --out_video=<filename>  Filename to save a video
    --plot                  Flag to plot images to result.png [default: False]
    --background=<bg>       Background of images to be one of (black|transparent|average) [default: black]
    --version               Show version.

Averaging Faces

Average faces from all images in a folder:

python facemorpher/averager.py --images=<images_folder> --out=average.png

All options listed in averager.py (pasted below):

Face averager

Usage:
    averager.py --images=<images_folder> [--blur] [--plot]
            [--background=(black|transparent|average)]
            [--width=<width>] [--height=<height>]
            [--out=<filename>] [--destimg=<filename>]

Options:
    -h, --help             Show this screen.
    --images=<folder>      Folder to images (.jpg, .jpeg, .png)
    --blur                 Flag to blur edges of image [default: False]
    --width=<width>        Custom width of the images/video [default: 500]
    --height=<height>      Custom height of the images/video [default: 600]
    --out=<filename>       Filename to save the average face [default: result.png]
    --destimg=<filename>   Destination face image to overlay average face
    --plot                 Flag to display the average face [default: False]
    --background=<bg>      Background of image to be one of (black|transparent|average) [default: black]
    --version              Show version.

Steps (facemorpher folder)

1. Locator

  • Locates face points
  • For a different locator, return an array of (x, y) control face points

2. Aligner

  • Align faces by resizing, centering and cropping to given size

3. Warper

  • Given 2 images and its face points, warp one image to the other
  • Triangulates face points
  • Affine transforms each triangle with bilinear interpolation

4a. Morpher

  • Morph between 2 or more images

4b. Averager

  • Average faces from 2 or more images

Blender

Optional blending of warped image:

  • Weighted average
  • Alpha feathering
  • Poisson blend

Examples - Being John Malkovich

Create a morphing video between the 2 images:

> python facemorpher/morpher.py --src=alyssa.jpg --dest=john_malkovich.jpg
--out_video=out.avi

(out.avi played and recorded as gif)

gif

Save the frames to a folder:

> python facemorpher/morpher.py --src=alyssa.jpg --dest=john_malkovich.jpg
--out_frames=out_folder --num=30

Plot the frames:

> python facemorpher/morpher.py --src=alyssa.jpg --dest=john_malkovich.jpg
--num=12 --plot
plot

Average all face images in a folder:

85 images used

> python facemorpher/averager.py --images=images --blur --background=transparent
--width=220 --height=250
average\_faces

Use as pip library

$ pip install facemorpher

Examples

Additional options are exactly the same as the command line

import facemorpher

# Get a list of image paths in a folder
imgpaths = facemorpher.list_imgpaths('imagefolder')

# To morph, supply an array of face images:
facemorpher.morpher(imgpaths, plot=True)

# To average, supply an array of face images:
facemorpher.averager(['image1.png', 'image2.png'], plot=True)

Once pip installed, 2 binaries are also available as a command line utility:

$ facemorpher --src=<src_imgpath> --dest=<dest_imgpath> --plot
$ faceaverager --images=<images_folder> --plot

Try out in a docker container

Mount local folder to /images in docker container, run it and enter a bash session. --rm removes the container when you close it.

$ docker run -v  /Users/alyssa/Desktop/images:/images --name py3 --rm -it jjanzic/docker-python3-opencv bash

Once you're in the container, install facemorpher and try the examples listed above

root@0dad0912ebbe:/# pip install facemorpher
root@0dad0912ebbe:/# facemorpher --src=<img1> --dest=<img2> --plot

Documentation

http://alyssaq.github.io/face_morpher

Build & publish Docs

./scripts/publish_ghpages.sh

License

MIT

More Repositories

1

3Dreconstruction

3D reconstruction, sfm with Python3
Python
399
star
2

opencv

Find squares in an image with OpenCV C++
C++
239
star
3

hough_transform

Hough Transform implementation in Python
Python
122
star
4

usda-sqlite

USDA food files SR28 to SQLite
Python
115
star
5

reconstruction

3D reconstruction with openCV and SFM
C++
77
star
6

react-redux-table-example

React-Redux Router Table Sample App
JavaScript
50
star
7

stats-analysis

Engineering Statistics and Data Analysis
JavaScript
32
star
8

prequest

Promisified HTTP requests with `bluebird` and `request` modules
JavaScript
27
star
9

flask-restful-api-appengine

A restful API with flask for Google App Engine
Python
23
star
10

timeseries

Time series analytics with Python
Python
17
star
11

stasm_build

Mac/unix build scripts for stasm 4.1 with OpenCV 2 & 3
CMake
14
star
12

face-find-fun

Browser head-tracking and draws a party hat! - uses webrtc getUserMedia API, headtrackr and canvas
HTML
11
star
13

celery-flask-demo

Simple Flask app using celery with broker and backend
Python
10
star
14

python-data-science-intro

Intro to Python for Data Science for GA class
Jupyter Notebook
8
star
15

stasm

python wrapper for stasm - face points detector
C++
8
star
16

heap

Heap implementation in C++
C++
8
star
17

stl-complexities

C++ STL container complexities
8
star
18

MOOCS

MOOCS I love
7
star
19

alyssaq.github.io

Personal blog
CSS
5
star
20

esm-browser-example

ES modules in the browser
JavaScript
5
star
21

node-mongodb-api

Nodejs, MongoDB, Mongoose API boilerplate
JavaScript
4
star
22

hawkersg

Map markers and directions to hawker centres in Singapore
JavaScript
4
star
23

face_verifier

Verify whether a face is present in an image
Python
3
star
24

timezones-buddy

Convert between different timezones and unix timestamps
JavaScript
3
star
25

robotics_nanodegree_udacity_2019

Udacity's robotics nanodegree (2019)
CMake
3
star
26

kaggle

python notebooks for kaggle
Jupyter Notebook
3
star
27

egg

Egg math equation drawn with canvas
JavaScript
3
star
28

bottle-heroku-skeleton

Bottle skeleton with scientific packages for deployment on Heroku
Python
2
star
29

face-trackr-canvas

Face detection and canvas shape
JavaScript
2
star
30

cs231n-2017

Assignment solutions for cs231n 2017
Jupyter Notebook
2
star
31

financial-report

A payment report for rental property
JavaScript
2
star
32

alyssa.asia

My online CV - alyssa.asia site
JavaScript
1
star
33

backbone-basics

Simple backbonejs apps
JavaScript
1
star
34

appcache_sample

Sample offline app with appcache manifest
JavaScript
1
star
35

face_morpher_slides

Face Morpher Slides for FOSS Asia 2015
CSS
1
star
36

meetup_events

Find meetup events in your country
JavaScript
1
star
37

proxy

Go Proxy Server Example
Go
1
star
38

tornado-examples

Tornado examples showing async, coroutines, yield, periodic calls, etc
Python
1
star
39

facebook_api_user

Get user info from Facebook Graph API
JavaScript
1
star
40

threejs-jam

Three.js playground - ball bouncing
1
star
41

rainbow

RGB to hex visualizer, slider
JavaScript
1
star