-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
42 lines (34 loc) · 1020 Bytes
/
index.js
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
34
35
36
37
38
39
40
41
42
const axios = require('axios')
const fs = require('fs')
const cv = require('opencv')
exports.detect = function(path, cb) {
if (!/^(f|ht)tps?:\/\//i.test(path)) {
cv.readImage(path, function(err, im) {
im.detectObject(cv.FACE_CASCADE, {}, function(err, faces) {
cb(faces.length)
});
});
} else {
var filename = path.replace(/[^a-z0-9]/gi, '_').toLowerCase();
axios({
method: 'get',
url: path,
responseType: 'stream'
})
.then(function(response) {
var stream = response.data.pipe(fs.createWriteStream(filename))
stream.on('finish', function() {
cv.readImage(filename, function(err, im) {
im.detectObject(cv.FACE_CASCADE, {}, function(err, faces) {
fs.unlink(filename, function(error) {
if (error) {
cb('Unable to delete file')
}
cb(faces.length)
})
});
});
});
});
}
}