-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
53 lines (46 loc) · 1.3 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
const express = require('express')
const app = express()
const port = process.env.PORT || 3000
const { exec } = require('child_process')
const py_path = '\\opencv.py'
const asb_py_path = process.cwd() + py_path
// create socket.io server
const { createServer } = require('http')
const httpServer = createServer(app)
const io = require('socket.io')(httpServer, {
cors: {
origin: '*',
credentials: true
}
})
app.use(express.static('public'));
io.on('connection', socket => {
socket.on('open-webcam', () => {
exec(`python ${asb_py_path}`, (error, stdout, stderr) => {
if (error) {
console.error(`error from executing python script:${error}`);
return;
}
})
})
// get python opencv frame
socket.on('python-opencv', (data) => {
// emit frames to front-end
// const frame = Buffer.from(data, 'base64').toString()
socket.broadcast.emit('node-server-frame', data)
})
// screenshot event
socket.on('capture-screenshot', () => {
// emit to python
socket.broadcast.emit('python-screenshot')
})
// close webcam
socket.on('close-webcam', () => {
socket.broadcast.emit('close-opencv')
})
})
app.get('/', (req,res)=>{
res.redirect('index.html');
})
// start
httpServer.listen(port, () => console.log(`Server is running on http://localhost:${port}`))