-
Notifications
You must be signed in to change notification settings - Fork 158
/
demo.js
260 lines (200 loc) · 6.59 KB
/
demo.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*globals $: true, getUserMedia: true, alert:true, ccv:true */
/*! getUserMedia demo - v1.0
* for use with https://github.com/addyosmani/getUserMedia.js
* Copyright (c) 2012 addyosmani; Licensed MIT */
(function () {
'use strict';
var App = {
init: function () {
// The shim requires options to be supplied for it's configuration,
// which can be found lower down in this file. Most of the below are
// demo specific and should be used for reference within this context
// only
if ( !!this.options ) {
this.pos = 0;
this.cam = null;
this.filter_on = false;
this.filter_id = 0;
this.canvas = document.getElementById("canvas");
this.ctx = this.canvas.getContext("2d");
this.img = new Image();
this.ctx.clearRect(0, 0, this.options.width, this.options.height);
this.image = this.ctx.getImageData(0, 0, this.options.width, this.options.height);
this.snapshotBtn = document.getElementById('takeSnapshot');
this.detectBtn = document.getElementById('detectFaces');
// Initialize getUserMedia with options
getUserMedia(this.options, this.success, this.deviceError);
// Initialize webcam options for fallback
window.webcam = this.options;
// Trigger a snapshot
this.addEvent('click', this.snapshotBtn, this.getSnapshot);
// Trigger face detection (using the glasses option)
this.addEvent('click', this.detectBtn, function () {
App.drawToCanvas('glasses');
});
} else {
alert('No options were supplied to the shim!');
}
},
addEvent: function (type, obj, fn) {
if (obj.attachEvent) {
obj['e' + type + fn] = fn;
obj[type + fn] = function () {
obj['e' + type + fn](window.event);
}
obj.attachEvent('on' + type, obj[type + fn]);
} else {
obj.addEventListener(type, fn, false);
}
},
// options contains the configuration information for the shim
// it allows us to specify the width and height of the video
// output we're working with, the location of the fallback swf,
// events that are triggered onCapture and onSave (for the fallback)
// and so on.
options: {
"audio": false, //OTHERWISE FF nightlxy throws an NOT IMPLEMENTED error
"video": true,
el: "webcam",
extern: null,
append: true,
// noFallback:true, use if you don't require a fallback
width: 320,
height: 240,
mode: "callback",
// callback | save | stream
swffile: "../dist/fallback/jscam_canvas_only.swf",
quality: 85,
context: "",
debug: function () {},
onCapture: function () {
window.webcam.save();
},
onTick: function () {},
onSave: function (data) {
var col = data.split(";"),
img = App.image,
tmp = null,
w = this.width,
h = this.height;
for (var i = 0; i < w; i++) {
tmp = parseInt(col[i], 10);
img.data[App.pos + 0] = (tmp >> 16) & 0xff;
img.data[App.pos + 1] = (tmp >> 8) & 0xff;
img.data[App.pos + 2] = tmp & 0xff;
img.data[App.pos + 3] = 0xff;
App.pos += 4;
}
if (App.pos >= 4 * w * h) {
App.ctx.putImageData(img, 0, 0);
App.pos = 0;
}
},
onLoad: function () {}
},
success: function (stream) {
if (App.options.context === 'webrtc') {
var video = App.options.videoEl;
if ((typeof MediaStream !== "undefined" && MediaStream !== null) && stream instanceof MediaStream) {
if (video.mozSrcObject !== undefined) { //FF18a
video.mozSrcObject = stream;
} else { //FF16a, 17a
video.src = stream;
}
return video.play();
} else {
var vendorURL = window.URL || window.webkitURL;
video.src = vendorURL ? vendorURL.createObjectURL(stream) : stream;
}
video.onerror = function () {
stream.stop();
streamError();
};
} else{
// flash context
}
},
deviceError: function (error) {
alert('No camera available.');
console.error('An error occurred: [CODE ' + error.code + ']');
},
changeFilter: function () {
if (this.filter_on) {
this.filter_id = (this.filter_id + 1) & 7;
}
},
getSnapshot: function () {
// If the current context is WebRTC/getUserMedia (something
// passed back from the shim to avoid doing further feature
// detection), we handle getting video/images for our canvas
// from our HTML5 <video> element.
if (App.options.context === 'webrtc') {
var video = document.getElementsByTagName('video')[0];
App.canvas.width = video.videoWidth;
App.canvas.height = video.videoHeight;
App.canvas.getContext('2d').drawImage(video, 0, 0);
// Otherwise, if the context is Flash, we ask the shim to
// directly call window.webcam, where our shim is located
// and ask it to capture for us.
} else if(App.options.context === 'flash'){
window.webcam.capture();
App.changeFilter();
}
else{
alert('No context was supplied to getSnapshot()');
}
},
drawToCanvas: function (effect) {
var source, glasses, canvas, ctx, pixels, i;
source = document.querySelector('#canvas');
glasses = new Image();
glasses.src = "js/glasses/i/glasses.png";
canvas = document.querySelector("#output");
ctx = canvas.getContext("2d");
ctx.drawImage(source, 0, 0, 520, 426);
pixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
// Hipstergram!
if (effect === 'hipster') {
for (i = 0; i < pixels.data.length; i = i + 4) {
pixels.data[i + 0] = pixels.data[i + 0] * 3;
pixels.data[i + 1] = pixels.data[i + 1] * 2;
pixels.data[i + 2] = pixels.data[i + 2] - 10;
}
ctx.putImageData(pixels, 0, 0);
}
// Green Screen
else if (effect === 'greenscreen') {
// Selectors
var rmin = $('#red input.min').val(),
gmin = $('#green input.min').val(),
bmin = $('#blue input.min').val(),
rmax = $('#red input.max').val(),
gmax = $('#green input.max').val(),
bmax = $('#blue input.max').val(),
green = 0, red = 0, blue = 0;
for (i = 0; i < pixels.data.length; i = i + 4) {
red = pixels.data[i + 0];
green = pixels.data[i + 1];
blue = pixels.data[i + 2];
alpha = pixels.data[i + 3];
if (red >= rmin && green >= gmin && blue >= bmin && red <= rmax && green <= gmax && blue <= bmax) {
pixels.data[i + 3] = 0;
}
}
ctx.putImageData(pixels, 0, 0);
} else if (effect === 'glasses') {
var comp = ccv.detect_objects({
"canvas": (canvas),
"cascade": cascade,
"interval": 5,
"min_neighbors": 1
});
// Draw glasses on everyone!
for (i = 0; i < comp.length; i++) {
ctx.drawImage(glasses, comp[i].x, comp[i].y, comp[i].width, comp[i].height);
}
}
}
};
App.init();
})();