-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
178 lines (161 loc) · 4.3 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
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
var width;
var height;
let gridSize = 3;
let currentImage;
var memories = [];
let activeVid, activeText;
let videoPlaying = false;
let customFont;
var debug = false;
let spacing = 10;
// stuff for ML
let classifier;
let imageModelURL = './assets/model/';
let video;
let flippedVideo;
let label = "Nothing";
let currentLabel = "";
var texts = [
"Wendy playing outside, 06/06/21",
"Cami's parents in Colombia, 07/09/22",
"Visiting Bruna in Brazil, 01/21/21",
"Evening in Paris, 09/10/22",
"Hiking near Mendrisio, 11/10/22",
"Abuelita feliz, 10/31/20",
"Basel field trip, 11/29/22",
"Dancing in Berlin, 12/01/22",
"Thanksgiving, 11/25/22",
]
function preload() {
for (i = 0; i < pow(gridSize, 2); i++) {
memory = []
memory.img = loadImage(`assets/img/memory${i + 1}.jpg`);
memory.vid = createVideo(`assets/vid/memory${i + 1}.mp4`);
memory.vid.volume(0.3);
memory.vid.hide();
memory.vid.onended(videoEnded);
memories.push(memory);
}
currentScene = '';
// stuff for ML
classifier = ml5.imageClassifier(imageModelURL + 'model.json');
customFont = loadFont('assets/font/vcr_mono.ttf');
}
function videoEnded(vid) {
videoPlaying = false;
}
function setup() {
width = window.innerWidth
height = window.innerHeight
textFont(customFont);
textSize(20);
createCanvas(width, height);
console.clear();
video = createCapture(VIDEO);
video.size(160, 120);
video.hide();
flippedVideo = ml5.flipImage(video)
classifyVideo();
}
// Get a prediction for the current video frame
function classifyVideo() {
flippedVideo = ml5.flipImage(video)
classifier.classify(flippedVideo, gotResult);
}
// When we get a result
function gotResult(error, results) {
// If there is an error
if (error) {
console.error(error);
return;
}
// The results are in an array ordered by confidence.
// console.log(results[0]);
if (results[0].confidence > 0.97) {
label = results[0].label;
if (currentLabel != label) {
currentLabel = label;
console.log(`It's ${label}`);
}
}
// Classifiy again!
classifyVideo();
}
function draw() {
if (videoPlaying == false && label != 'Nothing') {
background(0);
if (activeVid != null) {
activeVid.stop();
videoPlaying = false;
}
for (i = 0; i < pow(gridSize, 2); i++) {
if (label == `Memory${i + 1}`) {
activeVid = memories[i].vid;
activeText = texts[i]
}
}
activeVid.play();
videoPlaying = true;
}
if (activeVid == null || videoPlaying == false) {
for (i = 0; i < gridSize; i++) {
for (j = 0; j < gridSize; j++) {
fill(255);
noStroke();
rect(i / gridSize * width, j / gridSize * height, width / gridSize, height / gridSize);
currentImage = memories[j * 3 + i].img;
push();
translate(i / gridSize * width + width / gridSize / 2, j / gridSize * height + height / gridSize / 2);
imageMode(CENTER);
image(currentImage, spacing, spacing, width / gridSize - spacing, currentImage.height * width / currentImage.width / gridSize / 2 - spacing, 0, 0, currentImage.width, currentImage.height, COVER);
pop();
if (debug) {
push();
translate(i / gridSize * width, j / gridSize * height);
rect(0, 0, width / gridSize, height / gridSize / 10);
fill(0);
textAlign(CENTER, CENTER);
text(texts[j * 3 + i], 0, 0, width / gridSize, height / gridSize / 10);
pop();
}
}
}
} else {
background(0);
let vidWidth = height * activeVid.width / activeVid.height;
push();
translate(width / 2, 0);
image(activeVid, -vidWidth / 2, 0, vidWidth, height);
pop();
push();
let offset = 100;
translate(0, height - offset);
fill(255);
rect(0, 0, width, offset);
textSize(offset / 2);
textAlign(CENTER);
fill(0);
text(activeText, 0, offset * 0.7, width, offset);
pop();
}
if (debug) {
fill(0);
rect(0, 0, 160, 140);
image(flippedVideo, 0, 0);
fill(255);
textSize(16);
textAlign(CENTER);
text(label, 80, 140 - 4);
}
}
function windowResized() {
width = window.innerWidth;
height = window.innerHeight;
resizeCanvas(windowWidth, windowHeight);
}
function keyPressed() {
if (keyCode == 70) {
let fs = fullscreen();
fullscreen(!fs);
}
}