Hi All,
For this tutorial, we're going to build an 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 CameraOnce 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.
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
.
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 codeNow 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
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
.
In the debugger in the Wia dashboard you should now see the Event appearing.
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
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.
Drag across the Detect Faces node under Services and connect it to the Event 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 NodeDrag 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 the code below:
Detected smile: ${input.body.isSmiling}
Click Update to save.
That's your whole Flow setup!
Run everything togetherNow 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 featuresThere are lots of other attributes you can try out. Click here to see the whole list.
That's all folks!
CL
Comments