Skip to content

Instantly share code, notes, and snippets.

@johnelliott
Last active April 10, 2016 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnelliott/12f4d5f7ea4c09e618dc4e66c9aefe3f to your computer and use it in GitHub Desktop.
Save johnelliott/12f4d5f7ea4c09e618dc4e66c9aefe3f to your computer and use it in GitHub Desktop.
pedestrian
var http = require('http');
var dup = require('./ped-duplex-stream.js');
var staticServer = require('ecstatic')({ root: __dirname + '/public' });
// Set up application data server
var server = http.createServer(function(req, res) {
if (req.url == '/api') {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('This is API data');
} else if (req.method === 'POST') {
console.log('got request', req.length);
res.writeHead(200, {'Content-Type': 'text/json'});
req.pipe(dup()).pipe(res);
} else {
req.addListener('end', function () {
//TODO specify caching, if needed and use in dev mode
staticServer(req, res);
}).resume();
}
});
var port = 3000;
console.log('Listen:', port);
server.listen(port);
var through = require('through');
var duplex = require('duplexer');
var tr = require('./transcode');
var cv = require('opencv');
module.exports = function pedTransformStream() {
// TODO parse piped in file and explode if it's a .mov
var cvStream = new cv.ImageStream();
var output = through();
tr.pipe(cvStream);
cvStream.on('finish', function() {
output.end();
});
var algo = 'node_modules/opencv/data/haarcascade_fullbody.xml';
cvStream.on('data', function(im) {
if (im.width() < 1 || im.height() < 1) throw new Error('image has no size');
im.detectObject(algo, {}, function(err, matches) {
if (err) {
console.error("Error:", err);
throw err;
}
if (matches.length > 0) {
for (var i = 0; i < matches.length; i++) {
var match = matches[i];
im.ellipse(match.x + match.width / 2,
match.y + match.height / 2,
match.width / 2,
match.height / 2);
}
// Write to output stream because cvStream is an event emitter
output.write(JSON.stringify(match) + '\n');
}
});
});
return duplex(tr, output);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment