Skip to content

Commit

Permalink
Commit with too many func :( #hackathon.
Browse files Browse the repository at this point in the history
  • Loading branch information
stygma committed Apr 26, 2015
1 parent 3022633 commit 365d3ee
Show file tree
Hide file tree
Showing 26 changed files with 282 additions and 51 deletions.
30 changes: 19 additions & 11 deletions server/api/controllers/AuthController.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,27 @@ function logout(req, res) {
};

function register(req, res) {
var userObj = {
username: req.param('username'),
password: req.param('password'),
karma: 0,
};
UtilService.uploadFile(req.file('uploadFile')).then(function(file) {
var fd = file.fd.split('/');
var filename = fd[fd.length - 1];

var userObj = {
username: req.param('username'),
password: req.param('password'),
photo: filename,
karma: 0,
};

User.create(userObj).then(function(user) {
req.session.authenticated = true;
req.session.username = user.username;
req.session.user = user;
req.session.name = user.username;
User.create(userObj).then(function(user) {
UserActionService.registerActor(file);

return res.redirect('/thanks');
req.session.authenticated = true;
req.session.username = user.username;
req.session.user = user;
req.session.name = user.username;

return res.redirect('/thanks');
});
});
};

Expand Down
8 changes: 5 additions & 3 deletions server/api/controllers/PartnerController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

function view(req, res) {
Partner.findOne(req.param('partnerId')).populate('rewards').then(function(partner) {
var fd = partner.photo.split('/');
var filename = fd[fd.length - 1];
if (partner.photo) {
var fd = partner.photo.split('/');
var filename = fd[fd.length - 1];

partner.photo = filename;
partner.photo = filename;
}

return res.view('partner', {
partner: partner,
Expand Down
3 changes: 3 additions & 0 deletions server/api/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ module.exports = {
phone: {
type: 'string'
},
photo: {
type: 'string'
},
karma : {
type: 'integer'
},
Expand Down
21 changes: 21 additions & 0 deletions server/api/policies/userAuth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* sessionAuth
*
* @module :: Policy
* @description :: Simple policy to allow any authenticated user
* Assumes that your login action in one of your controllers sets `req.session.authenticated = true;`
* @docs :: http://sailsjs.org/#!documentation/policies
*
*/
module.exports = function(req, res, next) {

// User is allowed, proceed to the next policy,
// or if this is the last policy, the controller
if (req.session.user) {
return res.redirect('/thanks');
}

// User is not allowed
// (default res.forbidden() behavior can be overridden in `config/403.js`)
return next();
};
72 changes: 52 additions & 20 deletions server/api/services/UserActionService.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,75 @@
var opencv = require('opencv');

function registerAction(action, recipient) {
function preprocessImage(file) {
return new Promise(function(resolve) {
UtilService.uploadFile(action.file).then(function(file) {
opencv.readImage(file.fd, function(err, img) {
img.detectObject(opencv.FACE_CASCADE, {}, function(err, faces) {
opencv.readImage(file.fd, function(err, img) {
img.detectObject(opencv.FACE_CASCADE, {}, function(err, faces) {
for (var i=0;i<faces.length; i++){
var x = faces[i];
img.ellipse(x.x + x.width/2, x.y + x.height/2, x.width/2, x.height/2);
}

console.log('ERROR: ', err);
var preprocImg = './assets/images/pre/' + file.filename;
img.save(preprocImg);

return resolve(preprocImg);
});
});
});
};

for (var i=0;i<faces.length; i++){
var x = faces[i]
img.ellipse(x.x + x.width/2, x.y + x.height/2, x.width/2, x.height/2);
}

var preprocImg = './assets/images/pre/' + file.filename;
console.log(preprocImg)
img.save(preprocImg);
function registerAction(action, recipient) {
return new Promise(function(resolve) {
UtilService.uploadFile(action.file).then(function(file) {
preprocessImage(file).then(function(preprocImg) {

identifyUser(preprocImg).then(function(actor) {
Action.create({ photo: preprocImg.fd, agent: actor.id, description: action.description, tags: extractTags(action.description)}).then(function(action) {
return resolve(action);
});
identifyUser(preprocImg).then(function(actor) {
Action.create({ photo: preprocImg.fd, agent: actor.id, description: action.description, tags: extractTags(action.description)}).then(function(action) {
return resolve(action);
});
});

});
});
});
};

function registerActor(image) {
sails.log('register actor: ', image);

return new Promise(function(resolve) {
preprocessImage(image).then(function(preprocImg) {
identifyUser(image).then(function(user) {
if (user) {
return resolve(user);
}

// TODO

return resolve(null);
});
});
});
};

function identifyUser(image) {
sails.log('register actor: ', image);

return new Promise(function(resolve) {
User.find().then(function(users) {
var user = users[0];
opencv.readImage(image, function(err, img) {

sails.log('USER: ', user);
return resolve(user);
User.find().then(function(users) {
var user = users[0];

sails.log('USER: ', user);
return resolve(user);
});
});
});
};


function extractTags(description){
var tagsRegex = /\S*#(?:\[[^\]]+\]|\S+)/ig;
return description ? description.match(tagsRegex) : '';
Expand All @@ -47,4 +78,5 @@ function extractTags(description){
module.exports = {
registerAction: registerAction,
identifyUser: identifyUser,
registerActor: registerActor,
};
Binary file added server/assets/images/users/0/ogy.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added server/assets/images/users/1/ivan.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added server/assets/images/users/2/user.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
79 changes: 75 additions & 4 deletions server/assets/js/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ $(document).ready(function () {
url: "subscribe.php",
data: $("#postcontent").serialize(),
success: function (response) {
$('[name="email"]').val('');
$('[name="email"]').val('');
// alert(response); // FOR ACTUAL RESPONSE
alert('Thanks for subscribing Us');
alert('Thanks for subscribing Us');
}
});
e.preventDefault();
Expand All @@ -78,5 +78,76 @@ $(document).ready(function () {
data = $(this).text() + ' ';
$("#description").val($("#description").val() + data);
});

});
});

var isStreaming = false,
v = document.getElementById('v'),
c = document.getElementById('c'),
play = document.getElementById('play');
con = c.getContext('2d');
w = 600,
h = 420,
greyscale = false;

v.addEventListener('canplay', function(e) {
if (!isStreaming) {
// videoWidth isn't always set correctly in all browsers
if (v.videoWidth > 0) h = v.videoHeight / (v.videoWidth / w);
c.setAttribute('width', w);
c.setAttribute('height', h);
// Reverse the canvas image
con.translate(w, 0);
con.scale(-1, 1);
isStreaming = true;
}
}, false);

v.addEventListener('play', function() {
// Every 33 milliseconds copy the video image to the canvas
setInterval(function() {
if (v.paused || v.ended) return;
con.fillRect(0, 0, w, h);
con.drawImage(v, 0, 0, w, h);
goingGrey();
}, 33);
}, false);

play.addEventListener('click', function() {
var v = document.getElementById('v');
navigator.getUserMedia = (navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
if (navigator.getUserMedia) {
// Request access to video only
navigator.getUserMedia({
video:true,
audio:false
},
function(stream) {
var url = window.URL || window.webkitURL;
v.src = url ? url.createObjectURL(stream) : stream;
v.play();
},
function(error) {
alert('Something went wrong. (error code ' + error.code + ')');
return;
});
} else {
alert('Sorry, the browser you are using doesn\'t support getUserMedia');
return;
}
}, false);

var goingGrey = function() {
var imageData = con.getImageData(0, 0, w, h);
var data = imageData.data;
for (var i = 0; i < data.length; i += 4) {
var bright = 0.34 * data[i] + 0.5 * data[i + 1] + 0.16 * data[i + 2];
data[i] = bright;
data[i + 1] = bright;
data[i + 2] = bright;
}
con.putImageData(imageData, 0, 0);
}

Binary file added server/assets/ogy.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added server/assets/ogy2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added server/assets/ogy3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions server/assets/styles/profile-card.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
line-height: 16px;
position: relative;
margin-bottom: 10px;
margin-top: 10px;
margin-top: 40px;
}

.profile-card .profile-card-background{
Expand Down Expand Up @@ -40,9 +40,10 @@
}
.profile-card .profile-card-content .profile-card-user-fields {
position: absolute;
top: 103px;
/*top: 103px;*/
left: 90px;
width: 185px;
margin-top: -35px;
}

.profile-card .profile-card-content .profile-card-user-fields .profile-card-name{
Expand Down
5 changes: 5 additions & 0 deletions server/assets/styles/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,8 @@ ul.img-list li:hover span.text-content {
opacity: 1;
font-size: 20px;
}

video {
position:absolute;
visibility:hidden;
}
72 changes: 69 additions & 3 deletions server/config/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,77 @@
* http://sailsjs.org/#/documentation/reference/sails.config/sails.config.bootstrap.html
*/

var upload = './assets/images/upload/';
var predir = './assets/images/pre/';
var usersdir = './assets/images/users/';

Promise = require('bluebird');
cv = opencv = require('opencv');
fr = opencv.FaceRecognizer.createLBPHFaceRecognizer();
trainSet = [];

function train(userPhotos) {
return new Promise(function(resolve) {

_.each(userPhotos, function(photo, id) {
opencv.readImage(upload + photo, function(err, img) {
if (err) throw err;
if (img.width() < 1 || img.height() < 1) throw new Error('Image has no size');

//img.inRange(lower_threshold, upper_threshold);
//img.save(predir + picname);

img.convertGrayscale();
img.save(predir + photo);

opencv.readImage(predir + photo, function(err, img) {

img.detectObject(cv.FACE_CASCADE, {}, function(err, faces){
if (err || !faces.length || faces.length > 1) {
sails.log.error(err, faces, photo);
return sails.log("Take another photo");
}

var face = faces[0];
var ims = img.size();

var diffX = Math.abs(200 - face.width);
var diffY = Math.abs(200 - face.height);

var im2 = img.roi(face.x, face.y, face.width, face.height)

im2.resize(200, 200);
im2.save(usersdir + '/' + id + '/' + photo)

trainSet.push([parseInt(id), im2]);

console.log('Image saved');
});
});
});

setTimeout(function(){
console.log('Training with: ' + photo);
},1000);
});

return resolve();
});
};


module.exports.bootstrap = function(cb) {
//fr.trainSync([[0, img], [1, img1], [2, img2]]);

var lower_threshold = [46, 57, 83];
var upper_threshold = [80, 96, 115];

train({
'0': 'ogy.jpg',
'1': 'ivan.jpg',
'2': 'user.jpg',
}).then(cb);

// It's very important to trigger this callback method when you are finished
// with the bootstrap! (otherwise your server will never lift, since it's waiting on the bootstrap)
cb();
//var whoisit = fr.predictSync(img4);
// console.log("Identified image", whoisit);
};
Loading

0 comments on commit 365d3ee

Please sign in to comment.