Skip to content

Instantly share code, notes, and snippets.

@cranic
Created March 13, 2014 16:37
Show Gist options
  • Save cranic/9531963 to your computer and use it in GitHub Desktop.
Save cranic/9531963 to your computer and use it in GitHub Desktop.
/*
Setup a server, RTC and face tracking
Install opencv
brew install opencv
NPM
https://www.npmjs.org/package/fs
https://www.npmjs.org/package/http
https://www.npmjs.org/package/express
https://www.npmjs.org/package/socket.io
https://www.npmjs.org/package/opencv
*/
var APP = {
// include some scripts
server: null,
port: 3000,
express: require('express'),
app: null,
mdb: require('mongodb'),
dbUrl: 'mongodb://127.0.0.1:27017/test',
db: null,
io: null,
cv: require('opencv'),
fs: require("fs"),
init: function (){
// connect stuff up
APP.app = APP.express();
APP.server = require('http').createServer(APP.app).listen(APP.port, '0.0.0.0');
APP.io = require('socket.io').listen(APP.server);
// http routing
APP.routing();
// connect the websocket
APP.io.on('connection', function (socket){
console.log('Server started on port ' + APP.port);
APP.main(socket);
});
},
// open the db
openDB: function (){
APP.mdb.connect(APP.dbURL, function(err, db){
if(err)
throw err;
APP.db = db;
});
},
// close the db
closeDB: function (){
APP.db.close();
},
// insert a file to the db
dbInsert: function (col, data){
// open the db
APP.openDB();
var collection = APP.db.collection(col);
collection.insert(data, function(err, docs){
if(err){
console.warn(err.message);
} else {
console.log('Successfully inserted record');
}
});
// close the db
APP.closeDB();
},
// insert a file to the db
dbUpdate: function (col, crit, data){
// open the db
APP.openDB();
var collection = APP.db.collection(col);
collection.update(crit, {$set: {hi: 'there'}}, {w:1}, function (){
if(err){
console.warn(err.message);
} else {
console.log('Successfully updated record');
}
});
// close the db
APP.closeDB();
},
// find a file in the db
dbFind: function (col){
// open the db
APP.openDB();
var collection = APP.db.collection(col);
collection.find().toArray(function(err, results) {
console.dir(results);
});
// close the db
APP.closeDB();
},
// routing files
routing: function (){
// set directory to use for files prefixed with /library
APP.app.use('/library', APP.express.static(__dirname + '/library'));
// index page
APP.app.get('/*', function (req, res){
// get query
// console.log(req.query);
// get file
res.sendfile(__dirname + '/' + req.route.params);
});
},
main: function (socket){ // you was missing the socket here
// setInterval(function (){
// APP.io.sockets.emit('put_down', {'fingers': 's'});
// }, 5000);
// need to liston to the socket, not to the Socket.io library
socket.on('update', function (data) {
console.log(data);
// create the file
var filename = './file' + data.count + '.png';
APP.fs.writeFile(filename, data.imgData, 'base64', function(err) {
console.log(err);
});
APP.cv.readImage(filename, function(err, im){
im.detectObject(APP.cv.FACE_CASCADE, {}, function(err, faces){
for (var i=0;i<faces.length; i++){
var x = faces[i];
im.ellipse(x.x + x.width/2, x.y + x.height/2, x.width/2, x.height/2);
}
im.save(filename);
});
});
});
}
};
// run the script
APP.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment