forked from Ralphive/b1ml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
163 lines (120 loc) · 4.45 KB
/
app.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Load Node Dependencies
var AWS = require('aws-sdk');
var express = require('express');
var bodyParser = require('body-parser');
// Load Node Modules
var path = require('path');
var fs = require('fs');
var uuid = require('node-uuid');
//Local Modules
var bucket = require('./bucket')
var global = require('./global')
var ml = require('./ml');
var sl = require('./sl');
var leo = require('./leo');
var port = 8080
//Initialization
//Load Credentials and Region
AWS.config.loadFromPath('./awsConfig.json');
// Create an S3 client (for file management)
var s3 = new AWS.S3({apiVersion: '2006-03-01'});
// Create an Rekognition client (Image classification)
var rek = new AWS.Rekognition({apiVersion: '2016-06-27'})
//Configure Express
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
leo.updateVectorsBase();
// Main html page
/** Endpoints */
app.get('/', function(req, res){
res.sendFile(path.join(__dirname, 'views/index.html'));
});
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// Service Layer Services
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// Login started by external apps or managed internally?
app.get('/sl/Connect', function(req, res){
sl.Connect(res);
console.log('Connect SL started');
});
// Get Similar Items
// input = picture of an item
app.post('/sl/GetSimilarItems', function(req, res){
var picture = JSON.stringify(req.body);
sl.GetSimilarItems(picture, res);
console.log('GetSimilarItems');
});
// Create Draft Order
app.post('/sl/CreateDraftOrder', function(req, res){
var draft = JSON.stringify(req.body);
sl.CreateDraftOrder(draft, res);
console.log('CreateDraftOrder ');
});
// Get last Order Draft for a specific customer - bpCode as query string (if empty default value taken)
app.get('/sl/GetDraftOrder', function(req, res){
var bpCode = req.query.bpCode;
sl.GetDraftOrder(bpCode, res);
console.log('Got bpCode from QUERY ' + bpCode);
});
// Create Order from a Draft DocEntry and Lines details
app.post('/sl/CreateOrderFromDraft', function(req, res){
var input = JSON.stringify(req.body);
sl.CreateOrderFromDraft(input, res);
console.log('CreateOrderFromDraft');
});
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//Called by FB Integration
// Receives a Json with user + Pictures array
app.post('/trainSystem', function(req, res){
//Store the images on the S3 Bucket, then add them to the collection
var bucketName = global.userNs(req.body.user)+"-"+uuid.v4();
bucket.create(s3, req.body.user, bucketName, req.body.pics, rek)
res.send({msg:'All Good!'});
});
// Identifies the user of a given image
app.post('/searchFace', function(req, res){
var output = {}
console.log("Call Received");
//Store image on the defaulFacesBucket
console.dir(req.body);
bucket.put(s3, global.faceBucket(),'xxx', req.body.pics,null, function(ret){
ml.searchFaces(rek, global.faceBucket(), ret.Key, function(err, data){
if (err){
output = err;
res.send(output);
}
if (data.FaceMatches.length > 0){
data = data.FaceMatches;
var extImgId = '';
extImgId = data[0].Face.ExternalImageId;
output.user = extImgId.substring(0,extImgId.indexOf('-'));
output.Similarity = data[0].Similarity
res.send(output);
}else{
output = data;
res.send(output);
}
console.log(output);
})
});
//Create a collection on Rekognition for the give user
});
app.post('/initialize', function(req,res){
//Initialize all the system
if (req.body.collections){
ml.deleteCollections(rek, function(data){
//Create a collection on Rekognition for the give user
ml.createCollection(rek,global.faceCollection())
});
}
if(req.body.buckets){
bucket.deleteBuckets(s3, function(){
bucket.create(s3, null, global.faceBucket(), null, null);
});
}
});
var server = app.listen(port, function(){
console.log('Server listening on port '+port);
});