use.js
2.51 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
77
78
79
80
81
82
83
84
85
86
var express = require("express");
var router = express.Router();
var AWS = require("aws-sdk");
var fs = require("fs");
var atob = require("atob");
AWS.config.loadFromPath("./config.json");
var rekognition = new AWS.Rekognition();
const { exec } = require("child_process");
router.post("/", function (req, res, next) {
console.log(req.body);
if (req.body.landing == "true") {
res.render("use", {
method: req.body.type,
collection: req.body.collection,
});
} else {
image = req.body.image;
console.log(req.body.collection);
// console.log(image)
// var img_arr = req.body.image.split(',').map(function(item) {
// return parseInt(item, 10);
// });
// console.log(img_arr)
// enc_data = Buffer.from(image.split("data:image/png;base64,")[1], 'base64').toString('ascii')
if (image[11] == "p")
enc_data = atob(image.split("data:image/png;base64,")[1]);
else if (image[11] == "j")
enc_data = atob(image.split("data:image/jpeg;base64,")[1]);
var length = enc_data.length;
imageBytes = new ArrayBuffer(length);
var ua = new Uint8Array(imageBytes);
for (var i = 0; i < length; i++) {
ua[i] = enc_data.charCodeAt(i);
}
// console.log(imageBytes)
var params = {
CollectionId: req.body.collection,
FaceMatchThreshold: 95,
Image: {
Bytes: imageBytes,
},
MaxFaces: 5,
};
rekognition.searchFacesByImage(params, function (err, data) {
if (err) {
console.log(err, err.stack);
} else {
if (data.FaceMatches.length > 0) {
res.render("redirect", {
error: "Recognized!",
collection: req.body.collection,
});
} else {
exec("python action.py " + req.body.type, (error, stdout, stderr) => {
if (error) {
res.render("error", {
message: "Error",
title: "No action.py",
content: "make sure you have cloned github repository",
});
return;
}
if (stderr) {
res.render("error", {
message: "Error",
title: "No action.py",
content: "make sure you have cloned github repository",
});
return;
}
console.log(`stdout: ${stdout}`);
res.render("action");
});
}
}
});
}
});
router.get("/", function (req, res, next) {
res.render("use");
});
module.exports = router;