-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove.html
More file actions
164 lines (133 loc) · 4.95 KB
/
Copy pathremove.html
File metadata and controls
164 lines (133 loc) · 4.95 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Remove background</title>
<link rel="stylesheet" type="text/css" href="main.css" />
<style>
#loading progress {
width: 16em;
margin-top: 0.5em;
}
#result {
max-width: 100%;
background-image:
conic-gradient(#bbb 25%, transparent 0 50%, #bbb 0 75%, transparent 0);
background-size: 1.25em 1.25em;
background-repeat: repeat;
}
[hidden] {
display: none !important;
}
</style>
</head>
<body>
<header>
<h1><a href="index.html">Tools</a></h1>
</header>
<main>
<header class="bar">
<h2>Remove background</h2>
</header>
<div class="placeholder-text" id="loading">
<p id="loading-label">Loading model...</p>
<progress id="loading-bar" value="0" max="100" hidden></progress>
</div>
<input type="file" id="file" accept="image/*" hidden />
<div class="placeholder-text" id="status" hidden>
<p>Removing background...</p>
</div>
<header class="bar" id="heading" hidden>
<h3>Result</h3>
<div class="group">
<button id="download" type="button">Download</button>
</div>
</header>
<img id="result" hidden alt="Result" />
</main>
<script type="module">
import { AutoModel, AutoProcessor, RawImage, env } from '/vendor/transformers.js';
env.backends.onnx.wasm.wasmPaths = '/vendor/';
// Load only the vendored weights, never reach out to the HuggingFace.
env.allowLocalModels = true;
env.allowRemoteModels = false;
env.localModelPath = '/vendor/models/';
const MODEL = 'briaai/RMBG-1.4';
const loading = document.getElementById('loading');
const loadingLabel = document.getElementById('loading-label');
const loadingBar = document.getElementById('loading-bar');
const status = document.getElementById('status');
const heading = document.getElementById('heading');
const result = document.getElementById('result');
const file = document.getElementById('file');
const download = document.getElementById('download');
let model = null;
let processor = null;
async function loadModel() {
const fileProgress = {};
const onProgress = ({ status, file, progress }) => {
if (status != 'progress' || !file) return;
loadingBar.hidden = false;
fileProgress[file] = progress;
const values = Object.values(fileProgress);
loadingBar.value = values.reduce((a, b) => a + b, 0) / values.length;
};
model = await AutoModel.from_pretrained(MODEL, {
config: { model_type: 'custom' },
dtype: 'q8', // keep in sync with the vendored variant in the Bakefile
progress_callback: onProgress,
});
// RMBG-1.4 ships no preprocessor config,
// but it is being written by `bake vendor` for *reasons*.
processor = await AutoProcessor.from_pretrained(MODEL, {
progress_callback: onProgress,
});
loading.hidden = true;
file.hidden = false;
}
async function removeBackground(url) {
heading.hidden = true;
result.hidden = true;
status.hidden = false;
file.hidden = true;
const image = await RawImage.fromURL(url);
const { pixel_values } = await processor(image);
const { output } = await model({ input: pixel_values });
const mask = await RawImage.fromTensor(
output[0].mul(255).to('uint8')
).resize(image.width, image.height);
const canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(image.toCanvas(), 0, 0);
const pixels = ctx.getImageData(0, 0, image.width, image.height);
for (let i = 0; i < mask.data.length; ++i)
pixels.data[4 * i + 3] = mask.data[i];
ctx.putImageData(pixels, 0, 0);
result.src = canvas.toDataURL('image/png');
heading.hidden = false;
result.hidden = false;
status.hidden = true;
file.value = '';
file.hidden = false;
}
function handleFile(f) {
if (!f || !f.type.startsWith('image/')) return;
const url = URL.createObjectURL(f);
removeBackground(url).finally(() => URL.revokeObjectURL(url));
}
file.onchange = () => handleFile(file.files[0]);
download.onclick = () => {
const a = document.createElement('a');
a.href = result.src;
a.download = 'cutout.png';
a.click();
};
loadModel();
</script>
<script src="/vendor/instant-page.js" type="module"></script>
</body>
</html>