-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
322 lines (286 loc) · 9.89 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no" />
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/camera_utils/camera_utils.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/selfie_segmentation/selfie_segmentation.js"
crossorigin="anonymous"></script>
<meta name="keywords" content="passport, photo, webcam, service, free, browser" />
<meta name="description" content="Take your own passport photo in the browser" />
<meta property="og:title" content="Webcam Passport Photo" />
<meta property="og:description" content="Take your own passport photo in the browser" />
<meta property="og:image" content="https://webcam-passport-photo.onrender.com/demo.jpg" />
<title>Webcam Passport Photo</title>
</head>
<style>
html,
body {
padding: 0;
margin: 0;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.canvas_container {
margin: 0 auto;
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
background-repeat: no-repeat;
background-position: center;
background-size: 100%;
}
.preview_image {
width: auto;
height: 75%;
box-shadow: 0px 1px 20px rgba(0, 0, 0, 0.3);
}
.controls {
position: absolute;
bottom: 0;
width: 100%;
padding: 10px 0 10px;
background-color: rgba(0, 0, 0, 0.3);
display: flex;
justify-content: space-around;
}
.controls button {
width: 75px;
height: 75px;
border-radius: 50%;
border: none;
box-shadow: 0px 1px 5px rgba(0, 0, 0, 0.3);
overflow: hidden;
background-color: white;
background-repeat: no-repeat;
background-position: center;
}
.start {
background-image: url(https://www.gstatic.com/images/icons/material/system_gm/2x/check_black_24dp.png);
}
.mirror {
background-image: url(https://www.gstatic.com/images/icons/material/system_gm/2x/swap_horiz_black_24dp.png);
}
.capture {
background-image: url(https://www.gstatic.com/images/icons/material/system_gm/2x/photo_camera_black_24dp.png);
}
.flip {
background-image: url(https://www.gstatic.com/images/icons/material/system_gm/2x/flip_camera_ios_black_24dp.png);
}
.retake {
background-image: url(https://www.gstatic.com/images/icons/material/system_gm/2x/arrow_back_black_24dp.png);
}
.download {
background-image: url(https://www.gstatic.com/images/icons/material/system_gm/2x/file_download_black_24dp.png);
}
.step-capture,
.step-download {
display: none;
}
</style>
<body>
<div class="canvas_container">
<div class="step-intro" style="padding: 0 10px 0 10px">
<h1>Webcam Passport Photo</h1>
<p>Take your own passport photo in the browser.</p>
<img src="./passport-mask.png"
style="float:right;border:5px solid white;box-shadow:0px 0px 3px rgba(0,0,0,0.3);" width="100"
height="100" />
<ul>
<li>No uploads</li>
<li>No downloads</li>
<li>No installs</li>
<li>No accounts</li>
<li>No tracking</li>
</ul>
<p>
This app respects your privacy. Webcam image is processed and saved locally.
</p>
<!-- <p style="border: 1px solid #ccc; background-color:#eee; padding:10px">
Let's begin. Click the checkmark below to activate your webcam.
</p> -->
</div>
<canvas class="output_canvas step-capture"></canvas>
<img class="preview_image step-download" />
</div>
<div class="controls">
<button class="start step-intro" title="Start"></button>
<button class="mirror step-capture" title="Mirror"></button>
<button class="capture step-capture" title="Capture"></button>
<button class="flip step-capture" title="Flip"></button>
<button class="meta_info step-download" style="visibility: hidden;"></button>
<button class="retake step-download" title="Retake"></button>
<button class="download step-download" title="Download"></button>
</div>
<script>
const canvasContainer = document.getElementsByClassName('canvas_container')[0];
const canvasElement = document.getElementsByClassName('output_canvas')[0];
const previewElement = document.getElementsByClassName('preview_image')[0];
const startEl = document.getElementsByClassName('start')[0];
const mirrorEl = document.getElementsByClassName('mirror')[0];
const flipEl = document.getElementsByClassName('flip')[0];
const captureEl = document.getElementsByClassName('capture')[0];
const retakeEl = document.getElementsByClassName('retake')[0];
const downloadEl = document.getElementsByClassName('download')[0];
const meta_info = document.getElementsByClassName('meta_info')[0];
const canvasCtx = canvasElement.getContext('2d');
const facingMode = ['user', 'environment'];
let camera, video, mirrored = false;
let facingModeIndex = 0;
let downloadFn;
let videoWidth, videoHeight, videoRatio;
let smallestWindowDimension = Math.min(window.innerWidth, window.innerHeight);
function hide(el) { el.style.display = 'none'; }
function show(el) { el.style.display = 'block'; }
startEl.addEventListener('click', () => {
start();
});
mirrorEl.addEventListener('click', () => {
if (mirrored) {
mirrored = false;
canvasElement.style.transform = 'none';
} else {
mirrored = true;
canvasElement.style.transform = 'scaleX(-1)';
}
});
flipEl.addEventListener('click', () => {
stopCamera();
facingModeIndex = (facingModeIndex + 1) % facingMode.length;
startCamera();
});
retakeEl.addEventListener('click', () => {
startCamera();
document.querySelectorAll('.step-download').forEach(hide);
document.querySelectorAll('.step-capture').forEach(show);
canvasContainer.style.backgroundImage = 'url(./passport-mask.png)';
downloadEl.removeEventListener('click', downloadFn);
});
captureEl.addEventListener('click', function () {
let seconds = 3;
let interval = 0;
captureEl.disabled = true;
const timer = document.createElement('div');
timer.style = `position:absolute;font-size:160px;color:white;top:${smallestWindowDimension / 2}px;margin-top:-80px;text-align:center;width:100%;`;
timer.textContent = seconds;
document.body.appendChild(timer);
const countdown = function () {
seconds -= 1;
timer.textContent = seconds;
if (seconds === 0) {
takePhoto();
window.clearInterval(interval);
timer.remove();
captureEl.disabled = false;
}
}
interval = window.setInterval(countdown, 1000);
});
window.onresize = () => {
smallestWindowDimension = Math.min(window.innerWidth, window.innerHeight);
resizeOutput();
}
function takePhoto() {
const outputSize = 600;
const img = new Image();
img.onload = function () {
const imageHeight = img.height;
const imageWidth = img.width;
canvasElement.height = outputSize;
canvasElement.width = outputSize;
canvasCtx.save(); // save canvas transform state before doing horizontal flip
canvasCtx.clearRect(0, 0, outputSize, outputSize); // reset canvas
canvasCtx.rect(0, 0, outputSize, outputSize); // paint white background so it's not transparent
canvasCtx.fillStyle = 'white';
canvasCtx.fill();
if (mirrored) {
canvasCtx.translate(outputSize, 0);
canvasCtx.scale(-1, 1);
}
canvasCtx.drawImage(img, (imageWidth - imageHeight) / 2, 0, imageHeight, imageHeight, 0, 0, outputSize, outputSize);
canvasCtx.restore();
const dataURL = canvasElement.toDataURL();
previewElement.src = dataURL;
resizeOutput();
document.querySelectorAll('.step-download').forEach(show);
document.querySelectorAll('.step-capture').forEach(hide);
canvasContainer.style.backgroundImage = 'none';
stopCamera();
downloadFn = function () { download(dataURL) };
downloadEl.addEventListener('click', downloadFn);
}
img.src = canvasElement.toDataURL();
}
function download(url) {
const a = document.createElement('a')
a.href = url;
a.download = 'webcam-passport-photo';
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
}
function resizeOutput() {
canvasElement.style.height = `${smallestWindowDimension}px`;
canvasElement.style.width = `${smallestWindowDimension * videoRatio}px`;
canvasContainer.style.width = `${smallestWindowDimension}px`;
canvasContainer.style.height = `${smallestWindowDimension}px`;
canvasElement.height = videoHeight;
canvasElement.width = videoWidth;
}
function onResults(results) {
canvasCtx.save();
canvasCtx.clearRect(0, 0, videoWidth, videoHeight);
canvasCtx.drawImage(results.segmentationMask, 0, 0,
videoWidth, videoHeight);
// Only overwrite existing pixels.
canvasCtx.globalCompositeOperation = 'source-in';
canvasCtx.drawImage(
results.image, 0, 0, videoWidth, videoHeight);
canvasCtx.restore();
}
const selfieSegmentation = new SelfieSegmentation({
locateFile: (file) => {
return `https://cdn.jsdelivr.net/npm/@mediapipe/selfie_segmentation/${file}`;
}
});
selfieSegmentation.setOptions({
selfieMode: true,
modelSelection: 0,
});
selfieSegmentation.onResults(onResults);
function startCamera() {
const mode = facingMode[facingModeIndex];
video = document.createElement('video');
video.setAttribute('playsinline', 'true');
let firstFrame = true;
camera = new Camera(video, {
onFrame: async () => {
if (firstFrame) {
firstFrame = false;
videoWidth = video.videoWidth;
videoHeight = video.videoHeight;
videoRatio = videoWidth / videoHeight;
canvasContainer.style.backgroundImage = 'url(./passport-mask.png)';
resizeOutput();
meta_info.textContent = `${mode}, ${video.videoWidth}, ${video.videoHeight}`;
}
await selfieSegmentation.send({ image: video });
},
facingMode: mode,
width: 720,
height: 720
});
camera.start();
}
function stopCamera() {
camera && camera.stop();
video && video.remove();
}
function start() {
startCamera();
document.querySelectorAll('.step-capture').forEach(show);
document.querySelectorAll('.step-intro').forEach(hide);
}
</script>
</body>
</html>