inspirit / jsfeat

JavaScript Computer Vision library.
MIT License
2.74k stars 372 forks source link

Face recognition confidence #42

Closed 45kb closed 9 years ago

45kb commented 9 years ago

Hi, sorry for the issue, i am not understanding how to detect a face in a photo. This is the code i am using http://pastebin.com/WrphXW1N

Actually it returns the Object with all the infos (confidence, x, y etc..), but how do i check if the data matches a face? Is the confidence value telling me if it's a face or not?

I took this piece of code from the clmtrackr library, which seems to use jsfeat.

Any help appreciated, thank you!

inspirit commented 9 years ago

you are getting an array of rectangles with some properties that allow you to decide if face is good enough to further processing. you can also look at the demo here: https://github.com/inspirit/jsfeat/blob/gh-pages/sample_haar_face.html

45kb commented 9 years ago

@inspirit Hi, thank you for the support, i am actually in trouble cause the code seems ok but rects.length is always 0 i debugged but it seems i am missing something, any clue please?

  var canvas = $window.document.getElementById('canvas')
  , canvasWidth = canvas.width
  , canvasHeight = canvas.height
  , ctx = canvas.getContext('2d')
  , work_canvas = $window.document.createElement('canvas')
  , work_ctx = work_canvas.getContext('2d')
  , ii_sum = new Int32Array((canvasWidth+1)*(canvasHeight+1))
  , ii_sqsum = new Int32Array((canvasWidth+1)*(canvasHeight+1))
  , ii_tilted = new Int32Array((canvasWidth+1)*(canvasHeight+1))
  , img_u8 = new jsfeat.matrix_t(canvasWidth, canvasHeight, jsfeat.U8_t | jsfeat.C1_t)
  , classifier = jsfeat.haar.frontalface
  , imageData
  , rects;

  work_canvas.width = canvasWidth;
  work_canvas.height = canvasHeight;
  jsfeat.haar.edges_density = 0.13;

  work_ctx.drawImage(canvas, 0, 0);
  imageData = work_ctx.getImageData(0, 0, work_canvas.width, work_canvas.height);

  var anim = function () {

    jsfeat.imgproc.grayscale(imageData.data, work_canvas.width, work_canvas.height, img_u8);
    jsfeat.imgproc.equalize_histogram(img_u8, img_u8);
    jsfeat.imgproc.compute_integral_image(img_u8, ii_sum, ii_sqsum, classifier.tilted ? ii_tilted : null);

    rects = jsfeat.haar.detect_multi_scale(ii_sum,
     ii_sqsum,
     ii_tilted,
     null,
     img_u8.cols,
     img_u8.rows,
     classifier,
     1.15,
     2);

    rects = jsfeat.haar.group_rectangles(rects, 1);

    if (rects && rects.length === 0) {

      $window.requestAnimationFrame(anim);
    } else {

      console.log(rects);
    }
  }

  anim();
45kb commented 9 years ago

Sorry, it's my mistake, it works, i was using a wrong version of jsfeat i guess, sorry again :)

inspirit commented 9 years ago

Congratulations! :+1: