-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
229 lines (212 loc) · 7.46 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>gestureRecognition</title>
<link rel="stylesheet" href="./css/main.css" />
<!-- 引入样式 -->
<link
rel="stylesheet"
href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"
/>
<!-- 引入组件库 -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-data@2.0.0/dist/tf-data.min.js"></script>
</head>
<body>
<div id="app">
<div class="contain">
<div class="left">
<video
class="video"
autoplay
playsinline
muted
id="webcam"
width="80%"
height="80%"
></video>
<div
id="buttons"
style="
display: flex;
justify-content: space-around;
padding: 0 1rem;
"
>
<button onclick="captureSample(0)">None</button>
<button onclick="captureSample(1)">✊ (Rock)</button>
<button onclick="captureSample(2)">🖐 (Paper)</button>
<button onclick="captureSample(3)">✌️ (Scissors)</button>
<button onclick="trainModel()">Train</button>
</div>
<h3 id="status" style="margin-left: 1.25rem"></h3>
</div>
<div class="right" style="display: flex; align-items: center">
<video
class="video video-teach"
src="https://api.kexie.space/data/resource/hello/videos/ml-game.mp4"
width="90%"
height="auto"
controls
></video>
</div>
</div>
</div>
<script>
let trainingData = [];
const labels = ["None", "✊ (Rock)", "🖐 (Paper)", "✌️ (Scissors)"];
function setText(text) {
document.getElementById("status").innerText = text;
}
async function predictImage() {
if (!hasTrained) {
return;
} // Skip prediction until trained
const img = await getWebcamImage();
let result = tf.tidy(() => {
const input = img.reshape([1, 224, 224, 3]);
return model.predict(input);
});
img.dispose();
let prediction = await result.data();
result.dispose();
// Get the index of the highest value in the prediction
let id = prediction.indexOf(Math.max(...prediction));
setText(labels[id]);
}
function createTransferModel(model) {
// Create the truncated base model (remove the "top" layers, classification + bottleneck layers)
const bottleneck = model.getLayer("dropout"); // This is the final layer before the conv_pred pre-trained classification layer
const baseModel = tf.model({
inputs: model.inputs,
outputs: bottleneck.output,
});
// Freeze the convolutional base
for (const layer of baseModel.layers) {
layer.trainable = false;
}
// Add a classification head
const newHead = tf.sequential();
newHead.add(
tf.layers.flatten({
inputShape: baseModel.outputs[0].shape.slice(1),
})
);
newHead.add(tf.layers.dense({ units: 100, activation: "relu" }));
newHead.add(tf.layers.dense({ units: 100, activation: "relu" }));
newHead.add(tf.layers.dense({ units: 10, activation: "relu" }));
newHead.add(
tf.layers.dense({
units: labels.length,
kernelInitializer: "varianceScaling",
useBias: false,
activation: "softmax",
})
);
// Build the new model
const newOutput = newHead.apply(baseModel.outputs[0]);
const newModel = tf.model({
inputs: baseModel.inputs,
outputs: newOutput,
});
return newModel;
}
async function trainModel() {
hasTrained = false;
setText("Training...");
// Setup training data
const imageSamples = [];
const targetSamples = [];
trainingData.forEach((sample) => {
imageSamples.push(sample.image);
let cat = [];
for (let c = 0; c < labels.length; c++) {
cat.push(c === sample.category ? 1 : 0);
}
targetSamples.push(tf.tensor1d(cat));
});
const xs = tf.stack(imageSamples);
const ys = tf.stack(targetSamples);
// Train the model on new image samples
model.compile({
loss: "meanSquaredError",
optimizer: "adam",
metrics: ["acc"],
});
await model.fit(xs, ys, {
epochs: 30,
shuffle: true,
callbacks: {
onEpochEnd: (epoch, logs) => {
console.log("Epoch #", epoch, logs);
},
},
});
hasTrained = true;
}
// Mobilenet v1 0.25 224x224 model
const mobilenet =
"https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json";
let model = null;
let hasTrained = false;
async function setupWebcam() {
return new Promise((resolve, reject) => {
const webcamElement = document.getElementById("webcam");
const navigatorAny = navigator;
navigator.getUserMedia =
navigator.getUserMedia ||
navigatorAny.webkitGetUserMedia ||
navigatorAny.mozGetUserMedia ||
navigatorAny.msGetUserMedia;
if (navigator.getUserMedia) {
navigator.getUserMedia(
{ video: true },
(stream) => {
webcamElement.srcObject = stream;
webcamElement.addEventListener("loadeddata", resolve, false);
},
(error) => reject()
);
} else {
reject();
}
});
}
async function resizeImage(imageTensor, targetHeight, targetWidth) {
const resizedImage = tf.image.resizeBilinear(imageTensor, [
targetHeight,
targetWidth,
]);
return resizedImage;
}
async function getWebcamImage() {
const img = (await webcam.capture()).toFloat();
const resizedImg = await resizeImage(img, 224, 224);
const normalized = resizedImg.div(127).sub(1);
return normalized;
}
async function captureSample(category) {
trainingData.push({
image: await getWebcamImage(),
category: category,
});
setText("Captured: " + labels[category]);
}
let webcam = null;
(async () => {
// Load the model
model = await tf.loadLayersModel(mobilenet);
model = createTransferModel(model);
await setupWebcam();
webcam = await tf.data.webcam(document.getElementById("webcam"));
// Setup prediction every 200 ms
setInterval(predictImage, 200);
})();
</script>
</body>
</html>