-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
57 lines (47 loc) · 1.39 KB
/
server.js
File metadata and controls
57 lines (47 loc) · 1.39 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
const cv = require('opencv4nodejs')
const net = require('net');
const port = 8888
const fps = 24
// opencv camera
let camera = null
// tcp server
const server = net.createServer()
/**
* @todo:
* USE socket.io NOT TCP(net) ?????
* USE pi-camera PACKAGE, NOT OPENCV ?????
* ADD multiple clients and cameras support
* THINK: do pi(camera computer) need to be server (DNS-problem)
*/
let cameraInterval = null
// on client connection
server.on('connection', (socket) => {
console.log("user connected");
// start camera
camera = new cv.VideoCapture(0)
camera.set(cv.CAP_PROP_FRAME_WIDTH, 640)
camera.set(cv.CAP_PROP_FRAME_HEIGHT, 480)
cameraInterval = setInterval(() => {
const frame = camera.read()
const image = cv.imencode('.jpg', frame).toString('base64')
socket.write(image + ",") // comma is not base64 character
}, 1000 / fps)
socket.on('end', () => {
clearInterval(cameraInterval)
camera.release()
socket.destroy()
console.log("\x1b[33m", "client disconnected!", "\x1b[0m")
})
})
// errors handling
process.on('uncaughtException', (err) => {
clearInterval(cameraInterval)
camera.release()
if(err.message == "read ECONNRESET"){
console.log("\x1b[33m", "client disconnected", "\x1b[0m")
} else{
console.error(err.message)
}
})
// start server
server.listen(port, () => console.log("\x1b[32m", "server started", "\x1b[0m"))