-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase.js
102 lines (95 loc) · 4.31 KB
/
firebase.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* This program is running on the Local Raspberry Pi, The other files in this repository are
running on the cloud in Lambda */
var request = require('request');
var firebase = require('firebase');
const spawn = require('child_process').spawn;
const fs = require('fs');
var photoNum = 0;
firebase.initializeApp({
databaseURL: '<firebaseURLhere>',
serviceAccount: './<firebaseSecurityProfile>.json'
});
/* Local Database Access */
var ref = firebase.database().ref("Alexa");
var name = './photo/image.jpg';
/* The Raspberry Pi is always listening to the database to see if voice commands to Alexa are issued */
ref.on("child_changed", function(snap) {
console.log("initial data loaded!", snap.key +":",snap.val());
if(snap.val() == 'callFacialRecog'){ //After the Alexa application is triggered by onLaunch or DoorIntent
const Raspistill = spawn('raspistill',['-e','jpg','-o','./photo/image.jpg']); //Snaps a picture of whatever is in front of the door
Raspistill.on('close', (code) => {
fs.readFile(name, function(err, data) {
var base64data = new Buffer(data).toString('base64');
var options = {
url: 'https://api.kairos.com/recognize',
headers:{
'Content-Type': 'application/json',
'app_id': '<KairosAppID>',
'app_key': '<KairosAppKey>'
},
body: JSON.stringify({
image: base64data, //After picture is taken, image data is set on the facial recognition API
gallery_name: 'alexaTest'
})
};
request.post(options,function(error, response, body){
if(error){ //Case if request not properly made
ref.child("Read").set("Failed");
}
else if(JSON.parse(body)["Errors"]){ //Case if no face is detected in the door (aka no one is at the door)
ref.child("Read").set("noPerson");
}
else{
var status = JSON.parse(body)["images"][0]["transaction"]["status"];
if(status == "success"){
var name = 'doneRecog '+JSON.parse(body)["images"][0]["transaction"]["subject"]; //Sends recognized name with 'doneRecog' data to database when recognition is complete
ref.child("Read").set(name);
}
else if (status == "failure"){
ref.child("Read").set("Failed"); //Sends fail to recognize data to the DB on completed request
}
}
});
});
});
}
/* When Raspberry Pi reads that the Alexa wrote the facial traning indicator word to the database */
else if(snap.val()!=null && snap.val().includes('callFacialTrain')){
fs.readFile(name, function(err, data) {
var base64data = new Buffer(data).toString('base64');
// console.log(base64data);
var options = {
url: 'https://api.kairos.com/enroll',
headers:{
'Content-Type': 'application/json',
'app_id': '<KairosAppID>',
'app_key': '<KairosAppKey>'
},
body: JSON.stringify({
image: base64data, //Uses the previously taken picture of the person's face to train
subject_id: snap.val().substring(16),
gallery_name: 'alexaTest',
selector: 'SETPOSE',
symmetricFill: 'true'
})
};
request.post(options,function(error, response, body){
if(error){
ref.child("Read").set("Failed"); //If request fails or returns an error response, sends a fail message to DB
}
else if(JSON.parse(body)["Errors"]){
ref.child("Read").set("Failed");
}
else{
var status = JSON.parse(body)["images"][0]["transaction"]["status"];
if(status == "success"){
ref.child("Read").set("doneTrain"); //If successfully trained, indication word sent to DB for Alexa to say a proper response
}
else if (status == "failure"){
ref.child("Read").set("Failed");
}
}
});
});
}
});