DroopyTersen
10/19/2016 - 7:22 PM

pi-server-find-employee-by-face.js

// THIS IS A NODEJS WEB SERVER RUNNING ON PI
var bodyParser = require("body-parser");
var express = require("express");
var createGuid = require('node-uuid').v4;

var app = express();
app.use(bodyParser.json()); //to handle POST body

// THESE DONT EXIST YET
var azureCam = require("azure-cam"); // bschlintz npm published module
var checkForFace = require("face-finder").checkForFace; //bschlintz npm published module. wrapper around openCV?
var getSkylineEmployee = require("./skyline-whoareyou") // thin wrapper module (not public) to promisify Fazio's cognitive services function 

azureCam.setup("<azuresubscription>", "<blobcontainer>");
// hookup middleware
azureCam.use(function(imgPath, next) {
    checkForFace(imagePath).then(hasFace => { 
        if (hasFace) next(imgPath)
        else throw new NoFaceException();
    });
})


app.post("/takepic", (req, res) => {
    var deviceId = req.body.deviceId;
    var trackingId = createGuid();

    azureCam.takePic(trackingId)
        .then(blobId => {
            return getSkylineEmployee(blobId, trackingId);
        })
        .then(employee => {
            res.send(employee);
        })
        .catch(e => {
            if (e instanceof NoFaceException) {
                res.status(400).send(e);
            } else {
                res.status(500).send(e);
            }
        })
});

// Start the web server on the specified port
app.listen(3000, function() {
    console.log("Server running on port 3000")
});

function NoFaceException(msg) {
    this.message = `Unable to find face in the image. ${msg}`.trim();
    this.name = "NoFaceException"
};