-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtual_background.html
335 lines (287 loc) · 10.7 KB
/
virtual_background.html
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<!DOCTYPE html>
<!--
* Copyright (c) 2020 Florin-Cătălin Tofan, All Rights Reserved.
*
* This project is inspired by:
* https://github.com/bensonruan/Selfie-Anywhere
* https://bensonruan.com/selfie-anywhere-person-segmentation-with-bodypix/
*
* This project includes codes from the following repositories:
*
* https://www.tensorflow.org/js/
* https://github.com/tensorflow/tfjs
* https://github.com/tensorflow/tfjs-models
* Apache-2.0 License
*
* NOKUBI Takatsugu
* https://github.com/knok/virtbg
* http://blog.daionet.gr.jp/knok-e/2020/05/10/virtual-background-using-webcam/
* Apache-2.0 License
*
* https://webrtc.github.io/samples/
* https://github.com/webrtc/samples
* BSD-style license
*
* Background image is from Pixabay
* https://pixabay.com/users/yeskay1211-6332528/
* License Free for commercial use
* No attribution required
* Author Yeskay1211
*
-->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="description" content="Virtual Background, Segmentation, Tensorflow, BodyPix, WebRTC, mediaRecorder, webm">
<meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1, maximum-scale=1">
<base target="_blank">
<link href="css/dropdown_hdiv.css" rel="stylesheet" type="text/css">
<!-- Load TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.2"></script>
<!-- Load BodyPix -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/body-pix@2.0"></script>
<style>
h1 {
text-align: center;
padding: 2rem;
}
#video,
#canvas {
border: 5px outset #000080;
align: center;
width: 640px;
height: 480px;
}
#video_out {
border: 5px outset red;
align: center;
width: 640px;
height: 480px;
margin-top: 10px;
}
.parent {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
hr {
width: 650px;
color: #000080;
}
.flex-container {
display:-webkit-flex;
display:flex;
-webkit-flex-direction:column;
flex-direction:column;
-webkit-justify-content:center;
justify-content:center;
-webkit-align-items:center;
align-items:center;
-webkit-align-content:space-around;
align-content:space-around;
}
#bcomutare, #brecord, #selimgs {
font-size: 20px;
margin: 5px;
padding: 5px;
border-width: 3px;
border-style: outset;
border-color: #000080;
border-radius: 10px;
}
</style>
</head>
<body>
<div id="centrat" class="colv">
<br>
<div class="dropdown colv_el">
<button id="selimgs" class="dropbtn">Select Backgrounds: </button>
<br>
<div class="dropdown-content colv_el">
<div><img onclick="selimg(this)" src= "bg/pixabay_hills.jpg" width="64" height="48"> Hills</div>
<div><img onclick="selimg(this)" src= "bg/pixabay_fall.jpg" width="64" height="48"> Fall</div>
<div><img onclick="selimg(this)" src= "bg/pixabay_landscape.jpg" width="64" height="48"> Landscape</div>
<div><img onclick="selimg(this)" src= "bg/pixabay_road.jpg" width="64" height="48"> Road</div>
<div><img onclick="selimg(this)" src= "bg/pixabay_forest.jpg" width="64" height="48"> Forest</div>
</div>
</div>
</div>
<script>
function selimg(image) {
document.getElementById('bg').src = image.src;
}
</script>
<div id ="container" class="flex-container">
<video id="camera" width="640" height="480" style="display: none"></video>
<div class="flex-item">
<button id="bcomutare" onclick="comutare();">Start Video</button>
<button id="brecord" onclick="inregistrare();" disabled>Start Recording</button>
</div>
<hr>
<canvas id="canvas" class="flex-item"></canvas>
<img id="bg" src="bg/pixabay_hills.jpg" style="display: none">
</div>
<script>
let localStream = null;
let localVideo = document.getElementById('camera');
let net = null;
let continueAnimation = false;
let animationId = null;
async function loadModel() {
net = await bodyPix.load({
architecture: 'MobileNetV1',
outputStride: 16,
multiplier: 0.50,
quantBytes: 2,
});
}
async function getImages() {
const img = await document.getElementById('camera');
const bg = document.getElementById('bg');
const cv_img = document.createElement('canvas');
const cv_bg = document.createElement('canvas');
cv_img.width = img.width;
cv_img.height = img.height;
const ctx_img = cv_img.getContext('2d');
ctx_img.drawImage(img, 0, 0);
cv_bg.width = img.width;
cv_bg.height = img.height;
const ctx_bg = cv_bg.getContext('2d');
ctx_bg.drawImage(bg, 0, 0, bg.width, bg.height, 0, 0, img.width, img.height);
const data_img = ctx_img.getImageData(0, 0, img.width, img.height);
const data_bg = ctx_bg.getImageData(0, 0, img.width, img.height);
return [img, bg, cv_img, cv_bg, ctx_img, ctx_bg, data_img, data_bg];
}
function drawToCanvas(canvas, segmentation, img, data_img, data_bg) {
const ctx = canvas.getContext("2d");
canvas.width = img.width;
canvas.height = img.height;
let width = img.width;
let height = img.height;
let imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
let pixels = imageData.data;
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
let base = (y * width + x) * 4;
let segbase = y * width + x;
if (segmentation.data[segbase] == 1) { // is fg
pixels[base + 0] = data_img.data[base + 0];
pixels[base + 1] = data_img.data[base + 1];
pixels[base + 2] = data_img.data[base + 2];
pixels[base + 3] = data_img.data[base + 3];
} else {
pixels[base + 0] = data_bg.data[base + 0];
pixels[base + 1] = data_bg.data[base + 1];
pixels[base + 2] = data_bg.data[base + 2];
pixels[base + 3] = data_bg.data[base + 3];
}
}
}
ctx.putImageData(imageData, 0, 0);
}
async function startVideoAnimation() {
await startVideo();
continueAnimation = true;
animationId = window.requestAnimationFrame(updateCanvas);
}
async function startPredict() {
await startVideo();
let [img, bg, cv_img, cv_bg, ctx_img, ctx_bg, data_img, data_bg] = await getImages();
const segmentation = await net.segmentPerson(img);
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext("2d");
drawToCanvas(canvas, segmentation, img, data_img, data_bg);
};
loadModel();
async function updateCanvas() {
let [img, bg, cv_img, cv_bg, ctx_img, ctx_bg, data_img, data_bg] = await getImages();
const segmentation = await net.segmentPerson(img);
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext("2d");
drawToCanvas(canvas, segmentation, img, data_img, data_bg);
if (continueAnimation) {
animationId = window.requestAnimationFrame(updateCanvas);
}
}
function stopVideo() {
continueAnimation = false;
localVideo.pause();
localVideo.srcObject = null;
if (localStream) {
localStream.getTracks().forEach(track => {
console.log('stop track:', track);
track.stop();
});
localStream = null;
}
}
async function startVideo() {
const mediaConst = { video: { width: 640, height: 480, frameRate: 25}, audio: false };
localStream = await navigator.mediaDevices.getUserMedia(mediaConst).catch(err => {
console.error('media error:', err);
return;
});
localVideo.srcObject = localStream;
await localVideo.play().catch(err => console.error('local play error:', err))
}
var chunks; // here we will store our recorded media chunks (Blobs)
var stream; // grab our canvas MediaStream
var rec; // init the recorder
function startRecording() {
chunks = []; // here we will store our recorded media chunks (Blobs)
stream = canvas.captureStream(); // grab our canvas MediaStream
rec = new MediaRecorder(stream); // init the recorder
// every time the recorder has new data, we will store it in our array
rec.ondataavailable = e => chunks.push(e.data);
rec.start();
// setTimeout(()=>rec.stop(), 3000); // stop recording in 3s
}
function stopRecording() {
// only when the recorder stops, we construct a complete Blob from all the chunks
rec.onstop = e => exportVid(new Blob(chunks, {type: 'video/webm'}));
rec.stop();
}
function exportVid(blob) {
const vid = document.createElement('video');
vid.style.display = "none";
vid.src = URL.createObjectURL(blob);
//vid.controls = true;
document.body.appendChild(vid);
const a = document.createElement('a');
a.download = 'myvid.webm';
a.href = vid.src;
// a.textContent = 'download the video';
document.body.appendChild(a);
a.click();
setTimeout(() => {
document.body.removeChild(a);
window.URL.revokeObjectURL(blob);
}, 500);
}
function inregistrare () {
if (document.getElementById('brecord').innerHTML === "Start Recording") {
document.getElementById('brecord').innerHTML = "Stop Recording";
startRecording();
document.getElementById('bcomutare').disabled = true;
} else {
stopRecording();
document.getElementById('brecord').innerHTML = "Start Recording";
document.getElementById('bcomutare').disabled = false;
}
};
function comutare() {
if (document.getElementById('bcomutare').innerHTML === "Start Video") {
document.getElementById('bcomutare').innerHTML = "Stop Video";
startVideoAnimation();
document.getElementById('brecord').disabled = false;
document.getElementById('selimgs').style.visibility = "hidden";
} else {
stopVideo();
document.getElementById('bcomutare').innerHTML = "Start Video";
document.getElementById('brecord').disabled = true;
document.getElementById('selimgs').style.visibility = "initial";
}
}
</script>
</body>
</html>