-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
160 lines (150 loc) · 4.36 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My first time getting judged by a machine</title>
<script src="lib/p5.min.js"></script>
<script src="lib/ml5.min.js"></script>
<script src="lib/p5.speech.js"></script>
<style>
* {
box-sizing: border-box;
}
#loading,
#face {
font-size: 120px
}
@keyframes spinning {
0% {
transform: rotateZ(0deg)
},
100% {
transform: rotateZ(360deg)
}
}
#output {
font-size: 40px;
}
#loading {
animation: spinning 2s infinite linear;
transform-origin: center
}
.wrapper {
max-width: 400px;
margin: auto;
padding-top: 10rem;
text-align: center;
height: calc(100vh - 4rem);
}
#judge1 {
border: none;
font-size: 30px;
background-color: lightgrey;
margin-top: 5rem;
padding: 1.3rem 2rem;
border-radius: 4px;
cursor: pointer;
}
body {
display: flex;
flex-direction: column;
margin: 0;
line-height: 1.35;
overflow: hidden;
}
footer,
#output {
font-family: sans-serif;
}
footer {
height: 4rem;
background-color: lightgrey;
font-size: 20px;
padding: 0 1rem;
}
</style>
</head>
<body>
<div class="wrapper">
<div id="loading">⏳</div>
<div id="face"></div>
<div id="output"></div>
<button id="judge1" type="button">Judge me</button>
</div>
<footer><p>My first time getting judged by a machine. Made with ml5.js</p></footer>
<script>
let word2Vec;
let mobileNet;
let vid;
let voice = new p5.Speech(); // speech synthesis object
let output;
let btn;
function modelLoaded() {
console.log('Wordvec loaded')
document.querySelector('#loading').textContent = "⌛"
}
function mobileNetLoaded() {
console.log('DarkNet loaded')
state = 0
btn.style = "display: inline-block"
document.querySelector('#loading').style = "display: none"
document.querySelector('#face').textContent = "😐"
}
function gotResults(error, results) {
if (error) {
console.log('error predicting')
} else {
console.log(results)
setTimeout(() => {
document.querySelector('#face').textContent = "😬"
const thing = results[0].label.split(',')[0]
const sentences = ["You're face is like a ",
"You remind me of a ",
"You look like a ",
"Somehow you're like a ",
"When I see your face I think of a ",
"I think of you as a "]
//debugger
const judgement = sentences[Math.floor(Math.random() * sentences.length)] + thing + ".";
voice.speak(judgement)
output.textContent = judgement
}, 1800)
/*
word2Vec.subtract(["machine", thing])
.then((error,difference) => {
if (error) {
console.log('error in difference')
} else {
word2Vec.add(["human", difference[0].word])
}
})
.then((error,result) => {
if (error) {
console.log("error adding")
} else {
const word = result[0].word
let analogy = thing + "for a machine is like " + word + "to you humans.<br>In case you didn't get that"
output.textContext = analogy
}
})
*/
}
}
function setup() {
noCanvas()
//createCanvas(600, 400)
word2Vec = ml5.word2vec('data/output.json', modelLoaded);
vid = createCapture(VIDEO)
vid.hide()
mobileNet = ml5.imageClassifier('MobileNet', vid, mobileNetLoaded);
btn = document.querySelector("#judge1");
output = document.querySelector("#output");
btn.style = "display: none"
btn.addEventListener('click', () => {
document.querySelector('#face').textContent = "🤔"
mobileNet.predict(gotResults)
})
}
</script>
</body>
</html>