Koko2011 commited on
Commit
06f05e5
·
verified ·
1 Parent(s): ee0b410

Upload 2 files

Browse files
Files changed (2) hide show
  1. index.html +360 -0
  2. model.onnx +3 -0
index.html ADDED
@@ -0,0 +1,360 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>advanced ai image enhancer</title>
7
+ <script src="https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.min.js"></script>
8
+ <style>
9
+ :root {
10
+ --bg-color: #121212; --surface-color: #1e1e1e; --primary-color: #03dac6;
11
+ --on-primary-color: #000000; --text-color: #e0e0e0; --border-color: #333333;
12
+ }
13
+ body {
14
+ font-family: -apple-system, blinkmacsystemfont, "segoe ui", roboto, helvetica, arial, sans-serif;
15
+ background-color: var(--bg-color); color: var(--text-color); margin: 0;
16
+ display: flex; flex-direction: column; align-items: center; justify-content: center;
17
+ min-height: 100vh; padding: 20px; box-sizing: border-box;
18
+ }
19
+ h1 { color: var(--primary-color); text-align: center; }
20
+ .container {
21
+ width: 100%; max-width: 900px; background-color: var(--surface-color);
22
+ border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,0.2);
23
+ padding: 2rem; box-sizing: border-box;
24
+ }
25
+ .app-state { display: none; }
26
+ body.state-loading #loadingstate, body.state-upload #uploadstate,
27
+ body.state-pre-process #preprocessstate, body.state-processing #processingstate,
28
+ body.state-results #resultsstate { display: block; }
29
+ #loadingstate, #processingstate { text-align: center; }
30
+ .spinner {
31
+ border: 4px solid rgba(255, 255, 255, 0.2); border-left-color: var(--primary-color);
32
+ border-radius: 50%; width: 40px; height: 40px;
33
+ animation: spin 1s linear infinite; margin: 20px auto;
34
+ }
35
+ @keyframes spin { to { transform: rotate(360deg); } }
36
+ #drop-area {
37
+ border: 2px dashed var(--border-color); border-radius: 8px; padding: 40px;
38
+ text-align: center; transition: background-color 0.2s, border-color 0.2s; cursor: pointer;
39
+ }
40
+ #drop-area.highlight { background-color: rgba(3, 218, 198, 0.1); border-color: var(--primary-color); }
41
+ #drop-area p { margin: 0; font-size: 1.2rem; }
42
+ .btn {
43
+ display: inline-block; background-color: var(--primary-color); color: var(--on-primary-color);
44
+ padding: 12px 24px; border-radius: 6px; border: none; font-weight: bold;
45
+ margin-top: 20px; cursor: pointer; transition: opacity 0.2s;
46
+ }
47
+ .btn:hover { opacity: 0.9; }
48
+ #fileelem { display: none; }
49
+ #progressbarcontainer { width: 100%; background-color: var(--border-color); border-radius: 4px; overflow: hidden; margin-top: 20px; }
50
+ #progressbar { width: 0%; height: 20px; background-color: var(--primary-color); transition: width 0.3s ease-in-out; }
51
+ .comparison-container { position: relative; width: 100%; overflow: hidden; border-radius: 8px; }
52
+ .comparison-container canvas { display: block; width: 100%; height: auto; }
53
+ #outputcanvas { position: absolute; top: 0; left: 0; clip-path: polygon(0 0, 50% 0, 50% 100%, 0 100%); }
54
+ .slider { position: absolute; top: 0; left: 50%; width: 4px; height: 100%; background-color: rgba(255, 255, 255, 0.7); cursor: ew-resize; transform: translatex(-50%); }
55
+ .slider-handle { position: absolute; top: 50%; left: 50%; width: 40px; height: 40px; border: 2px solid white; border-radius: 50%; background-color: var(--primary-color); transform: translate(-50%, -50%); display: flex; align-items: center; justify-content: space-evenly; }
56
+ .slider-handle::before, .slider-handle::after { content: ''; width: 0; height: 0; border-top: 6px solid transparent; border-bottom: 6px solid transparent; }
57
+ .slider-handle::before { border-right: 8px solid var(--on-primary-color); } .slider-handle::after { border-left: 8px solid var(--on-primary-color); }
58
+ .controls { margin-top: 20px; display: flex; justify-content: center; gap: 15px; flex-wrap: wrap; }
59
+ #preprocessstate canvas { max-width: 100%; border-radius: 8px; margin-top: 15px; }
60
+ .blur-controls { display: flex; flex-direction: column; align-items: center; gap: 10px; margin-top: 20px; }
61
+ </style>
62
+ </head>
63
+ <body class="state-loading">
64
+
65
+ <div class="container">
66
+ <h1>ai image enhancer</h1>
67
+
68
+ <div id="loadingstate" class="app-state"><p>loading ai model...</p><div class="spinner"></div><p id="modelerror" style="color: #cf6679; display: none;"></p></div>
69
+
70
+ <div id="uploadstate" class="app-state">
71
+ <input type="file" id="fileelem" accept="image/*">
72
+ <div id="drop-area"><p>drag & drop image here</p><p>or</p><label for="fileelem" class="btn">choose a file</label></div>
73
+ </div>
74
+
75
+ <div id="preprocessstate" class="app-state">
76
+ <p>your uploaded image:</p>
77
+ <canvas id="preprocesscanvas"></canvas>
78
+ <div class="blur-controls">
79
+ <label for="blurslider">blur radius: <span id="blurvalue">0</span>px</label>
80
+ <input type="range" id="blurslider" min="0" max="10" value="0" step="0.1" style="width: 80%;">
81
+ </div>
82
+ <div class="controls">
83
+ <button id="enhanceblurredbtn" class="btn">enhance blurred image</button>
84
+ <button id="enhanceoriginalbtn" class="btn">enhance original</button>
85
+ </div>
86
+ </div>
87
+
88
+ <div id="processingstate" class="app-state"><p>enhancing image, please wait...</p><div id="progressbarcontainer"><div id="progressbar"></div></div><p id="progresstext">0%</p></div>
89
+
90
+ <div id="resultsstate" class="app-state">
91
+ <p style="text-align:center;">slide to compare before vs. after</p>
92
+ <div class="comparison-container" id="comparisoncontainer"><canvas id="inputcanvas"></canvas><canvas id="outputcanvas"></canvas><div class="slider" id="slider"><div class="slider-handle"></div></div></div>
93
+ <div class="controls"><button id="downloadbtn" class="btn">download enhanced image</button><button id="resetbtn" class="btn">enhance another</button></div>
94
+ </div>
95
+ </div>
96
+
97
+ <script>
98
+ const tile_size = 256;
99
+ const overlap_size = 32; // Pixels to overlap on each side
100
+ const effective_tile_size = tile_size - 2 * overlap_size; // The non-overlapping part
101
+ const dom = {
102
+ body: document.body, droparea: document.getElementById('drop-area'), fileelem: document.getElementById('fileelem'),
103
+ progressbar: document.getElementById('progressbar'), progresstext: document.getElementById('progresstext'),
104
+ modelerrortext: document.getElementById('modelerror'), inputcanvas: document.getElementById('inputcanvas'),
105
+ outputcanvas: document.getElementById('outputcanvas'), comparisoncontainer: document.getElementById('comparisoncontainer'),
106
+ slider: document.getElementById('slider'), downloadbtn: document.getElementById('downloadbtn'), resetbtn: document.getElementById('resetbtn'),
107
+ preprocesscanvas: document.getElementById('preprocesscanvas'), blurslider: document.getElementById('blurslider'),
108
+ blurvalue: document.getElementById('blurvalue'), enhanceblurredbtn: document.getElementById('enhanceblurredbtn'),
109
+ enhanceoriginalbtn: document.getElementById('enhanceoriginalbtn')
110
+ };
111
+ let session, originalimage;
112
+
113
+ function setappstate(state) { dom.body.className = `state-${state}`; }
114
+
115
+ async function initmodel() {
116
+ try {
117
+ session = await ort.InferenceSession.create("./model.onnx");
118
+ setappstate('upload');
119
+ } catch (error) {
120
+ dom.modelerrortext.textContent = `Error: Failed to load model. Make sure 'model.onnx' is in the same directory as this HTML file and you are running a local server. Details: ${error.message}`;
121
+ dom.modelerrortext.style.display = 'block';
122
+ }
123
+ }
124
+
125
+ function getimagetensor(ctx, x, y, width = tile_size, height = tile_size) {
126
+ const imageData = ctx.getImageData(x, y, width, height);
127
+ const { data } = imageData;
128
+ const float32Data = new Float32Array(3 * width * height);
129
+ for (let i = 0; i < width * height; i++) {
130
+ float32Data[i] = data[i * 4] / 255.0;
131
+ float32Data[i + width * height] = data[i * 4 + 1] / 255.0;
132
+ float32Data[i + 2 * width * height] = data[i * 4 + 2] / 255.0;
133
+ }
134
+ return new ort.Tensor("float32", float32Data, [1, 3, height, width]);
135
+ }
136
+
137
+ function putimagetensor(ctx, x, y, tensor, width = tile_size, height = tile_size) {
138
+ const outputData = tensor.data;
139
+ const imageData = ctx.createImageData(width, height);
140
+ for (let i = 0; i < width * height; i++) {
141
+ imageData.data[i * 4] = Math.max(0, Math.min(255, outputData[i] * 255));
142
+ imageData.data[i * 4 + 1] = Math.max(0, Math.min(255, outputData[i + width * height] * 255));
143
+ imageData.data[i * 4 + 2] = Math.max(0, Math.min(255, outputData[i + 2 * width * height] * 255));
144
+ imageData.data[i * 4 + 3] = 255;
145
+ }
146
+ ctx.putImageData(imageData, x, y);
147
+ }
148
+
149
+ // New blending function
150
+ function blendpixels(targetData, sourceData, index, alpha) {
151
+ targetData[index] = targetData[index] * (1 - alpha) + sourceData[index] * alpha;
152
+ targetData[index + 1] = targetData[index + 1] * (1 - alpha) + sourceData[index + 1] * alpha;
153
+ targetData[index + 2] = targetData[index + 2] * (1 - alpha) + sourceData[index + 2] * alpha;
154
+ targetData[index + 3] = 255; // Alpha channel
155
+ }
156
+
157
+ async function enhanceimage(sourcecanvas) {
158
+ setappstate('processing');
159
+ await new Promise(r => setTimeout(r, 100)); // Allow UI to update
160
+
161
+ const originalWidth = sourcecanvas.width;
162
+ const originalHeight = sourcecanvas.height;
163
+
164
+ // Calculate padded dimensions to ensure full coverage with effective_tile_size
165
+ const numTilesX = Math.ceil(originalWidth / effective_tile_size);
166
+ const numTilesY = Math.ceil(originalHeight / effective_tile_size);
167
+
168
+ const paddedWidth = numTilesX * effective_tile_size + 2 * overlap_size;
169
+ const paddedHeight = numTilesY * effective_tile_size + 2 * overlap_size;
170
+
171
+ const paddedInputCanvas = document.createElement('canvas');
172
+ paddedInputCanvas.width = paddedWidth;
173
+ paddedInputCanvas.height = paddedHeight;
174
+ const paddedInputCtx = paddedInputCanvas.getContext('2d');
175
+ // Draw the original image centered within the padded canvas
176
+ paddedInputCtx.drawImage(sourcecanvas, overlap_size, overlap_size, originalWidth, originalHeight);
177
+
178
+ const paddedOutputCanvas = document.createElement('canvas');
179
+ paddedOutputCanvas.width = paddedWidth;
180
+ paddedOutputCanvas.height = paddedHeight;
181
+ const paddedOutputCtx = paddedOutputCanvas.getContext('2d');
182
+
183
+ const totalTiles = numTilesX * numTilesY;
184
+ let processedTiles = 0;
185
+
186
+ for (let y_idx = 0; y_idx < numTilesY; y_idx++) {
187
+ for (let x_idx = 0; x_idx < numTilesX; x_idx++) {
188
+ // Calculate the top-left corner for extracting the *overlapping* input tile
189
+ let inputTileX = x_idx * effective_tile_size;
190
+ let inputTileY = y_idx * effective_tile_size;
191
+
192
+ // Ensure we don't go out of bounds for the input tile extraction
193
+ inputTileX = Math.min(inputTileX, paddedWidth - tile_size);
194
+ inputTileY = Math.min(inputTileY, paddedHeight - tile_size);
195
+
196
+ const tensor = getimagetensor(paddedInputCtx, inputTileX, inputTileY, tile_size, tile_size);
197
+ const results = await session.run({ input: tensor });
198
+ const outputTensor = results.output;
199
+
200
+ // Get the output tile as ImageData
201
+ const outputTileImageData = paddedOutputCtx.createImageData(tile_size, tile_size);
202
+ for (let i = 0; i < tile_size * tile_size; i++) {
203
+ outputTileImageData.data[i * 4] = Math.max(0, Math.min(255, outputTensor.data[i] * 255));
204
+ outputTileImageData.data[i * 4 + 1] = Math.max(0, Math.min(255, outputTensor.data[i + tile_size * tile_size] * 255));
205
+ outputTileImageData.data[i * 4 + 2] = Math.max(0, Math.min(255, outputTensor.data[i + 2 * tile_size * tile_size] * 255));
206
+ outputTileImageData.data[i * 4 + 3] = 255;
207
+ }
208
+
209
+ // Calculate the top-left corner for placing the *effective* part of the output tile
210
+ const outputPlacementX = x_idx * effective_tile_size;
211
+ const outputPlacementY = y_idx * effective_tile_size;
212
+
213
+ // Get the current image data from the padded output canvas for blending
214
+ const currentImageData = paddedOutputCtx.getImageData(outputPlacementX, outputPlacementY, effective_tile_size + overlap_size * 2, effective_tile_size + overlap_size * 2); // Get a larger area for blending
215
+
216
+ // Iterate over the output tile data and blend it into the padded output canvas
217
+ for (let yy = 0; yy < tile_size; yy++) {
218
+ for (let xx = 0; xx < tile_size; xx++) {
219
+ const globalX = outputPlacementX + xx;
220
+ const globalY = outputPlacementY + yy;
221
+
222
+ // Check bounds relative to the padded output canvas
223
+ if (globalX >= paddedWidth || globalY >= paddedHeight || globalX < 0 || globalY < 0) continue;
224
+
225
+ const outputTilePixelIndex = (yy * tile_size + xx) * 4;
226
+ const globalPixelIndex = ((globalY) * paddedWidth + (globalX)) * 4;
227
+
228
+ let alpha = 1.0; // Default to full opacity
229
+
230
+ // Horizontal blending (left side)
231
+ if (xx < overlap_size && x_idx > 0) {
232
+ alpha *= (xx / overlap_size);
233
+ }
234
+ // Horizontal blending (right side)
235
+ if (xx >= tile_size - overlap_size && x_idx < numTilesX - 1) {
236
+ alpha *= ((tile_size - 1 - xx) / overlap_size);
237
+ }
238
+ // Vertical blending (top side)
239
+ if (yy < overlap_size && y_idx > 0) {
240
+ alpha *= (yy / overlap_size);
241
+ }
242
+ // Vertical blending (bottom side)
243
+ if (yy >= tile_size - overlap_size && y_idx < numTilesY - 1) {
244
+ alpha *= ((tile_size - 1 - yy) / overlap_size);
245
+ }
246
+
247
+ // Apply blending
248
+ if (alpha < 1.0) {
249
+ // Read existing pixel data from the paddedOutputCtx to blend with
250
+ const existingPixelData = paddedOutputCtx.getImageData(globalX, globalY, 1, 1).data;
251
+ outputTileImageData.data[outputTilePixelIndex] = Math.round(existingPixelData[0] * (1 - alpha) + outputTileImageData.data[outputTilePixelIndex] * alpha);
252
+ outputTileImageData.data[outputTilePixelIndex + 1] = Math.round(existingPixelData[1] * (1 - alpha) + outputTileImageData.data[outputTilePixelIndex + 1] * alpha);
253
+ outputTileImageData.data[outputTilePixelIndex + 2] = Math.round(existingPixelData[2] * (1 - alpha) + outputTileImageData.data[outputTilePixelIndex + 2] * alpha);
254
+ }
255
+ }
256
+ }
257
+ paddedOutputCtx.putImageData(outputTileImageData, outputPlacementX, outputPlacementY);
258
+
259
+ processedTiles++;
260
+ const progress = Math.round((processedTiles / totalTiles) * 100);
261
+ dom.progressbar.style.width = `${progress}%`;
262
+ dom.progresstext.textContent = `${progress}%`;
263
+ await new Promise(resolve => setTimeout(resolve, 0));
264
+ }
265
+ }
266
+
267
+ dom.inputcanvas.width = originalWidth;
268
+ dom.inputcanvas.height = originalHeight;
269
+ dom.inputcanvas.getContext('2d').drawImage(sourcecanvas, 0, 0);
270
+
271
+ // Draw the effectively enhanced part from the padded output canvas onto the final output canvas
272
+ dom.outputcanvas.width = originalWidth;
273
+ dom.outputcanvas.height = originalHeight;
274
+ dom.outputcanvas.getContext('2d').drawImage(paddedOutputCanvas, overlap_size, overlap_size, originalWidth, originalHeight, 0, 0, originalWidth, originalHeight);
275
+
276
+ setappstate('results');
277
+ }
278
+
279
+ function applyblurpreview() {
280
+ const radius = dom.blurslider.value;
281
+ dom.blurvalue.textContent = radius;
282
+ const ctx = dom.preprocesscanvas.getContext('2d');
283
+ ctx.clearRect(0, 0, dom.preprocesscanvas.width, dom.preprocesscanvas.height);
284
+ ctx.filter = `blur(${radius}px)`;
285
+ ctx.drawImage(originalimage, 0, 0);
286
+ ctx.filter = 'none';
287
+ }
288
+
289
+ function handlefiles(files) {
290
+ const file = files[0];
291
+ if (!file || !file.type.startsWith('image/')) return alert('Please upload a valid image file.');
292
+
293
+ const img = new Image();
294
+ img.onload = () => {
295
+ originalimage = img;
296
+ const ctx = dom.preprocesscanvas.getContext('2d');
297
+ dom.preprocesscanvas.width = img.width;
298
+ dom.preprocesscanvas.height = img.height;
299
+ ctx.drawImage(img, 0, 0);
300
+ dom.blurslider.value = 0;
301
+ dom.blurvalue.textContent = '0';
302
+ setappstate('pre-process');
303
+ };
304
+ img.src = URL.createObjectURL(file);
305
+ }
306
+
307
+ ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(ename => dom.droparea.addEventListener(ename, e => { e.preventDefault(); e.stopPropagation(); }));
308
+ ['dragenter', 'dragover'].forEach(ename => dom.droparea.addEventListener(ename, () => dom.droparea.classList.add('highlight')));
309
+ ['dragleave', 'drop'].forEach(ename => dom.droparea.addEventListener(ename, () => dom.droparea.classList.remove('highlight')));
310
+ dom.droparea.addEventListener('drop', e => handlefiles(e.dataTransfer.files));
311
+ dom.fileelem.addEventListener('change', e => handlefiles(e.target.files));
312
+
313
+ dom.downloadbtn.addEventListener('click', () => {
314
+ const link = document.createElement('a');
315
+ link.download = 'enhanced_image.png';
316
+ link.href = dom.outputcanvas.toDataURL('image/png');
317
+ link.click();
318
+ });
319
+
320
+ dom.resetbtn.addEventListener('click', () => {
321
+ setappstate('upload');
322
+ dom.slider.style.left = '50%';
323
+ dom.outputcanvas.style.clipPath = 'polygon(0 0, 50% 0, 50% 100%, 0 100%)';
324
+ });
325
+
326
+ dom.blurslider.addEventListener('input', applyblurpreview);
327
+
328
+ dom.enhanceblurredbtn.addEventListener('click', () => {
329
+ const blurredcanvas = document.createElement('canvas');
330
+ blurredcanvas.width = originalimage.width;
331
+ blurredcanvas.height = originalimage.height;
332
+ const ctx = blurredcanvas.getContext('2d');
333
+ ctx.filter = `blur(${dom.blurslider.value}px)`;
334
+ ctx.drawImage(originalimage, 0, 0);
335
+ enhanceimage(blurredcanvas);
336
+ });
337
+
338
+ dom.enhanceoriginalbtn.addEventListener('click', () => enhanceimage(dom.preprocesscanvas));
339
+
340
+ let isdragging = false;
341
+ const moveslider = (clientX) => {
342
+ if (!isdragging) return;
343
+ const rect = dom.comparisoncontainer.getBoundingClientRect();
344
+ let x = clientX - rect.left;
345
+ x = Math.max(0, Math.min(x, rect.width));
346
+ const percent = (x / rect.width) * 100;
347
+ dom.slider.style.left = `${percent}%`;
348
+ dom.outputcanvas.style.clipPath = `polygon(0 0, ${percent}% 0, ${percent}% 100%, 0 100%)`;
349
+ };
350
+ dom.slider.addEventListener('mousedown', () => isdragging = true);
351
+ document.addEventListener('mouseup', () => isdragging = false);
352
+ document.addEventListener('mousemove', e => moveslider(e.clientX));
353
+ dom.slider.addEventListener('touchstart', e => { isdragging = true; e.preventDefault(); });
354
+ document.addEventListener('touchend', () => isdragging = false);
355
+ document.addEventListener('touchmove', e => moveslider(e.touches[0].clientX));
356
+
357
+ initmodel();
358
+ </script>
359
+ </body>
360
+ </html>
model.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:28df809253011dbd555e460af7b82a062cdddf7d362403393c8545db286f0ca7
3
+ size 124136009