Snapchat Filters: How Do They Work?
Tags: augmentedreality, snapchat, computervision
Published: 2018-01-31 02:53:18

---
title: "Snapchat Filters: How Do They Work?"
published: true
description: Here’s a breakdown of the engineering behind the augmented-reality technology that changed the way we send selfies.
tags: augmented reality, snapchat, computer vision,
cover_image: "https://thepracticaldev.s3.amazonaws.com/i/20jpnp1z1wmu08y1ke3s.PNG"
---


---

About five times a day, I, along with 180 million daily users, open Snapchat to share small moments of my life with family and friends. (Truth be told, I almost always send videos of my dogs.)

<a href="http://www.instagram.com/beeandclara"><img src ="https://thepracticaldev.s3.amazonaws.com/i/yomffh5l6e4l5gyhamru.jpg"></a>

A few times a week, I browse through Snapchat’s lenses (what we know as Snapchat filters) to see if there’s anything new.

<img src="https://thepracticaldev.s3.amazonaws.com/i/5zeziturle4rt1zguqlo.jpg">

I’m a big fan of Snapchat, not only because I’ve limited my Facebook and Instagram usage, but also because I grew up as an admirer of photo technology. Snapchat’s ability to entertain by swapping faces with a friend or making animated rainbows fall out of your mouth built on that longtime admiration and intrigued me.

<img src="https://thepracticaldev.s3.amazonaws.com/i/2ny8rijek7dyu02maxee.jpg">

I started using Snapchat in 2016, I’ve wondered how the augmented reality technology behind their filters worked. Since I needed an idea for my Fullstack Academy stackathon project, it was my opportunity to dive in deep to understand what was going on behind the curtain.
The technology came from Looskery, a Ukranian tech company acquired by Snapchat in 2015.

Before diving into Snapchat’s technology, let’s take a 30,000-foot view of the universe that is Computer Vision — which is how computers receive data from your camera to identify objects.
<h4><b>What is Computer Vision?</b></h4>
<img src="https://thepracticaldev.s3.amazonaws.com/i/7rfpqy8zm471lpumlp5q.png">
<strong><a href="https://en.wikipedia.org/wiki/Computer_vision">Computer vision</a></strong> is an interdisciplinary field that addresses how computers translate the large amount of data[deals with how computers can be made for gaining high-level understanding] from digital images or videos. From the perspective of engineering, it seeks to automate tasks that the human visual system typically does.

Computer vision is the reason that:
<ul>
<li>Facebook can tag your friends in photos;</li>
<li>you can deposit a check with your phone;</li>
<li>self-driving cars can avoid hitting other cars or trees; and</li>
<li>you can send snaps of your face with a dog nose.</li>
</ul>
<h4><b>How can computers see faces?</b></h4>
When a computer looks at an image, it only sees 1s and 0s.

<img src="https://thepracticaldev.s3.amazonaws.com/i/wzl81ytnem6374w98r7s.png">

All human faces share similar properties:
<ul>
<li>the nose bridge is lighter than its sides</li>
<li>the eye region is darker than the upper-cheeks</li>
<li>the eye sockets are darker than the forehead</li>
</ul>

<img src="https://thepracticaldev.s3.amazonaws.com/i/8ckgbjufpb1irtxvqntg.png">

Using the <strong><a href="https://en.wikipedia.org/wiki/Viola%E2%80%93Jones_object_detection_framework">Viola-Jones algorithm</a></strong>, computers scan through the image data to detect contrasts between light and dark pixel areas. When finding enough matches for facial features in the pixel data in one particular part of an image, the computer can identify a face in the photo.

<img src="https://thepracticaldev.s3.amazonaws.com/i/dc9b4hik4lmswx9r0pfb.png">
The algorithm works well with frontal facing, but is unable to detect side faces.
<h4><b>How does Snapchat put a dog nose on my face?</b></h4>
Now let’s zoom back to learn how Snapchat uses that algorithm to map its filters to your face.

For a computer to put a dog nose on your face, it needs to do more complex calculations to locate your facial features. This is done by using an <strong><a href="https://link.springer.com/chapter/10.1007/978-3-642-54851-2_1">active shape model</a></strong>. Computers relying on this model draw on manual markings by humans that indicate the borders and facial features on thousands and thousands of images.
<img src="https://thepracticaldev.s3.amazonaws.com/i/p7f48agfq7srbnxy6bif.JPG">
The data from these faces are then applied to an image from your phone’s camera, scaling it to the areas where it has already detected your face.

<img src="https://thepracticaldev.s3.amazonaws.com/i/bq0jooirfe6emdm882cn.png">
The model isn’t perfect, but it has a template for what the top of your mouth looks like. It looks for the pattern in your image and adjusts accordingly to match it.

It also takes into account the locations of other points on your face to create a 3D mask, which can be used to scale, rotate, and move as the data from your camera comes in frame by frame.

<img src="https://thepracticaldev.s3.amazonaws.com/i/d5mplpxtxevmppmxh4vd.png">
This 3D mask can then be re-formed to adapt to your facial features as they move (e.g., eyes or chin), add accessories, and set rainbows to come alive when you open your mouth.
<img src="https://thepracticaldev.s3.amazonaws.com/i/cmx4ayywj5km0z2ccifn.JPG">
<h4><b>Now you’re an augmented reality expert.</b></h3>

So now you know how Snapchat works, you can adopt this topic as your go-to cocktail party conversation starter or impress your significant other’s parents at the next family dinner.

I hope this was interesting and enjoyed reading it as much as I enjoyed doing the research. Happy snapping.


---