Multiple Faces¶
So far we assumed that there is a single face in the image. However, the real power of pychubby
lies in its ability to handle multiple faces. Let’s start with the following image.
If more than one face is detected in an image pychubby
uses the LandmarkFaces
class rather than
LandmarkFace
. LandmarkFaces
is essentially a container containing LandmarkFace
instances for each face in the image.
import matplotlib.pyplot as plt
from pychubby.detect import LandmarkFace
img = plt.imread("path/to/the/image")
lfs = LandmarkFace.estimate(img) # lfs is an instance of LandmarkFaces
lfs.plot(show_landmarks=False, show_numbers=True)
Each face is assigned a unique integer (starting from 0). This ordering is very important since it allows us to specify which action to apply to which face.
In order to apply actions we use the metaaction Multiple
. It has two modes:
- Same action on each face
- Face specific actions
Same action¶
The first possibility is to apply exactly the same action to each face in the image. Below is an example of making all faces more chubby.
from pychubby.actions import Chubbify, Multiple
a_single = Chubbify(0.2)
a = Multiple(a_single)
new_lfs, df = a.perform(lfs)
new_lfs.plot(show_landmarks=False, show_numbers=False)
from pychubby.visualization import create_animation
ani = create_animation(df, img)
Different actions¶
Alternetively, we might want to apply different action to each face (or potentially no action). Below is an example of face specific actions.
from pychubby.actions import Chubbify, LinearTransform, Multiple, OpenEyes, Pipeline, Smile
a_0 = LinearTransform(scale_x=0.9, scale_y=0.9)
a_1 = Smile(0.14)
a_2 = None
a_3 = Pipeline([OpenEyes(0.05), LinearTransform(scale_x=1.02, scale_y=1.02), Chubbify(0.2)])
a = Multiple([a_0, a_1, a_2, a_3])
new_lfs, df = a.perform(lfs)
new_lfs.plot(show_landmarks=False, show_numbers=False)
from pychubby.visualization import create_animation
ani = create_animation(df, img)