This repository was archived by the owner on Aug 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcamera.js
More file actions
83 lines (73 loc) · 2.47 KB
/
Copy pathcamera.js
File metadata and controls
83 lines (73 loc) · 2.47 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
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
var cv = require('opencv');
var raspicam2;
try { raspicam2 = require('raspicam2').raspicam2; } catch (e) {}
var sys = require('sys')
var exec = require('child_process').exec;
var FpsCounter = require('./fpscounter.js').FpsCounter;
var path = require('path');
// Camera class that allows multiple sources to access the last acquired
// image.
function Camera(size, fps) {
this.width = size[0];
this.height = size[1];
this.fpsCounter = FpsCounter("camera");
this.cvImage = null;
var instance = this;
var child = exec("uname -m", function (error, stdout, stderr) {
if (error !== null)
console.log('exec error: ' + error);
instance.isRaspi = stdout.trim() == "armv7l";
if (instance.isRaspi) {
console.log("camera: creating RaspiCam");
raspicam2.open();
raspicam2.setSize(instance.width, instance.height);
var dummyImageFn = path.resolve(__dirname, 'dummy.png');
cv.readImage(dummyImageFn, function(err, cvImg) {
instance.cvImage = cvImg;
instance.data = new Buffer(this.width * this.height * 3);
});
} else {
console.log("camera: creating OpenCV cam");
instance.cvcam = new cv.VideoCapture(0);
instance.cvcam.setWidth(size[0]);
instance.cvcam.setHeight(size[1]);
instance.cvImage = cv.ReadSync();
}
startCam(fps);
});
}
function startCam(instance, fps) {
console.log("starting camera");
var funcOuter;
if (instance.isRaspi) {
var funcInner = function() {
instance.cvImage.put(instance.data);
sys.exit();
instance.fpsCounter.tick(5000);
};
funcOuter = function() {
if (!instance.inited) return;
raspicam2.readAsync(instance.data, funcInner);
}
} else {
var funcInner = function(err, im) {
if (err) throw err;
instance.cvImage = im;
instance.fpsCounter.tick(5000);
};
funcOuter = function() {
if (!instance.inited) return;
instance.cvcam.read(funcInner);
}
}
this.intervalId = setInterval(funcOuter, 1000 / fps);
}
Camera.prototype.stop = function(fps) {
clearInterval(this.intervalId);
raspicam2.release();
console.log("stopping camera");
}
Camera.prototype.getCvImage = function() {
return this.cvImage;
}
module.exports.Camera = Camera;