forked from yining1023/fast_style_transfer_in_ML5
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketch.js
47 lines (40 loc) · 1 KB
/
sketch.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
/*
===
Fast Style Transfer Simple Example
===
*/
let net;
let inputImg;
let outputImgData;
function setup() {
createCanvas(252, 252);
net = new p5ml.TransformNet(modelLoaded, 'wave', 'models/wave/');
}
// A function to be called when the model has been loaded
function modelLoaded() {
// Set style for the model
net.setStyle('wave');
inputImg = select('#input-img').elt;
/**
* @param inputImg HTMLImageElement of input img
* @return Array3D containing pixels of output img
*/
outputImgData = net.predict(inputImg);
// Show the img on the canvas
renderToCanvas(outputImgData);
}
function renderToCanvas(outputImgData) {
const data = outputImgData.dataSync();
let k = 0;
for (let i = 0; i < width; i++) {
for (let j = 0; j < height; j++) {
k = (i + j * height) * 3;
let r = Math.round(255 * data[k + 0]);
let g = Math.round(255 * data[k + 1]);
let b = Math.round(255 * data[k + 2]);
let c = color(r, g, b);
set(i, j, c);
}
}
updatePixels();
}