-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstalk.js
122 lines (91 loc) · 2.63 KB
/
stalk.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
var http = require('http');
var cv = require('opencv');
var fs = require('fs');
var arDrone = require('ar-drone');
var Navigation = require('./cv-navigation.js');
var control = arDrone.createUdpControl();
var log = console.log;
var face_cascade = new cv.CascadeClassifier('./data/haarcascade_frontalface_alt2.xml');
log('Connecting png stream ...');
var COLOR = [0, 255, 0]; //default red
var thickness = 2; // default 1
var lastPngBuffer;
var mimeTypes = {
'html': 'text/html',
'js': 'text/javascript'
};
var nav = new Navigation(640, 360, 1.5);
var pngStream = arDrone.createPngStream();
var lastPng;
pngStream
.on('error', log)
.on('data', function(pngBuffer) {
lastPngBuffer = pngBuffer;
});
var ref = {};
var pcmd = {};
console.log('Recovering from emergency mode if there was one ...');
ref.emergency = true;
setTimeout(function() {
console.log('Takeoff ...');
ref.emergency = false;
ref.fly = true;
}, 8000);
/*setTimeout(function() {
console.log('Landing ...');
ref.fly = false;
pcmd = {};
}, 8000);*/
setInterval(function() {
control.ref(ref);
control.pcmd(pcmd);
control.flush();
}, 30);
var detectFace = function(){
cv.readImage(lastPngBuffer, function(err, im){
//log("Size: " + im.width() + "x" + im.height());
//im.resize(im.width() / 2, im.height() / 2);
//log("Resize: " + im.width() + "x" + im.height());
face_cascade.detectMultiScale(im, function(err, faces) {
for(var k = 0; k < faces.length; k++) {
face = faces[k];
im.rectangle([face.x, face.y], [face.x + face.width, face.y + face.height], COLOR, 2);
}
pcmd = nav.getOptions(faces);
console.log(pcmd);
//lastPng = im.toBuffer();
});
});
};
var faceDetectLoop = function(){
if(lastPngBuffer){
log('Detecting Face.');
detectFace();
};
setTimeout(faceDetectLoop, 500);
};
faceDetectLoop();
/*var server = http.createServer(function(req, res) {
if (req.url === '/cam.png') {
if (!lastPng) {
res.writeHead(503);
res.end('Did not receive any png data yet.');
return;
}
res.writeHead(200, {'Content-Type': 'image/png'});
res.end(lastPng);
} else {
var file = req.url.replace(/^\/$/, 'index.html').replace(/\.\./, '').replace(/^\//, './');
var fileType = file.replace(/\.\w+$/) || "html";
fs.readFile(file, function (err, content) {
if (err) { log(err); }
res.writeHead(200, {'Content-Type': mimeTypes[fileType]});
res.end(content);
});
}
});*/
/*
server.listen(8080, function() {
log('Serving latest png on port 8080 ...');
});
*/