-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
139 lines (104 loc) · 3.88 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
const express = require('express');
const app = express();
let { PythonShell } = require('python-shell')
// Establishing the port
const PORT = process.env.PORT || 3000;
// Executing the server on given port number
app.listen(PORT, console.log(
`Server started on port ${PORT}`));
const multer = require('multer');
const upload = multer({ dest: "uploads" }); // 設定上傳暫存目錄
const fs = require('fs'); // 處理檔案的核心套件
app.use(express.static('public'));
app.set('view engine','ejs');
// 首頁連接
app.get('/',(req,res)=>{
res.render('index');
});
// 人臉分析
app.get('/analyze',function(req,res) {
res.render('./analyze/analyze',{});
});
app.post('/analyze1', upload.single('photo'), function(req, res){
// console.log(req.file); // 查看裡面的屬性
if (req.file && req.file.originalname) {
// 判斷是否為圖檔
if (/\.(jpg|jpeg|png|gif)$/i.test(req.file.originalname)) {
// 將檔案搬至公開的資料夾
fs.rename(req.file.path, './public/img/001.jpg' , error => { });
// 執行Face_Analyze.py程式並傳回值
PythonShell.run('Face_Analyze.py', null, function(err, data) {
if (err) throw err;
// var results = await JSON.stringify(data)
var results = data.slice(-5,-1)
// var results = data
console.log(results);
res.render('./analyze/analyze_python',{results});
});
}
} else {
fs.unlink(req.file.path, error => { }); // 刪除暫存檔
}
});
// 人臉比對
app.get('/ver',function(req,res) {
res.render('./ver/ver',{});
});
var files = [{ name: 'photo1' }, { name: 'photo2' }]
app.post('/ver1', upload.fields(files), async function (req, res) { //async
//存照片
for (let i = 0; i < files.length; i++) {
if (/\.(jpg|jpeg|png|gif)$/i.test(req.files[files[i].name][0].originalname)) {
fs.createReadStream(req.files[files[i].name][0].path).pipe(
fs.createWriteStream('./public/img/ver' + [i] + '.jpg')
)
};
}
PythonShell.run('Face_Verification.py', null, async function (err, data) {
// 將update的照片檔名弄成object
Obj={fileName:req.files[files[0].name][0].originalname}
Obj1={fileName1:req.files[files[1].name][0].originalname}
var results = await JSON.stringify(data)
var results = data.slice(-5, -1)
console.log(results);
res.render('./ver/ver_python', { results });
});
});
// 群眾分析
app.get('/mtcnn',function(req,res) {
res.render('./mtcnn/mtcnn',{});
});
app.post('/mtcnn1', upload.single('photo'), function(req, res){
// console.log(req.file); // 查看裡面的屬性
if (req.file && req.file.originalname) {
// 判斷是否為圖檔
if (/\.(jpg|jpeg|png|gif)$/i.test(req.file.originalname)) {
// 將檔案搬至公開的資料夾
fs.rename(req.file.path, './public/img/mtcnn1.jpg' , error => { });
// 執行Face_Analyze.py程式並傳回值
PythonShell.run('MTCNN.py', null, function(err, data) {
if (err) throw err;
// var results = await JSON.stringify(data)
var results = data.slice(-5,-1)
// var results = data
console.log(results);
res.render('./mtcnn/mtcnn_python',{results});
});
}
} else {
fs.unlink(req.file.path, error => { }); // 刪除暫存檔
}
});
// Webcam
app.get('/cam',function(req,res) {
res.render('./cam/cam',{});
});
app.get('/cam1',function(req,res) {
PythonShell.run('Webcamhaar.py', null, function(err, data) {
if (err) throw err;
// var results = await JSON.stringify(data)
var results = data.slice(5,6)
console.log(results);
res.render('./cam/cam_python',{results});
});
});