-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool_nst0.html
329 lines (264 loc) · 11 KB
/
tool_nst0.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
<!DOCTYPE html>
<html>
<head>
<title>NST0: Transfering color palettes from one image to another</title>
<style>
#contentDropZone {
border: 4px dashed #ccc;
padding: 50px;
text-align: center;
}
#contentDropZone.dragover {
border-color: #007bff;
background-color: #f1f1f1;
}
#styleDropZone {
border: 4px dashed #ccc;
padding: 50px;
text-align: center;
}
#styleDropZone.dragover {
border-color: #007bff;
background-color: #f1f1f1;
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="favicon.png" type="image/png">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<link href="styles.css" rel="stylesheet">
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML">
</script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-7VHN6C2LBD"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-7VHN6C2LBD');
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4282081447618681"
crossorigin="anonymous"></script>
</head>
<body>
<header>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Tools</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item"><a class="nav-link" href="index.html#bio">Bio</a></li>
<li class="nav-item"><a class="nav-link" href="index.html#experience">Experience</a></li>
<li class="nav-item"><a class="nav-link" href="index.html#projects">Projects</a></li>
<li class="nav-item"><a class="nav-link" href="index.html#education">Education</a></li>
<li class="nav-item"><a class="nav-link" href="blog.html">Blog</a></li>
<li class="nav-item"><a class="nav-link" href="tools.html">Tools</a></li>
</ul>
</div>
</nav>
</header>
<main>
<div class="container mt-5">
<h1 class="text-center">NST0: Transfering color palettes from one image to another</h1>
<div class="row mt-5">
<div class="col-md-6">
<!-- Input for content image upload -->
<div class="custom-file">
<input type="file" class="custom-file-input" id="contentInput" accept="image/*">
<label class="custom-file-label" for="contentImageInput">Choose content image</label>
</div>
<!-- Drag and Drop Zone for content -->
<div id="contentDropZone">
Drag and Drop Content Image Here
</div>
</div>
<div class="col-md-6">
<!-- Input for style image upload -->
<div class="custom-file">
<input type="file" class="custom-file-input" id="styleInput" accept="image/*">
<label class="custom-file-label" for="styleImageInput">Choose style image</label>
</div>
<!-- Drag and Drop Zone for style -->
<div id="styleDropZone">
Drag and Drop Style Image Here
</div>
</div>
</div>
<div class="container mt-5">
<div class="progress">
<div id="progressBar" class="progress-bar" role="progressbar" style="width: 0%;" aria-valuenow="0"
aria-valuemin="0" aria-valuemax="100"></div>
</div>
<!-- Div to display the uploaded and generated images -->
<div class="row mt-5">
<div class="col-md-6 text-center">
<div id="contentImageContainer"></div>
</div>
<div class="col-md-6 text-center">
<div id="styleImageContainer"></div>
</div>
</div>
<div class="row mt-5">
<div class="col-md-12 text-center">
<div id="outputcanvasContainer">
<canvas id="outputCanvas" style="max-width: 100%;"></canvas>
</div>
</div>
</div>
</div>
</main>
<!-- Bootstrap JS (optional) -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.min.js"></script>
<script>
let fullSizeContentImage = null; // Global variable for content image
let fullSizeStyleImage = null; // Global variable for style image
// Drag and Drop functionality
const contentDropZone = document.getElementById('contentDropZone');
const styleDropZone = document.getElementById('styleDropZone');
// function displayImage(file, containerId) {
// const reader = new FileReader();
// reader.onload = function (e) {
// const img = new Image();
// img.src = e.target.result;
// img.className = "img-fluid";
// document.getElementById(containerId).innerHTML = '';
// document.getElementById(containerId).appendChild(img);
// };
// reader.readAsDataURL(file);
// }
function displayImage(file, containerId) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = function (e) {
const img = new Image();
img.src = e.target.result;
img.className = "img-fluid"; // Bootstrap's class for responsive images
img.style = "max-height: 100px;" // Limit height to 100px
img.onload = function () {
const container = document.getElementById(containerId);
container.innerHTML = '';
container.appendChild(img);
resolve(e.target.result); // Resolve with original image data
};
img.onerror = function () {
reject(new Error("Failed to load image"));
};
};
reader.onerror = function () {
reject(new Error("Failed to read file"));
};
reader.readAsDataURL(file);
});
}
function displayCanvas(imageData, canvasId) {
const canvas = document.getElementById(canvasId);
const ctx = canvas.getContext('2d');
// Set canvas dimensions
canvas.width = imageData.width;
canvas.height = imageData.height;
// Draw the image data on the canvas
ctx.putImageData(imageData, 0, 0);
}
async function tryRender() {
const progressBar = document.getElementById('progressBar');
try {
if (fullSizeContentImage && fullSizeStyleImage) {
progressBar.style.width = '10%';
progressBar.innerHTML = 'Initializing...';
console.log('Images ready, running inference');
const contentImg = new Image();
const styleImg = new Image();
contentImg.src = fullSizeContentImage;
styleImg.src = fullSizeStyleImage;
const contentTensor = await ort.Tensor.fromImage(contentImg)
const styleTensor = await ort.Tensor.fromImage(styleImg)
console.log('contentTensor', contentTensor)
console.log('contentTensor.shape', contentTensor.shape)
console.log('contentTensor.data', contentTensor.data)
console.log('contentTensor.data', contentTensor.data[0])
console.log('contentTensor.data', contentTensor.data[-1])
console.log('styleTensor', styleTensor)
console.log('styleTensor.shape', styleTensor.shape)
console.log('styleTensor.data', styleTensor.data)
console.log('styleTensor.data', styleTensor.data[0])
console.log('styleTensor.data', styleTensor.data[-1])
progressBar.style.width = '30%';
progressBar.innerHTML = 'Images ready, running inference...';
const feeds = { 'image': contentTensor, 'style': styleTensor };
const session = await ort.InferenceSession.create('./model_nst0.onnx');
progressBar.style.width = '70%';
progressBar.innerHTML = 'Inference complete, rendering...';
const outputs = await session.run(feeds);
const outputTensor = outputs['output'];
console.log('outputTensor.shape', outputTensor.shape)
console.log('outputTensor.data', outputTensor.data)
console.log('outputTensor.data', outputTensor.data[0])
console.log('outputTensor.data', outputTensor.data[-1])
const outputImg = outputTensor.toImageData()
displayCanvas(outputImg, 'outputCanvas');
progressBar.style.width = '100%';
progressBar.innerHTML = 'Done!';
} else {
}
} catch (e) {
progressBar.style.width = '100%';
progressBar.classList.add('bg-danger');
progressBar.innerHTML = `Error: ${e}`;
console.error(`Failed to inference ONNX model: ${e}`);
}
}
// Attach the function to input change event
document.getElementById('contentInput').addEventListener('change', async function (event) {
fullSizeContentImage = await displayImage(event.target.files[0], 'contentImageContainer');
tryRender();
});
document.getElementById('styleInput').addEventListener('change', async function (event) {
fullSizeStyleImage = await displayImage(event.target.files[0], 'styleImageContainer');
tryRender();
});
// Dragover
contentDropZone.addEventListener('dragover', function (event) {
event.preventDefault();
this.classList.add('dragover');
});
styleDropZone.addEventListener('dragover', function (event) {
event.preventDefault();
this.classList.add('dragover');
});
// Dragleave
contentDropZone.addEventListener('dragleave', function (event) {
this.classList.remove('dragover');
});
styleDropZone.addEventListener('dragleave', function (event) {
this.classList.remove('dragover');
});
// ImageRender
contentDropZone.addEventListener('drop', async function (event) {
event.preventDefault();
this.classList.remove('dragover');
const file = event.dataTransfer.files[0];
if (file && file.type.match('image.*')) {
fullSizeContentImage = await displayImage(file, 'contentImageContainer');
tryRender();
}
});
styleDropZone.addEventListener('drop', async function (event) {
event.preventDefault();
this.classList.remove('dragover');
const file = event.dataTransfer.files[0];
if (file && file.type.match('image.*')) {
fullSizeStyleImage = await displayImage(file, 'styleImageContainer');
tryRender();
}
});
</script>
<script src="https://cdn.jsdelivr.net/npm/onnxjs/dist/onnx.min.js"></script>
<script>
</script>
</body>
</html>