Build your own smile detector with Wia, Amazon Rekognition and Raspberry Pi
Tags: iot, raspberrypi, wia, facialrecognition
Published: 2018-04-12 14:16:53

---
title: Build your own smile detector with Wia, Amazon Rekognition and Raspberry Pi
published: true
description: For this tutorial, we're going to build a facial recognition camera that can detect if someone is smiling (or not).
tags: iot, raspberry pi, wia, facial recognition
---
Hi All,
For this tutorial, we're going to build a facial recognition camera that can detect if someone is smiling (or not). 

Here's what you will need:
- Raspberry Pi (any model 3, 3+, Zero, etc).
- Raspberry Pi Camera Module (we recommend [this](https://www.raspberrypi.org/products/camera-module-v2/) one).
- Micro SD card (we recommend at least 8Gb).
- Micro USB to USB cable.

We've already got lots of in depth guides showing you how to setup your Raspberry Pi from scratch. You can view those [here](https://developers.wia.io/docs/raspberry-pi).
  
## Setup the Camera
Once you've got everything up and going, it's time to start setting up the camera on your Raspberry Pi. When you are logged into your Raspberry Pi via SSH, run the command `sudo raspi-config`. Press the down arrow key until you reach `5 Interfacing Options` and press Enter.
  
![alt text](https://s3-eu-west-1.amazonaws.com/wia-flarum-bucket/2018-04-12/1523527924-901868-screen-shot-2018-04-12-at-103641.png)
  
Make sure `P1 Camera` is selected and press Enter.
  
![alt text](https://s3-eu-west-1.amazonaws.com/wia-flarum-bucket/2018-04-12/1523527954-3232-screen-shot-2018-04-12-at-103653.png)
  
When asked `Would you like the camera interface to be enabled?`, select `<Yes>`.
  
![alt text](https://s3-eu-west-1.amazonaws.com/wia-flarum-bucket/2018-04-12/1523527974-618439-screen-shot-2018-04-12-at-103742.png)
  
To exit, press the down arrow key until you reach `<Finish>` and press Enter.
  
## Install Node.js
Run the following 5 commands one by one:

```
cd ~
wget https://nodejs.org/dist/v4.3.2/node-v4.3.2-linux-$(uname -m).tar.gz 
tar -xvf node-v4.3.2-linux-$(uname -m).tar.gz
cd node-v4.3.2-linux-$(uname -m)
sudo cp -R * /usr/local/
```

To check Node.js is installed correctly, run `node -v`. It should return `v4.3.2`.
  
## Setup the Project
Go to your home directory by running the command `cd ~`. Create a new directory by running `mkdir wia-pi-camera`. Go into this directory by running the command `cd wia-pi-camera`.

Initialise the project by running `npm init`. Hit Enter at each of the prompts. Install the `wia` and `raspicam` libraries by running each of these commands:
```
npm install --save wia
npm install --save raspicam
```
  
## Write the code
Now we're going to write the code that will capture a photo and send it to Wia. 

Create a new file by running the command `touch run-camera.js`. Open the text editor by running `nano run-camera.js`. 

Copy and paste the code from below, replacing `your-device-secret-key` with your device's secret key. If you haven't already set one up, you can do so in the Wia dashboard [here](https://dashboard.wia.io/).

```
'use strict';

var wia = require('wia')('your-device-secret-key');
var fs = require('fs');
var RaspiCam = require("raspicam");

// Setup the camera
var camera = new RaspiCam({
  mode: 'photo',
  output: __dirname + '/photo.jpg',
  encoding: 'jpg'
});

// Listen for the "start" event triggered when the start method has been successfully initiated
camera.on("start", function(){
  console.log("Starting to take photo.");
});

// Listen for the "read" event triggered when each new photo/video is saved
camera.on("read", function(err, timestamp, filename){
  console.log("New photo created.", timestamp, filename);

  // Publish the photo to Wia
  wia.events.publish({
    name: 'photo',
    file:  fs.createReadStream(__dirname + '/' + filename)
  });
});

// Take a photo
camera.start();
```
  
Press `CTRL+O` (that's the letter o, not the number) to save the code, followed by `CTRL+X` to exit Nano. Back in your terminal, run the code by running the command `node run-camera.js`. 

![alt text](https://s3-eu-west-1.amazonaws.com/wia-flarum-bucket/2018-04-12/1523528587-325138-screen-shot-2018-04-12-at-112247.png)
  
In the debugger in the Wia dashboard you should now see the Event appearing.
  
![alt text](https://s3-eu-west-1.amazonaws.com/wia-flarum-bucket/2018-04-12/1523528622-426028-screen-shot-2018-04-12-at-112310.png)
  
## Build the Flow
In the Wia dashboard, we're now going to build the Flow that will detect happy faces. In the left side menu, click on Flows, then create a Flow called 'Detect Happy Faces'.

We're going to do the 4 following steps:
- Add a Trigger Node
- Add a Detect Faces Node
- Add a Run Function Node
- Add an Email Node

### Add a Trigger Node
Drag across an Event node from the Triggers section. Hover over the node and click the gear icon to open the settings. Enter `photo` as the Event Name and click Update. Now add the devices you would like this trigger to apply to.

![alt text](https://s3-eu-west-1.amazonaws.com/wia-flarum-bucket/2018-04-12/1523528878-925761-screen-shot-2018-04-12-at-112646.png)

### Add a Detect Faces Node
Drag across the Detect Faces node under Services and connect it to the Event node.

![alt text](https://s3-eu-west-1.amazonaws.com/wia-flarum-bucket/2018-04-12/1523528950-212324-screen-shot-2018-04-12-at-112810.png)

### Add a Run Function Node
Drag across a Function node under Logic and connect it to the Detect Faces node.
  
![alt text](https://s3-eu-west-1.amazonaws.com/wia-flarum-bucket/2018-04-12/1523529004-671765-screen-shot-2018-04-12-at-112947.png)
  
Hover over the Run Function node and click on the gear icon to open the settings.

Copy and paste the following code block to detect smiles. Then click Update.

```
if (input.body.faceDetails && input.body.faceDetails.length > 0) {
    output.body.isSmiling = input.body.faceDetails[0].smile.value;
} else {
    output.body.isSmiling = false;
}
```

## Add an Email Node
Drag over an Email node from under Actions and connect it to the Run Function node.

![alt text](https://s3-eu-west-1.amazonaws.com/wia-flarum-bucket/2018-04-12/1523529464-891185-screen-shot-2018-04-12-at-113550.png)

Hover over the Email node and click on the gear to open the settings.

Enter your To Address, Subject Line and in the Body, copy and paste `Detected smile: ${input.body.isSmiling}`. Click Update to save.

![alt text](https://s3-eu-west-1.amazonaws.com/wia-flarum-bucket/2018-04-12/1523529571-397273-screen-shot-2018-04-12-at-113920.png)

That's your whole Flow setup!
  
## Run everything together
Now go back to your Raspberry Pi and run the command `node run-camera.js` again to see your Flow running. Check your email inbox for the message.
  
## (Optional) Try out other facial features
There are lots of other attributes you can try out. [Click here](https://developers.wia.io/docs/integrations-aws-rekognition) to see the whole list.
  
That's all folks!

CL