Ask Your Question
1

how to compare detected face with image

asked 2020-05-29 07:25:09 -0600

MAE_sikki gravatar image

using opens.js able to detect the face from video source but how to identify image from array of blobs

    let width = vidIn.videoWidth;cvsOut.width = vidIn.width = width;
    let height = vidIn.videoHeight;cvsOut.height = vidIn.height = height;
    let cap = new cv.VideoCapture(vidIn);
    let src = new cv.Mat(height,width,cv.CV_8UC4);
    let dst = new cv.Mat(height,width,cv.CV_8UC4);
    let gray = new cv.Mat();
    let classifier = new cv.CascadeClassifier();
    let fps = 24;
    let faces = new cv.RectVector();
    let msize = new cv.Size(0,0);
    const utils = new Utils('error message');
    let faceCascadeFile = 'haarcascade_frontalface_default.xml';
    utils.createFileFromUrl(faceCascadeFile,faceCascadeFile,()=>{
        console.log('cascade loaded');
        classifier.load(faceCascadeFile);
        let begin = Date.now();
        function processVideo(){
            cap.read(src);
            src.copyTo(dst);
            cv.cvtColor(dst, gray, cv.COLOR_RGBA2GRAY, 0);
            //detect face
            try{
                classifier.detectMultiScale(gray, faces, 1.1, 3, 0,msize,msize);
                console.dir(faceCascadeFile,faces.size());
            }catch(err){
                console.error('detectMultiScale error: ',err);
            }

            // draw face
            for (let i = 0; i < faces.size(); ++i) {
                let face = faces.get(i);
                let point1 = new cv.Point(face.x, face.y);
                let point2 = new cv.Point(face.x + face.width, face.y + face.height);
                cv.rectangle(dst, point1, point2, [255, 0, 0, 255]);
            }
            // output to canvas
            cv.imshow(cvsOut,dst);
            let delay = 1000/fps -(Date.now()-begin);
            setTimeout(processVideo,delay);
        }
        setTimeout(processVideo,0);
    });
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2020-05-31 03:18:23 -0600

berak gravatar image

there is a nice js face detection / recognition sample here , please take a look, i'd propose, you use the pretrained openface dnn model for recognition, like this:

// once, on startup:
function loadModel(callback) {
  var utils = new Utils('');
  var recognModel = 'https://raw.githubusercontent.com/pyannote/pyannote-data/master/openface.nn4.small2.v1.t7';
      utils.createFileFromUrl('face_recognition.t7', recognModel, () => {
        document.getElementById('status').innerHTML = '';
        netDet = cv.readNetFromCaffe('face_detector.prototxt', 'face_detector.caffemodel');
        netRecogn = cv.readNetFromTorch('face_recognition.t7');
        callback();
      });
    });
  });
};

// for each (cropped face) image:
//! [Get 128 floating points feature vector]
function face2vec(face) {
  var blob = cv.blobFromImage(face, 1.0 / 255, {width: 96, height: 96}, [0, 0, 0, 0], true, false)
  netRecogn.setInput(blob);
  var vec = netRecogn.forward();
  blob.delete();
  return vec;
};

then compare distance between feature vectors using cv.norm(), cosine distance or even a simple dot product (smaller==better)

(please also check out the dnn based face detection used in the sample, it's much faster and more rubust than the cascades you're using now !)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-05-29 07:25:09 -0600

Seen: 830 times

Last updated: May 31 '20