-
Notifications
You must be signed in to change notification settings - Fork 0
/
face.js
149 lines (114 loc) · 4 KB
/
face.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Run this to receive a png image stream from your drone.
//
// copterface https://github.com/paulhayes/copterface
var arDrone = require('ar-drone');
var cv = require('opencv');
var http = require('http');
var fs = require('fs');
console.log('Connecting png stream ...');
var pngStream = arDrone.createPngStream();
//var stream = arDrone.createUdpNavdataStream();
var client = arDrone.createClient();
var processingImage = false;
var lastPng;
var face_cascade = new cv.CascadeClassifier('node_modules/opencv/data/haarcascade_frontalface_alt2.xml');
var navData;
var flying = false;
var startTime = new Date().getTime();
var log = function(s){
var time = ( ( new Date().getTime() - startTime ) / 1000 ).toFixed(2);
console.log(time+" \t"+s);
}
pngStream
.on('error', console.log)
.on('data', function(pngBuffer) {
//console.log("got image");
lastPng = pngBuffer;
});
var detectFaces = function(){
if( ! flying ) return;
if( ( ! processingImage ) && lastPng )
{
processingImage = true;
cv.readImage( lastPng, function(err, im) {
var opts = {};
face_cascade.detectMultiScale(im, function(err, faces) {
var face;
var biggestFace;
for(var k = 0; k < faces.length; k++) {
face = faces[k];
if( !biggestFace || biggestFace.width < face.width ) biggestFace = face;
//im.rectangle([face.x, face.y], [face.x + face.width, face.y + face.height], [0, 255, 0], 2);
}
if( biggestFace ){
face = biggestFace;
console.log( face.x, face.y, face.width, face.height, im.width(), im.height() );
face.centerX = face.x + face.width * 0.5;
face.centerY = face.y + face.height * 0.5;
var centerX = im.width() * 0.5;
var centerY = im.height() * 0.5;
var heightAmount = -( face.centerY - centerY ) / centerY;
var turnAmount = -( face.centerX - centerX ) / centerX;
turnAmount = Math.min( 1, turnAmount );
turnAmount = Math.max( -1, turnAmount );
log( turnAmount + " " + heightAmount );
//heightAmount = Math.min( 1, heightAmount );
//heightAmount = Math.max( -1, heightAmount );
heightAmount = 0;
if( Math.abs( turnAmount ) > Math.abs( heightAmount ) ){
log( "turning "+turnAmount );
if( turnAmount < 0 ) client.clockwise( Math.abs( turnAmount ) );
else client.counterClockwise( turnAmount );
setTimeout(function(){
log("stopping turn");
client.clockwise(0);
//this.stop();
},100);
}
else {
log( "going vertical "+heightAmount );
if( heightAmount < 0 ) client.down( heightAmount );
else client.up( heightAmount );
setTimeout(function(){
log("stopping altitude change");
client.up(0);
},50);
}
}
processingImage = false;
//im.save('/tmp/salida.png');
}, opts.scale, opts.neighbors
, opts.min && opts.min[0], opts.min && opts.min[1]);
});
};
};
var faceInterval = setInterval( detectFaces, 150);
client.takeoff();
client.after(5000,function(){
log("going up");
this.up(1);
}).after(1000,function(){
log("stopping");
this.stop();
flying = true;
});
client.after(60000, function() {
flying = false;
this.stop();
this.land();
});
client.on('navdata', function(navdata) {
navData = navdata;
})
var server = http.createServer(function(req, res) {
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);
});
server.listen(8080, function() {
console.log('Serving latest png on port 8080 ...');
});