Face Detection with jQuery

By on  
jQuery Face Detection

I've always been intrigued by recognition software because I cannot imagine the logic that goes into all of the algorithms. Whether it's voice, face, or other types of detection, people look and sound so different, pictures are shot differently, and from different angles, I cannot fathom how it's all done. Since I already covered booby nudity detection with JavaScript, I thought it would be worth some time to explore face detection. Facebook uses it, so maybe it has application in your websites.

One face detection library I found is Face Detection by Jay Salvat and Liu Liu. This is a standard jQuery plugin that receives an image and returns an array of coordinates of faces found within the image. Let's have a look at how to use it!


Four JS files are required for this jQuery plugin:

<script src="jquery-1.4.3.min.js"></script>

<!-- mas js -->
<script src="facedetection/ccv.js"></script>
<script src="facedetection/face.js"></script>
<script src="jquery.facedetection.js"></script>

The faceDetection plugin wraps functionality within the first two JavaScript files, and returns an array of objects which represent the coordinates of the faces within the photo (if any are found). An example would be:

var coords = jQuery("#myImage").faceDetection();
/* Returns:
		x: 525
		y: 435,
		width: 144,
		height: 144,
		positionX: 532.6353328125226,
		positionY: 443.240976080536,
		offsetX: 532.6353328125226,
		offsetY: 443.240976080536,
		confidence: 12.93120119,
		neighbour: undefined,

You may also add event callbacks to every call:

var coords = jQuery("#myImage").faceDetection({
	complete: function(image, coords) {
		// Do something
	error: function() {
		console.warn("Could not process image");

It's up to you what you'd like to do when the faces have been found. You could add a square around the person's face:

jQuery("img").each(function() {
	var img = this;
	// Get faces cooridnates
	var coordinates = jQuery(img).faceDetection();
	// Make boxes if faces are found
	if(coordinates.length) {
		coordinates.forEach(function(coord) {
", { css: { position: "absolute", left: coord.positionX + 5 + "px", top: coord.positionY + 5 + "px", width: coord.width + "px", height: coord.height + "px", border: "3px solid white" } }).appendTo(img.parentNode); }); } });

There's not much more to it than that!

I tried to vary the photos I used faceDetection on and as I expected, the results are the not perfect. They are, however, quite good; no software will be perfect for all cases. The software also does not do facial comparison, so you would need to provide suggestions as to the face's identity via another method. For what this plugin is meant to do, however, it does pretty well. I encourage you to give this a try!

Track.js Error Reporting

Recent Features

  • From Webcam to Animated GIF: the Secret Behind chat.meatspac.es!

    My team mate Edna Piranha is not only an awesome hacker; she's also a fantastic philosopher! Communication and online interactions is a subject that has kept her mind busy for a long time, and it has also resulted in a bunch of interesting experimental projects...

  • Send Text Messages with PHP

    Kids these days, I tell ya.  All they care about is the technology.  The video games.  The bottled water.  Oh, and the texting, always the texting.  Back in my day, all we had was...OK, I had all of these things too.  But I still don't get...

Incredible Demos

  • MooTools Flashlight Effect

    Another reason that I love Twitter so much is that I'm able to check out what fellow developers think is interesting. Chris Coyier posted about a flashlight effect he found built with jQuery. While I agree with Chris that it's a little corny, it...

  • Sexy Album Art with MooTools or jQuery

    The way that album information displays is usually insanely boring. Music is supposed to be fun and moving, right? Luckily MooTools and jQuery allow us to communicate that creativity on the web. The XHTML A few structure DIVs and the album information. The CSS The CSS...


  1. Alex

    iPad almost burned when browsing the demo page.

    • Yeah, that will happen with client-side technology like this. In practice, this is better done on the server side, but it’s still fascinating to see on the client side.

  2. Rasmus Fløe

    The perfect companion article would be about webworkers :D

  3. Yep latest FF got an slow/unresponsive script error while loading the demo. Interesting though!

  4. Most facial recognition software is done with neural networks….the varied results are because of a limited data set used in training….in theory they can be nearly as good as people at recognizing faces (same technology for voice recognition, OCR (Object Character Recognition), etc.)..it’s essentially pattern matching. I haven’t seen any good neural networks for javascript though, so this is pretty interesting to me.

  5. It would be great if the software could also recognize if people wear glasses, ear rings, wrinkles, et cetera. So that you would be able to apply filters on the image library and make selections.

  6. Nice work but Slow Script Error on FF

  7. David

    This is done somewhat quite easy using matlab.
    Too bad that my signal processing teacher was a bitch and I did not learn anything…

    • andre

      yeah and god accidentally forgot to give you brain

  8. foluso

    any thing on global browser detection am having battles with site displaying on other browsers as it did on my browser

  9. Tarlenan

    This website http://blog.geotitles.com/2011/08/facebook-like-face-detect

    had made a similar tutorial using the same jQuery pluign by Jay Salvat, but they have presented many examples on using the plugin.

    May be it is useful for your visitors.

  10. thank you for sharing, this is realy one of the complex jQuery plugins, but how the f**k they do it ?? have any one a document of the algorithm ?
    Thank you.

  11. serkan

    Can you add sound output in your face detection program.. it say your name..

  12. Hi i would like to recognize only one face from the group photo as well from the single image photo too. So what should i do in the code.

    It would be great help if anyone say the solution.

    Thanks in advance.

  13. Rufus

    It works great but when there is no face to detect in a picture it does not trigger the error function, but only the “complete” function.

    It does trigger the error function when the pic is below a certain size however.

    I tried checking for undefined (and tried null too) in the array like so:

    if (typeof array[x] === "undefined") {

    but no dice.

    Any suggestions?

    I am developing a chrome app so only testing there.

  14. Nice Tutorial David.. :) you are really rocking.. please share more tutorials on jQuery..

  15. Hi David,

    The demo no longer works (in Chrome) because you can no longer embed javascript from raw.github.com .


  16. Vikram

    Hi David Walsh,
    I am getting the error ‘ this image is invalid’. Can you hint how to solve this problem.

    • Vikram

      (when I tried it locally and on localhost)

  17. Justin O'Neill

    Hi David,
    This doesn’t seem to be working in Chrome at all and in FireFox is spits an error back at me. It did manage to pop out a white border box, however it wasn’t on the face.

  18. Man

    Man.. hotlinking from github has been disabled. Now your demo doesnt work. Can you update your demo? Cheers!

  19. perfo

    hello, all.
    How could I do this but without displaying the video on the screen ?
    I want to detect if someone is in front of the camera and roughly where they are but I don’t need to video to be displayed. Any hints ?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!

Recently on David Walsh Blog

  • OSCON Portland:  Conference  Discount!

    O'Reilly puts on the best web industry conferences in the world.  These conferences include Fluent Conference, Velocity Conference, and the upcoming OSCON in Portland, Oregon from July 20-24.  Open Source Convention (OSCON) is a conference that focuses specifically on open source developers and the tools and possibilities...

  • Follow Redirects with cURL

    I love playing around with cURL. There's something about loading websites via command line that makes me feel like some type of smug hacker, just like tweeting from command line does. I recently cURL'd the Google homepage and saw the following: I found it weird that Google...

  • Developers Have WordPress, Amateurs Have Squarespace, Professional Designers Have the NEW Webydo!

    Web design platforms have traditionally come in one of two varieties. There are the solutions like WordPress and Drupal that are incredibly powerful, but an understanding of web development and coding is required to be able to use those platforms effectively. On the other side of the...

  • Chris Coyierâs Favorite CodePen Demos II

    Hey everyone! Before we get started, I just want to say it’s damn hard to pick this few favorites on CodePen. Not because, as a co-founder of CodePen, I feel like a dad picking which kid he likes best (RUDE). But because there is just so...

  • GSAP + SVG For Power Users: Motion Along A Path

    Now that the GreenSock API is picking up steam, there are many tutorials and Getting Started guides out there to provide good introductions to the library, not to mention GreenSock’s own Forum and Documentation. This article isn’t intended for beginners, but rather a...