Code Monkey home page Code Monkey logo

face.js's Introduction

Face.js

by Tomas Alvarez (http://twitter.com/alvarez_tomas)
Face.js is a OpenCV Haar algorithm implementation for Javascript, in-progress right now, which aims to provide a simple way 
to detect faces in a picture.

Installation

git clone git://github.com/talvarez/Face.js.git
node-waf configure build

Just check that you have installed the latest OpenCV stable tarball (currently 2.2).

Basic example

var Face = require('../build/default/face.node'),
detector = new Face.init();

detector.img = '/tmp/last.png';
console.log(detector.img);

detector.oncomplete = function(faces){

	console.log("I found " + faces.length + " faces");
	for(var i = 0; i < faces.length; i++) {
			console.log(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
	}

};

detector.run();

Complete example

var Face = require('../build/default/face.node'),
recognizer = new Face.init(),
Canvas = require('../deps/canvas/canvas.js'),
Image = Canvas.Image,
canvas = new Canvas(800, 600),
ctx = canvas.getContext('2d');


function boundFaces(input, faces, res) {
	var img = new Image;

	img.onload = function(){
			ctx.drawImage(img, 0, 0);
	}


	var threshold_false_smile = 15;
	img.src = input;

	for(var i = 0; i < faces.length; i++) {

			res.write('Intensity: '+faces[i].intensity+'<BR>');

			if(faces[i].smile) {
					if(faces[i].intensity > threshold_false_smile) { //Threshold possible false positives smiles
							ctx.strokeStyle = 'rgb(0, 255, 0)';
					} else {
							ctx.strokeStyle = 'rgb(255, 0, 0)';
					}
			} else {

					ctx.strokeStyle = 'rgb(255, 0, 0)';
			}

			ctx.strokeRect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
	}
}


function getFaces(res) {

	recognizer.img = './samples/frame5.png';
	recognizer.pathto = '../cascades/';

	recognizer.checkSmile = true;
	recognizer.minsize = 20;

	recognizer.oncomplete = function(faces){

			boundFaces(recognizer.img, faces, res);
			res.write("I found " + faces.length + " faces <BR>");
			res.end('<img src="' + canvas.toDataURL() + '" />');

	};

	recognizer.run();

}

var http = require('http');
http.createServer(function (req, res) {
			res.writeHead(200, { 'Content-Type': 'text/html' });
			res.write("Getting faces...<BR>");
			getFaces(res);

}).listen(3000);

console.log('Server started on port 3000');


Setting 'checkSmile' true, it will scan the previously detected face and search a possible smile gesture.
The faces object now includes a property, called 'smile' in case it's true another new property will be added to the object, called 'intensity'.
'intensity' property is used for control possible false positives smiles, maybe the detector detect a smile, but its wrong (this is common in computer
vision programs, because the detectors handles a lot of variables like, illumination, pose, size) so you can threshold it.
You can set a minimum, and a maximum too, size for a face, if you dont provide it, Face.js will set it to 20 for the min, and 0 for the max (0 means all the sizes in the maxsize property).

NOTE: It uses node-canvas, but is not necessary, I used it (and I include it in deps) just for draw rectangles in the faces areas.

Versions

Tested with:

- node 0.4.8
- opencv 2.2.0

License

(The MIT License)

Copyright (c) 2011 talvarez

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

face.js's People

Contributors

alvarezgarcia avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.