forked from liuliu/ccv
-
Notifications
You must be signed in to change notification settings - Fork 485
/
Copy pathjquery.facedetection.js
executable file
·123 lines (98 loc) · 3.83 KB
/
jquery.facedetection.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
FaceDetection jQuery Plugin
Copyright (c) 2016 Jay Salvat
*/
/* global $, ccv, cascade */
$.fn.faceDetection = function (settingsOrCallback) {
"use strict";
var time;
var options = {
interval: 4,
minNeighbors: 1,
grayscale: true,
confidence: null,
async: false,
complete: function () {}, // (faces)
error: function () {} // (code, message)
};
if ($.isFunction(settingsOrCallback)) {
options.complete = settingsOrCallback;
} else {
$.extend(options, settingsOrCallback);
}
return this.each(function() {
var $$ = $(this),
offset = $$.offset(),
position = $$.position(),
scaleX = ($$.width() / (this.naturalWidth || this.videoWidth )) || 1,
scaleY = ($$.height() / (this.naturalHeight || this.videoHeight)) || 1;
if (!$$.is('img, video, canvas')) {
options.error.apply($$, [ 1, 'Face detection is possible on images, videos and canvas only.' ]);
options.complete.apply($$, [ [] ]);
return;
}
function detect() {
var source, canvas;
time = new Date().getTime();
if ($$.is('img')) {
source = new Image();
source.src = $$.attr('src');
source.crossOrigin = $$.attr('crossorigin');
canvas = ccv.pre(source);
} else if ($$.is('video') || $$.is('canvas')) {
var copy, context;
source = $$[0];
copy = document.createElement('canvas');
copy.setAttribute('width', source.videoWidth || source.width);
copy.setAttribute('height', source.videoHeight || source.height);
context = copy.getContext("2d");
context.drawImage(source, 0, 0);
canvas = ccv.pre(copy);
}
if (options.grayscale) {
canvas = ccv.grayscale(canvas);
}
try {
if (options.async && window.Worker) {
ccv.detect_objects({
"canvas": canvas,
"cascade": cascade,
"interval": options.interval,
"min_neighbors": options.minNeighbors,
"worker": 1,
"async": true
})(done);
} else {
done(ccv.detect_objects({
"canvas": canvas,
"cascade": cascade,
"interval": options.interval,
"min_neighbors": options.minNeighbors
}));
}
} catch (e) {
options.error.apply($$, [ 2, e.message ]);
options.complete.apply($$, [ false ]);
}
}
function done(faces) {
var n = faces.length,
data = [];
for (var i = 0; i < n; ++i) {
if (options.confidence !== null && faces[i].confidence <= options.confidence) {
continue;
}
faces[i].positionX = position.left + faces[i].x;
faces[i].positionY = position.top + faces[i].y;
faces[i].offsetX = offset.left + faces[i].x;
faces[i].offsetY = offset.top + faces[i].y;
faces[i].scaleX = scaleX;
faces[i].scaleY = scaleY;
data.push(faces[i]);
}
data.time = new Date().getTime() - time;
options.complete.apply($$, [ data ]);
}
return detect();
});
};