-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
200 lines (164 loc) · 5.05 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
import * as bodyPix from '@tensorflow-models/body-pix';
import * as partColorScales from './color-scales';
const state = {
video : null,
stream: null,
net: null,
videoConstraints: {},
changingCamera: false,
changingArchitecture: false,
}
function isAndroid() {
return /Android/i.test(navigator.userAgent);
}
function isiOS() {
return /iPhone|iPad|iPod/i.test(navigator.userAgent);
}
function isMobile() {
return isAndroid() || isiOS();
}
async function getVideoInputs() {
if(!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
console.log('enumerateDevices() not supported');
return [];
}
const devices = await navigator.mediaDevices.enumerateDevices();
const videoDevices = devices.filter((device) => device.kind === 'videoinput');
return videoDevices;
}
function stopExistingVideoCapture() {
if (state.video && state.video.srcObject) {
state.video.srcObject.getTracks().forEach((track) => {
track.stop();
});
state.video.srcObject = null;
}
}
async function getDeviceForLabel(cameraLabel) {
const videoInputs = await getVideoInputs();
for (let i = 0; i < videoInputs.length; i++) {
const videoInput = videoInputs[i];
if (videoInput.label === cameraLabel) {
return videoInput.deviceId;
}
}
return null;
}
function getFacingMode(cameraLabel) {
if(!cameraLabel) {
return 'user';
}
if (cameraLabel.toLowerCase().includes('back')) {
return 'environment';
}
else {
return 'user';
}
}
async function getConstraints(cameraLabel) {
let deviceId, facingMode;
if (cameraLabel) {
deviceId = await getDeviceForLabel(cameraLabel);
facingMode = isMobile() ? getFacingMode(cameraLabel) : null;
};
return { deviceId, facingMode };
}
async function setupCamera(cameraLabel) {
if(!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
throw new Error(
'Browser API navigator.mediaDevices.getUserMedia not available'
);
}
const videoElement = document.getElementById('video');
stopExistingVideoCapture();
const videoConstraints = await getConstraints(cameraLabel);
const stream = await navigator.mediaDevices.getUserMedia(
{'audio': false, 'video': videoConstraints}
);
videoElement.srcObject = stream;
return new Promise((resolve) => {
videoElement.onloadedmetadata = () => {
videoElement.width = videoElement.videoWidth;
videoElement.height = videoElement.videoHeight;
resolve(videoElement);
};
});
}
async function loadVideo(cameraLabel) {
try {
state.video = await setupCamera(cameraLabel);
} catch (e) {
let info = document.getElementById('info');
info.textContent = 'this browser does not support video capture,' +
'or this device does not have a camera';
info.style.display = 'block';
throw e;
}
state.video.play();
}
const guiState = {
estimate: 'segmentation',
camera: null,
flipHorizontal: true,
input: {
mobileNetArchitecture: isMobile() ? '0.50' : '0.75',
outputStride: 16
},
segmentation: {
segmentationThreshold: 0.5,
effect: 'mask',
maskBackground: true,
opacity: 0.7,
backgroundBlurAmount: 3,
maskBlurAmount: 0,
edgeBlurAmount: 3,
},
partMap: {
colorScale: 'rainbow',
segmentationThreshold: 0.5,
applyPixelation: false,
opacity: 0.9,
},
showFps: false,
};
function segmentBodyInRealTime() {
const canvas = document.getElementById('output');
async function bodySegmentationFrame() {
if(state.changingArchitecture || state.changingCamera) {
setTimeout(bodySegmentationFrame, 1000);
return;
}
const outputStride = +guiState.input.outputStride;
const flipHorizontal = guiState.flipHorizontal;
const personSegmentation = await state.net.estimatePersonSegmentation(
state.video,
outputStride,
guiState.segmentation.segmentationThreshold
);
const mask = bodyPix.toMaskImageData(
personSegmentation, guiState.segmentation.maskBackground
);
bodyPix.drawMask(
canvas,
state.video,
mask,
guiState.segmentation.opacity,
guiState.segmentation.maskBlurAmount,
flipHorizontal
);
requestAnimationFrame(bodySegmentationFrame);
}
bodySegmentationFrame();
}
export async function bindPage() {
state.net = await bodyPix.load(+guiState.input.mobileNetArchitecture);
document.getElementById('loading').style.display = 'none';
document.getElementById('main').style.display = 'inline-block';
await loadVideo();
segmentBodyInRealTime();
}
navigator.getUserMedia =
navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia;
bindPage();