lundi 30 mars 2015

Saving Images of Detected Faces using AR Drone

I am creating a code for the parrot ar drone such that when a face is detected through the front camera on the ar drone an image of the face is saved to the controller. Similar to the Copterface code, but instead of following the face, an image of it is saved. I originally wrote the code in c++ and it worked and I am writing the code for the drone in node.js/javascript but I cant get it to work. Note that I used the Coperface code as a basis for creating mine. Can anyone help me?



// Run this to receive a png image stream from your drone.

var arDrone = require('ar-drone');
var cv = require('opencv');
var http = require('http');
var fs = require('fs');

console.log('Connecting png stream ...');

//var stream = arDrone.createUdpNavdataStream();
var client = arDrone.createClient();
var pngStream = client.getPngStream();
var processingImage = false;
var lastPng;
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;
});

// Detect faces
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));

// Set Region of Interest
var roi_b;
var roi_c;

var ic = 0; // ic is index of current element
var ac = 0; // ac is area of current element

var ib = 0; // ib is index of biggest element
var ab = 0; // ab is area of biggest element

for (ic = 0; ic < faces.size(); ic++) // Iterate through all current elements (detected faces)

{
var roi_cqx = faces[ic].x;
var roi_cqy = faces[ic].y;
var roi_cqwidth = (faces[ic].width);
var roi_cqheight = (faces[ic].height);

var ac = roi_cqwidth * roi_cqheight; // Get the area of current element (detected face)

var roi_bqx = faces[ib].x;
var roi_bqy = faces[ib].y;
var roi_bqwidth = (faces[ib].width);
var roi_bqheight = (faces[ib].height);


var crop = frame(roi_b);
resize(crop, res, Size(128, 128), 0, 0, INTER_LINEAR); // This will be needed later while saving images
cvtColor(crop, gray, CV_BGR2GRAY); // Convert cropped image to Grayscale

// Form a filename
filename = "/home/iram4/Desktop/Faces";
var ssfn;
ssfn << filename.c_str() << filenumber << ".jpg";
filename = ssfn.str();
imwrite(filename, res); filenumber++;

}

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 ...');
});

Aucun commentaire:

Enregistrer un commentaire