-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
192 lines (174 loc) · 4.47 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<div>Teachable Machine Image Model - p5.js and ml5.js</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.dom.min.js"></script>
<script src="https://unpkg.com/ml5@latest/dist/ml5.min.js"></script>
<script type="text/javascript">
// Classifier Variable
let classifier;
// Model URL
let imageModelURL = 'https://teachablemachine.withgoogle.com/models/HKBpf5d2u/';
// Video
let video;
let flippedVideo;
let wait;
let nice;
let mind;
let smile;
let leave;
let stop;
let disappoint;
let waah;
let swag;
//Fading
let fade = 0;
// To store the classification
let label = "";
let current;
let confidence = 0;
// Load the model first
function preload() {
classifier = ml5.imageClassifier(imageModelURL + 'model.json');
swag = loadImage('ak.jpeg');
}
function setup() {
createCanvas(480, 360);
// Create the video
video = createCapture(VIDEO);
video.size(width, height);
video.hide();
wait = createVideo('anish.mp4');
wait.size(width, height);
wait.hide();
nice = createVideo('nice.mp4');
nice.size(width, height);
nice.hide();
mind = createVideo('mindBlown.mp4');
mind.size(width, height);
mind.hide();
smile = createVideo('laugh.mp4');
smile.size(width, height);
smile.hide();
leave = createVideo('leave.mp4');
leave.size(width, height);
leave.hide();
stop = createVideo('ruko.mp4');
stop.size(width, height);
stop.hide();
disappoint = createVideo('maaro.mp4');
disappoint.size(width, height);
disappoint.hide();
waah = createVideo('waah.mp4');
waah.size(100, 100);
waah.hide();
swag.resize(width, height);
current = wait;
// Start classifying
classifyVideo();
}
function draw() {
background(0);
//tint(255);
// Draw the video
if(label == null){
label = "Normal";
}
if(label == "Normal"){
current.pause();
image(flippedVideo, 0, 0);
}
if(label == "Wait" && confidence >=0.85){
current.pause();
video.hide();
image(wait, 60,40);
wait.play();
current = wait;
}
if(label == "Nice" && confidence >=0.85){
current.pause();
video.hide();
image(nice, 60, 40);
nice.play();
current = nice;
}
if(label == "MindBlown" && confidence >=0.85){
current.pause();
video.hide();
image(mind, 60, 40);
mind.play();
current = mind;
}
if(label == "Ak_swag" &&confidence >=0.85){
current.pause();
image(swag, 0, 0);
//fade = 255;
}
// if(fade>0){
// tint(255, fade);
// image(swag, 0, 0);
// fade -= 10;
// }
if(label == "Stop" && confidence >=0.80){
current.pause();
video.hide();
image(stop, 60, 40);
stop.play();
current = stop;
}
if(label == "Smile" && confidence >=0.75){
current.pause();
video.hide();
image(smile, 60, 40);
smile.play();
current = smile;
}
if(label == "Leave" && confidence >=0.85){
current.pause();
video.hide();
image(leave, 60, 40);
leave.play();
current = leave;
}
if(label == "Dissapointed" && confidence >=0.85){
current.pause();
video.hide();
image(disappoint, 60, 40);
disappoint.play();
current = disappoint;
}
// if(label == "Kadak" && confidence >=0.30){
// current.pause();
// video.hide();
// image(waah, 0, 0);
// waah.play();
// current = waah;
// }
// // Draw the label
fill(255);
textSize(16);
textAlign(CENTER);
text(label, width / 2, height - 4);
}
// Get a prediction for the current video frame
function classifyVideo() {
flippedVideo = ml5.flipImage(video)
classifier.classify(flippedVideo, gotResult);
flippedVideo.remove();
}
// 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]);
print(results[0]);
setTimeout(()=>{
label = results[0].label;
confidence = results[0].confidence;
}, 1500);
// Classifiy again!
classifyVideo();
}
</script>