# The Science Behind Generating Faces Algorithmically

When I was five years old I was terrified of the power sockets in my great aunt’s house. British power sockets use three pins, and some very old ones use round pins as opposed to rectangular ones. Those in my great aunt’s house looked a bit like faces. Since they were in a dark corridor, I found it very spooky.

Interpreting objects as faces is a psychological phenomenon called “Pareidolia,” and is perfectly normal. And it’s not just us — meaning humans — who do it. Some facial recognition software picks up these objects, as well. If your digital camera can pick out faces, it might be a fun experiment to create some fake ones to see if it can pick them out, too.

### Mona Lisa

Nearly four years ago, developer Roger Alsing published source code for an experiment he’d cobbled together. He wanted to see if he could “Paint a replica of the Mona Lisa using only 50 semi transparent polygons.”

A polygon is a closed shape, often — but not limited to being — a triangle. It’s filled in with a color, texture and shading. Many polygons are used to build up complex shapes (usually 3D) but they can be used in 2D also, as they were in the Mona Lisa experiment.

Alsing used a technique called “Genetic Algorithms,” which tries to solve problems by using “digital DNA” to generate a solution, compare it to the desired answer and, if it’s better than a previous answer, evolving a new one. The digital DNA is just a set of values used to generate the answer, and evolving means altering one or more of the values. There is also a fitness function that’s used to determine if the most recent answer is closer to the desired one. This approach is a well known method of trying to solve problems that aren’t easily solved in other, simpler ways.

Now developer and Author Philip McCarthy has used this approach to generate faces. His code starts with random polygons and then software tries to detect a face. His whole experiment is written in JavaScript, including the face detection software. He’s made it open source on GitHub so you can try it. There’s also a live demo version.

McCarthy’s approach requires intensive processing, so it isn’t suitable for generating faces in real time. But it certainly could be used to pre-generate a sea of faces in a crowd.