Over a million developers have joined DZone.

Build Your Own Smile Detector with Wia, Amazon Recognition, and Raspberry Pi

DZone's Guide to

Build Your Own Smile Detector with Wia, Amazon Recognition, and Raspberry Pi

Read this article in order to view a tutorial on how to build your own smile detector.

· AI Zone ·
Free Resource

Did you know that 50- 80% of your enterprise business processes can be automated with AssistEdge?  Identify processes, deploy bots and scale effortlessly with AssistEdge.

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
- 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.

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.

Make sure P1 Camera is selected and press Enter.

When asked Would you like the camera interface to be enabled?, select <Yes>.

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 raspicamlibraries 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.

'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
    name: 'photo',
    file:  fs.createReadStream(__dirname + '/' + filename)

// Take a photo

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.

In the debugger in the Wia dashboard you should now see the Event appearing.

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.

Add a Detect Faces Node

Drag across the Detect Faces node under Services and connect it to the Event node.

Add a Run Function Node

Drag across a Function node under Logic and connect it to the Detect Faces node.

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.

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.

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 to see the whole list.

That's all, folks!

Consuming AI in byte sized applications is the best way to transform digitally. #BuiltOnAI, EdgeVerve’s business application, provides you with everything you need to plug & play AI into your enterprise.  Learn more.

amazon ,amazon rekognition ,iot cloud ,facial recognition ,raspberry pi ,artificial intelligence ,ai

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}