-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
84 lines (69 loc) · 2.26 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
const morgan = require('morgan')
const express = require('express')
const { PythonShell } = require("python-shell");
const app = express()
PORT = process.env.PORT || 3000;
// Handling CORS
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
if (req.method === "OPTIONS") {
res.header("Access-Control-Allow-Methods", "PUT, POST, PATCH, DELETE, GET");
return res.status(200).json({
status: true,
message: "request granted",
});
}
next();
});
// Middlewares
app.use(morgan("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.get('/', (req, res) => {
res.sendFile('index.html', { root: 'detection-website' })
})
app.post('/test', (req, res) => {
res.send('Hello World')
})
var python_process;
app.post('/start', function (req, res) {
const input = req.body.input || 'videos/cctv_footage.mp4';
const output = req.body.output || 'output/Test.avi';
const yolo = req.body.yolo || 'yolo-coco';
const confidence = req.body.confidence || 0.0;
const threshold = req.body.threshold || 0.0;
let options = {
mode: "text",
args: ["-i" + input, "-o" + output, '-y' + yolo, '-c' + confidence, '-t' + threshold]
};
var pyshell = new PythonShell('detect.py', options);
pyshell.end(function (err) {
if (err)
console.log(err);
res.sendFile('index.html', { root: 'detection-website' });
});
python_process = pyshell.childProcess;
});
app.post('/stop', function (req, res) {
python_process.kill('SIGINT');
res.sendFile('index.html', { root: 'detection-website' });
});
// Throw 404 error if the request does not match an existing route
app.use((req, res, next) => {
const error = new Error();
error.status = 404;
error.message = "404 route not found";
next(error);
});
// Return the error thrown by any part of the project.
app.use((err, req, res, next) => {
res.status(err.status || 404).json({
status: false,
error: err.message,
});
});
app.listen(PORT, () => console.log(`\nServer running on localhost:${PORT}\n`));