-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.js
124 lines (97 loc) · 3.76 KB
/
camera.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
(function () {
'use strict';
const cv = require('opencv');
const request = require('request').defaults({encoding: null});
const Stream = require('stream');
const ffmpeg = require('fluent-ffmpeg');
const saveImage = require('./save-image');
const streamVideo = require('./stream-video');
const clients = require('./clients');
function setupStaticCamera(config) {
const camFps = config.fps;
const camInterval = 1000 / camFps;
setInterval(() => {
request(config.camera, (error, response, body) => {
if (error || !response || response.statusCode !== 200) {
error && console.log(`Camera ${config.camera} opened failed`, error);
return;
}
const contentType = response.headers['content-type'];
if (contentType === 'image/jpeg') {
for (const index in clients) {
clients[index].emit('frame2', {buffer: body});
}
}
});
}, camInterval);
}
function readCamera(config, stream, camera, callback) {
if (config.static) {
request(config.camera, {
'auth': {
'user': config.username,
'pass': config.password,
'sendImmediately': false
}
},
(error, response, body) => {
if (error || !response || response.statusCode !== 200) {
return;
}
const contentType = response.headers['content-type'];
if (contentType === 'image/jpeg') {
callback(config, stream, body);
}
});
} else if (Number.isInteger(config.camera)) {
camera.read(function (error, im) {
if (error) {
error && console.log(`Camera ${config.camera} opened failed`, error);
}
im.putText(new Date().toLocaleString(), 10, 20, 'HERSHEY_PLAIN', [255, 0, 0], 0.5, 1);
callback(config, stream, im.toBuffer());
});
}
}
function setupCamera(config) {
let camera;
let stream = new Stream.PassThrough();
streamVideo.recordVideoFromStream(config, stream);
if (Number.isInteger(config.camera)) {
// initialize camera
camera = new cv.VideoCapture(config.camera);
console.log(`SOURCE: Camera ${config.camera} opened`);
const camWidth = config.width;
const camHeight = config.height;
camera.setWidth(camWidth);
camera.setHeight(camHeight);
} else if (config.static) {
console.log(`SOURCE: Static image source ${config.camera}`);
}
const camFps = config.fps;
const camInterval = 1000 / camFps;
setInterval(() => {
readCamera(config, stream, camera, processImage);
// streamVideo(config);
}, camInterval);
return camera;
}
function createReadStream(stream, image) {
stream.push(image);
}
function processImage(config, stream, image) {
if (!image) return;
// Send image to clients
sendtoClients(config, image);
createReadStream(stream, image);
// Save image to disk
// saveImage(image, config);
}
function sendtoClients(config, image) {
if (!image) return;
for (const index in clients) {
clients[index].emit('frame' + config.id, {buffer: image});
}
}
module.exports = setupCamera;
})();