forked from joidegn/mustachio-chrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.objectdetect.js
102 lines (84 loc) · 3.44 KB
/
jquery.objectdetect.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
(function($) {
var methods = {
init: function() {
return this.each(function() {
var canvas = document.createElement("canvas"),
context = canvas.getContext("2d");
$(this).data("objectdetect", {canvas: canvas, context: context});
});
},
destroy: function() {
return this.each(function() {
$(this).removeData("objectdetect");
});
},
all: function(options, callback) {
options = $.extend({}, $.fn.objectdetect.options, options);
if (!options.classifier) throw "jQuery.objectdetect cannot operate without specified object classifier";
return this.each(function() {
var $this = $(this),
data = $this.data("objectdetect");
if (!data) {
methods.init.apply($this, options);
data = $this.data("objectdetect");
}
if (!options.selection) {
options.selection = this.videoWidth ? [0, 0, this.videoWidth, this.videoHeight] : [0, 0, $this.width(), $this.height()];
}
var canvasWidth = data.canvas.width = ~~(options.size * options.selection[2] / options.selection[3]),
canvasHeight = data.canvas.height = ~~(options.size),
canvasWidthRatio = options.selection[2] / canvasWidth,
canvasHeightRatio = options.selection[3] / canvasHeight,
gray = data.gray,
sat = data.sat,
rsat = data.rsat,
ssat = data.ssat;
if (gray && canvasWidth * canvasHeight != gray.length) {
gray = sat = rsat = ssat = null;
}
data.context.drawImage(this, options.selection[0], options.selection[1], options.selection[2], options.selection[3], 0, 0, canvasWidth, canvasHeight);
imageData = data.context.getImageData(0, 0, canvasWidth, canvasHeight);
gray = objectdetect.convertRgbaToGrayscale(imageData.data, gray);
objectdetect.equalizeHistogram(gray);
sat = objectdetect.computeSat(gray, canvasWidth, canvasHeight, sat);
ssat = objectdetect.computeSquaredSat(gray, canvasWidth, canvasHeight, ssat);
if (options.classifier.tilted) rsat = objectdetect.computeRsat(gray, canvasWidth, canvasHeight, rsat);
var rects = objectdetect.detectMultiScale(sat, rsat, ssat, undefined, canvasWidth, canvasHeight, options.classifier, options.scaleFactor, options.scaleMin);
rects = objectdetect.groupRectangles(rects, 1).sort(function(rect) {return rect[4];});
for (var i = rects.length - 1; i >= 0; --i) {
rects[i][0] = ~~(rects[i][0] * canvasWidthRatio) + options.selection[0];
rects[i][1] = ~~(rects[i][1] * canvasHeightRatio) + options.selection[1];
rects[i][2] = ~~(rects[i][2] * canvasWidthRatio);
rects[i][3] = ~~(rects[i][3] * canvasHeightRatio);
}
if (options.cache) {
data.gray = gray;
data.sat = sat;
data.rsat = rsat;
data.ssat = ssat;
$this.data("objectdetect", data);
}
if (typeof callback == "function") {
callback.call(this, rects);
}
});
}
};
$.fn.objectdetect = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === "object" || !method ) {
return methods.init.apply(this, arguments);
} else {
$.error("Method " + method + " does not exist on jQuery.objectdetect");
}
};
$.fn.objectdetect.options = {
size: 200,
scaleFactor: 1.2,
scaleMin: 1,
classifier: null,
selection: null,
cache: true
};
})(jQuery);