forked from riju/WebCamera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
317 lines (271 loc) · 11 KB
/
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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
let utils = new Utils('errorMessage');
let stats = null;
let controls = {};
let videoConstraint;
let streaming = false;
let videoTrack = null;
let video = document.getElementById('videoInput');
let canvasOutput = document.getElementById('canvasOutput');
let canvasInput = null;
let canvasInputCtx = null;
let src = null;
let background = null;
let dst = null;
// Camera parameters are not stable when the camera is just getting started.
// So we execute a loop where we capture background and use the last frame
// as a stable background.
const BACKGROUND_CAPTURE_ITERATIONS = 30;
// In OpenCV, Hue range is [0,179], Saturation range is [0,255]
// and Value range is [0,255]. We use ~0-33 range for blue color as default.
const LOWER_HUE_DEFAULT = 0;
const UPPER_HUE_DEFAULT = 33;
const LOWER_SATURATION_DEFAULT = 100;
const UPPER_SATURATION_DEFAULT = 255;
const LOWER_VALUE_DEFAULT = 7;
const UPPER_VALUE_DEFAULT = 255;
let HSVranges = {
lowerHue: LOWER_HUE_DEFAULT,
upperHue: UPPER_HUE_DEFAULT,
lowerSaturation: LOWER_SATURATION_DEFAULT,
lowerSaturation: UPPER_SATURATION_DEFAULT,
lowerValue: LOWER_VALUE_DEFAULT,
upperValue: UPPER_VALUE_DEFAULT,
}
function initOpencvObjects() {
src = new cv.Mat(video.height, video.width, cv.CV_8UC4);
background = new cv.Mat(video.height, video.width, cv.CV_8UC4);
dst = new cv.Mat();
}
function inRange(hsv, mask) {
let lowerRange = new cv.Mat(video.height, video.width, cv.CV_8UC3,
new cv.Scalar(HSVranges.lowerHue, HSVranges.lowerSaturation,
HSVranges.lowerValue, 255));
let upperRange = new cv.Mat(video.height, video.width, cv.CV_8UC3,
new cv.Scalar(HSVranges.upperHue, HSVranges.upperSaturation,
HSVranges.upperValue, 255));
cv.inRange(hsv, lowerRange, upperRange, mask);
lowerRange.delete(); upperRange.delete();
}
function deleteOpencvObjects() {
src.delete(); background.delete(); dst.delete();
}
function completeStyling() {
let cameraBar = document.querySelector('.camera-bar-wrapper');
cameraBar.style.width = `${video.width}px`;
document.getElementById('takePhotoButton').disabled = false;
// Extra canvas to get source image from video element
// (instead of cv.VideoCapture).
canvasInput = document.createElement('canvas');
canvasInput.width = video.width;
canvasInput.height = video.height;
canvasInputCtx = canvasInput.getContext('2d');
let resetButton = document.querySelector('.reset-button');
resetButton.style.left = `${video.videoWidth - resetButton.offsetWidth}px`;
resetButton.style.bottom = `${video.videoHeight}px`;
canvasOutput.width = video.width;
canvasOutput.height = video.height;
}
let backgroundCaptureCounter = 0;
function runCapturingRound() {
if (backgroundCaptureCounter < BACKGROUND_CAPTURE_ITERATIONS) {
++backgroundCaptureCounter;
canvasInputCtx.drawImage(video, 0, 0, video.width, video.height);
let imageData = canvasInputCtx.getImageData(0, 0, video.width, video.height);
background.data.set(imageData.data);
cv.imshow(canvasOutput, background);
requestAnimationFrame(runCapturingRound);
return;
}
backgroundCaptureCounter = 0;
document.getElementById("demoState").innerText = "Ready for magic! You may adjust color range below.";
requestAnimationFrame(processVideo);
}
function captureBackground() {
document.getElementById("demoState").innerText = "Capturing background...";
requestAnimationFrame(runCapturingRound);
}
function applyColorSegmentation(source, destination) {
let hsv = new cv.Mat(video.height, video.width, cv.CV_8UC3);
let mask = new cv.Mat();
let maskInv = new cv.Mat();
let sourceResult = new cv.Mat();
let backgroundResult = new cv.Mat();
// Convert source image to HSV color space.
// HSV - Hue (color information), Saturation (intensity), Value (brightness).
cv.cvtColor(source, hsv, cv.COLOR_BGR2HSV);
// Apply lower and upper boundaries of HSV channels to inRange filter.
inRange(hsv, mask);
// Dilation increases area of filtered object.
let kernel = cv.Mat.ones(3, 3, cv.CV_32F);
cv.morphologyEx(mask, mask, cv.MORPH_DILATE, kernel);
// Apply mask to background image.
cv.bitwise_and(background, background, sourceResult, mask);
// Create an inverted mask of a filtered object and apply it to source image.
cv.bitwise_not(mask, maskInv);
cv.bitwise_and(source, source, backgroundResult, maskInv);
// Combine source and background images.
cv.addWeighted(backgroundResult, 1, sourceResult, 1, 0, destination);
hsv.delete();
mask.delete(); maskInv.delete();
sourceResult.delete(); backgroundResult.delete();
}
function processVideo() {
try {
if (!streaming) {
cleanupAndStop();
return;
}
stats.begin();
canvasInputCtx.drawImage(video, 0, 0, video.width, video.height);
let imageData = canvasInputCtx.getImageData(0, 0, video.width, video.height);
src.data.set(imageData.data);
applyColorSegmentation(src, dst);
cv.imshow(canvasOutput, dst);
stats.end();
requestAnimationFrame(processVideo);
} catch (err) {
utils.printError(err);
}
};
function startCamera() {
if (!streaming) {
utils.clearError();
utils.startCamera(videoConstraint, 'videoInput', onVideoStartedCustom);
} else {
utils.stopCamera();
onVideoStopped();
}
}
function onVideoStartedCustom() {
streaming = true;
setMainCanvasProperties(video);
videoTrack = video.srcObject.getVideoTracks()[0];
document.getElementById('mainContent').classList.remove('hidden');
completeStyling();
initOpencvObjects();
// Capture background first.
requestAnimationFrame(captureBackground);
}
function cleanupAndStop() {
deleteOpencvObjects();
utils.stopCamera(); onVideoStopped();
}
function resetHSVsettings() {
document.getElementById('lowerHue').value =
document.getElementById('lowerHueOutput').value =
HSVranges.lowerHue = LOWER_HUE_DEFAULT;
document.getElementById('upperHue').value =
document.getElementById('upperHueOutput').value =
HSVranges.upperHue = UPPER_HUE_DEFAULT;
document.getElementById('lowerSaturation').value =
document.getElementById('lowerSaturationOutput').value =
HSVranges.lowerSaturation = LOWER_SATURATION_DEFAULT;
document.getElementById('upperSaturation').value =
document.getElementById('upperSaturationOutput').value =
HSVranges.upperSaturation = UPPER_SATURATION_DEFAULT;
document.getElementById('lowerValue').value =
document.getElementById('lowerValueOutput').value =
HSVranges.lowerValue = LOWER_VALUE_DEFAULT;
document.getElementById('upperValue').value =
document.getElementById('upperValueOutput').value =
HSVranges.upperValue = UPPER_VALUE_DEFAULT;
}
function initUI() {
let menuHeight = parseInt(getComputedStyle(
document.querySelector('.camera-bar-wrapper')).height);
getVideoConstraint(menuHeight);
initStats();
// Event for reset button.
let resetButton = document.querySelector('.reset-button');
resetButton.addEventListener('click', function () {
resetHSVsettings();
});
// Initialize values for HSV sliders.
resetHSVsettings();
// Event listeners for HSV sliders.
// Hue.
let lowerHueSlider = document.getElementById('lowerHue');
let lowerHueOutput = document.getElementById('lowerHueOutput');
lowerHueSlider.addEventListener('input', function () {
HSVranges.lowerHue = lowerHueOutput.value = parseInt(lowerHueSlider.value);
});
lowerHueSlider.addEventListener('change', function () {
HSVranges.lowerHue = lowerHueOutput.value = parseInt(lowerHueSlider.value);
});
let upperHueSlider = document.getElementById('upperHue');
let upperHueOutput = document.getElementById('upperHueOutput');
upperHueSlider.addEventListener('input', function () {
HSVranges.upperHue = upperHueOutput.value = parseInt(upperHueSlider.value);
});
upperHueSlider.addEventListener('change', function () {
HSVranges.upperHue = upperHueOutput.value = parseInt(upperHueSlider.value);
});
// Saturation.
let lowerSaturationSlider = document.getElementById('lowerSaturation');
let lowerSaturationOutput = document.getElementById('lowerSaturationOutput');
lowerSaturationSlider.addEventListener('input', function () {
HSVranges.lowerSaturation = lowerSaturationOutput.value =
parseInt(lowerSaturationSlider.value);
});
lowerSaturationSlider.addEventListener('change', function () {
HSVranges.lowerSaturation = lowerSaturationOutput.value =
parseInt(lowerSaturationSlider.value);
});
let upperSaturationSlider = document.getElementById('upperSaturation');
let upperSaturationOutput = document.getElementById('upperSaturationOutput');
upperSaturationSlider.addEventListener('input', function () {
HSVranges.upperSaturation = upperSaturationOutput.value =
parseInt(upperSaturationSlider.value);
});
upperSaturationSlider.addEventListener('change', function () {
HSVranges.upperSaturation = upperSaturationOutput.value =
parseInt(upperSaturationSlider.value);
});
// Value (brightness).
let lowerValueSlider = document.getElementById('lowerValue');
let lowerValueOutput = document.getElementById('lowerValueOutput');
lowerValueSlider.addEventListener('input', function () {
HSVranges.lowerValue = lowerValueOutput.value = parseInt(lowerValueSlider.value);
});
lowerValueSlider.addEventListener('change', function () {
HSVranges.lowerValue = lowerValueOutput.value = parseInt(lowerValueSlider.value);
});
let upperValueSlider = document.getElementById('upperValue');
let upperValueOutput = document.getElementById('upperValueOutput');
upperValueSlider.addEventListener('input', function () {
HSVranges.upperValue = upperValueOutput.value = parseInt(upperValueSlider.value);
});
upperValueSlider.addEventListener('change', function () {
HSVranges.upperValue = upperValueOutput.value = parseInt(upperValueSlider.value);
});
// TakePhoto event by clicking takePhotoButton.
let takePhotoButton = document.getElementById('takePhotoButton');
takePhotoButton.addEventListener('click', function () {
// Here we are not using takePhoto() per se.
// new ImageCapture(videoTrack) gives image without applied filter.
let dstCanvas = document.getElementById('gallery');
drawCanvas(dstCanvas, canvasOutput);
});
// TODO(sasha): move to utils.js.
let facingModeButton = document.getElementById('facingModeButton');
// Switch to face or environment mode by clicking facingModeButton.
facingModeButton.addEventListener('click', function () {
if (controls.facingMode == 'user') {
controls.facingMode = 'environment';
videoConstraint.deviceId = { exact: controls.backCamera.deviceId };
facingModeButton.innerText = 'camera_front';
} else if (controls.facingMode == 'environment') {
controls.facingMode = 'user';
videoConstraint.deviceId = { exact: controls.frontCamera.deviceId };
facingModeButton.innerText = 'camera_rear';
}
utils.clearError();
utils.stopCamera();
utils.startCamera(videoConstraint, 'videoInput', startVideoProcessing);
});
enableThreads();
}
utils.loadOpenCv(() => {
initUI();
initCameraSettingsAndStart();
});