-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
75 lines (62 loc) · 2.41 KB
/
app.js
File metadata and controls
75 lines (62 loc) · 2.41 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
let model;
let img;
let canvas;
let dreamFactor = 0.2; // Strength of initial dream effect
let numIterations = 5; // Number of passes for stronger Inception effect
let exaggerationFactor = 1.5; // How much to amplify changes in each iteration
function preload() {
// Load your TensorFlow.js model via ml5 (locally hosted)
model = ml5.imageClassifier('model/model.json', modelLoaded);
}
function setup() {
canvas = createCanvas(400, 400);
canvas.parent('app');
background(255);
// Add event listener for image upload
document.getElementById('imageUpload').addEventListener('change', handleImageUpload);
}
function modelLoaded() {
console.log('Model loaded successfully!');
}
function handleImageUpload(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
img = loadImage(e.target.result, imageLoaded);
};
reader.readAsDataURL(file);
}
}
function imageLoaded() {
// Display the uploaded image on the canvas
image(img, 0, 0, width, height);
applyDeepDream(numIterations); // Apply the multi-pass inception effect
}
function applyDeepDream(iterations) {
if (img && model) {
img.loadPixels();
for (let iter = 0; iter < iterations; iter++) {
console.log(`Applying deep dream iteration: ${iter + 1}`);
// Run classification at each stage to create exaggerated features
model.classify(canvas, (err, results) => {
if (err) {
console.error(err);
return;
}
let result = results[0].label;
document.getElementById('prediction').innerText = `Iteration ${iter + 1}: ${result}`;
// Deep dream effect: manipulate pixel colors, amplifying with each iteration
for (let i = 0; i < img.pixels.length; i += 4) {
img.pixels[i] = img.pixels[i] * (1 + random(-dreamFactor, dreamFactor) * exaggerationFactor); // Red
img.pixels[i + 1] = img.pixels[i + 1] * (1 + random(-dreamFactor, dreamFactor) * exaggerationFactor); // Green
img.pixels[i + 2] = img.pixels[i + 2] * (1 + random(-dreamFactor, dreamFactor) * exaggerationFactor); // Blue
}
img.updatePixels(); // Apply the modified pixels to the image
image(img, 0, 0, width, height); // Re-draw the image on the canvas
// Gradually increase exaggeration factor for stronger dream effect
exaggerationFactor *= 1.2;
});
}
}
}