app.js
2.46 KB
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
var express = require('express');
var bodyParser= require('body-parser');//post할때 필요
var multer = require('multer'); //업로드할 때 필요
var request = require('request');
var http = require('http');
var app= express();
app.locals.pretty = true;
app.use(express.static(__dirname));
app.use('/user',express.static('uploads'));
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.engine('html', require('ejs').renderFile);
app.use(bodyParser.urlencoded({extended:false}));
var imgname;
var storage = multer.diskStorage({ //이 객체의 프로퍼티는 2개갖고 있다.
destination: function (req, file, cb) {
cb(null, 'uploads/');//업로드 위치를 저장하는 path함수
},
filename: function (req, file, cb) {
cb(null, file.originalname);
cb(null, 'mypic.jpg');
}
});
var upload = multer({ storage: storage});
// Replace <Subscription Key> with your valid subscription key.
var subscriptionKey = '61650f4a2941462ea2dba74eabefd114';
// You must use the same location in your REST call as you used to get your
// subscription keys. For example, if you got your subscription keys from
// westus, replace "westcentralus" in the URL below with "westus".
var uriBase = 'https://eastasia.api.cognitive.microsoft.com/face/v1.0/detect';
// Request parameters.
var params = {
'returnFaceId': 'true',
'returnFaceLandmarks': 'false',
'returnFaceAttributes': 'age,gender,emotion,hair,accessories'
//'headPose,smile,facialHair,glasses, emotion,hair,makeup,occlusion,accessories,blur,exposure,noise'
};
app.get('/home', function(req,res){
console.log("home");
res.render('upload');
});
app.post('/uploads', upload.single('userfile'),function(req,res){
res.render("facedetection");
imgname = req.file.filename;
});
app.get('/send2api', function (req, res) {
var options = {
uri: uriBase,
qs: params,
body: '{"url": "http://13.125.213.193:8080/uploads/' + imgname + '"}',
headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key' : subscriptionKey
}
};
request.post(options, (error, response, body) => {
if (error) {
console.log('Error: ', error);
return;
}
let jsonResponse = JSON.stringify(JSON.parse(body), null, ' ');
console.log('JSON Response\n');
res.send(jsonResponse);
});
})
app.listen(8080,function(){
console.log("connect 8080");
});