File: face-proxy.js

package info (click to toggle)
node-opencv 6.0.0%2Bgit20180416.cfc96ba0-3
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 24,632 kB
  • sloc: xml: 476,707; cpp: 5,950; makefile: 114; sh: 59; ansic: 20
file content (33 lines) | stat: -rwxr-xr-x 1,134 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Face recognition proxy
var http = require('http'),
    request = require('request'),
    cv = require('../lib/opencv');

var server = http.createServer(function(req, resp){
  var url = req.url.slice(1);
  request({uri:url, encoding:'binary'}, function(err, r, body){
    if (err) return resp.end(err.stack);
    if (!/image\//.test(r.headers['content-type'])) return resp.end('Not an image');

    cv.readImage(new Buffer(body, 'binary'), function(err, im){
      if (err) return resp.end(err.stack);
      if (im.width() < 1 || im.height() < 1) return resp.end('Image has no size');

      im.detectObject('../data/haarcascade_frontalface_alt.xml', {}, function(err, faces) {
        if (err) return resp.end(err.stack);

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

        resp.writeHead(200, {'Content-Type': 'image/jpeg'});
        resp.end(im.toBuffer());
      });
    });
  });

})


//server.listen(3000, function(){ console.log('Listening on http://localhost:3000'); })