element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi Projects
  • Products
  • Raspberry Pi
  • Raspberry Pi Projects
  • More
  • Cancel
Raspberry Pi Projects
Blog Build Your Own Smile Detector with Wia, Amazon Rekognition and Raspberry Pi
  • Blog
  • Documents
  • Events
  • Polls
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi Projects to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: anpivey
  • Date Created: 3 Aug 2018 11:13 AM Date Created
  • Views 1470 views
  • Likes 4 likes
  • Comments 3 comments
  • tutorial
  • aws
  • raspberri pi
  • facial_recognition
  • iot
  • smile
  • raspberry_pi_projects
  • wia
Related
Recommended

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

anpivey
anpivey
3 Aug 2018

image

 

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

 

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.

 

image

 

Make sure P1 Camera is selected and press Enter.

 

image

 

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

 

image

 

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

  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.

image

 

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

 

image

 

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.

image

ADD A DETECT FACES NODE

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

image

ADD A RUN FUNCTION NODE

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

 

image

 

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.

image

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.

image

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!

  • Sign in to reply

Top Comments

  • dixonselvan
    dixonselvan over 7 years ago +2
    Thank you anpivey Nice documentation.
  • anpivey
    anpivey over 7 years ago in reply to dixonselvan +2
    Glad you like it! I will be posting many more over the next few weeks!
  • dixonselvan
    dixonselvan over 7 years ago in reply to anpivey +1
    Meanwhile let me try if I can get my hands on using your tutorial.
  • dixonselvan
    dixonselvan over 7 years ago in reply to anpivey

    Meanwhile let me try if I can get my hands on using your tutorial.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • anpivey
    anpivey over 7 years ago in reply to dixonselvan

    Glad you like it! I will be posting many more over the next few weeks!

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • dixonselvan
    dixonselvan over 7 years ago

    Thank you anpivey

     

    Nice documentation.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube