-
Notifications
You must be signed in to change notification settings - Fork 0
/
transferlearning.php
237 lines (197 loc) · 7.34 KB
/
transferlearning.php
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
230
231
232
233
234
235
236
237
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Online Transfer Learning</title>
<!-- Load the latest version of TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/knn-classifier"></script>
<!-- Load bootstrap for formatting -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<style type="text/css">
html,
body {
margin: 0;
padding: 0;
}
html {
height: 100%;
}
body {
font-family: Helvetica, Arial, sans-serif;
min-height: 100%;
display: grid;
grid-template-rows: 1fr auto;
}
header {
background: #f0293e;
color: #fff;
text-align: center;
}
main {
background: #ffffff;
min-height: 60vh;
}
.controls {
text-align: center;
padding: 0.5em 0;
background: #333e5a;
}
video {
width: 100%;
max-width: 600px;
display: block;
margin: 0 auto;
}
.headercontent {
text-align: center;
background: #03DAC6;
padding: 1.5em 0;
}
</style>
</head>
<body>
<!-- Add an image that we will use to test -->
<!-- <img id="img" crossorigin src="https://i.imgur.com/JlUvsxa.jpg" width="227" height="227"/> -->
<div class="headercontent row justify-content-md-center" style="text-align: center">
<h1>Webcam Face Detection</h1>
</div>
<div class="headercontent row justify-content-md-center">
<div class="col-md-4">
<div class="input-group mb-12">
<div class="input-group-prepend">
<label class="input-group-text" for="selectcamera">Camera</label>
</div>
<select class="custom-select" id="selectcamera">
</select>
</div>
</div>
<div class="col-md-4">
<button id="button" class="btn btn-dark" style="width: 45%;border-radius: 20px;margin: 5px">Start</button>
<button id="stopbutton" class="btn btn-dark" style="width: 45%;border-radius: 20px;margin: 5px">Stop</button>
</div>
</div>
<main>
<video autoplay playsinline muted id="webcam"></video>
</main>
<button id="class-a">Add A</button>
<button id="class-b">Add B</button>
<button id="class-c">Add C</button>
<div id="console">
prediction: None <br>
probability: 0.0
</div>
<!-- Load index.js after the content of the page -->
<script type="text/javascript">
const button = document.getElementById('button');
const select = document.getElementById('selectcamera');
let currentStream;
const classifier = knnClassifier.create();
const webcamElement = document.getElementById('webcam');
let net;
let modelstr;
function stopMediaTracks(stream) {
stream.getTracks().forEach(track => {
track.stop();
});
}
function gotDevices(mediaDevices) {
select.innerHTML = '';
select.appendChild(document.createElement('option'));
let count = 1;
mediaDevices.forEach(mediaDevice => {
if (mediaDevice.kind === 'videoinput') {
const option = document.createElement('option');
option.value = mediaDevice.deviceId;
const label = mediaDevice.label || `Camera ${count++}`;
const textNode = document.createTextNode(label);
option.appendChild(textNode);
select.appendChild(option);
}
});
}
button.addEventListener('click', event => {
if (typeof currentStream !== 'undefined') {
stopMediaTracks(currentStream);
}
const videoConstraints = {};
if (select.value === '') {
videoConstraints.facingMode = 'environment';
} else {
videoConstraints.deviceId = { exact: select.value };
}
const constraints = {
video: videoConstraints,
audio: false
};
navigator.mediaDevices
.getUserMedia(constraints)
.then(stream => {
currentStream = stream;
webcamElement.srcObject = stream;
return navigator.mediaDevices.enumerateDevices();
})
.then(gotDevices)
.catch(error => {
console.error(error);
});
});
async function app() {
console.log('Loading mobilenet..');
// Load the model.
net = await mobilenet.load();
console.log('Successfully loaded model');
// Create an object from Tensorflow.js data API which could capture image
// from the web camera as Tensor.
const webcam = await tf.data.webcam(webcamElement);
// Reads an image from the webcam and associates it with a specific class
// index.
const addExample = async classId => {
// Capture an image from the web camera.
const img = await webcam.capture();
// Get the intermediate activation of MobileNet 'conv_preds' and pass that
// to the KNN classifier.
const activation = net.infer(img, true);
// Pass the intermediate activation to the classifier.
classifier.addExample(activation, classId);
// prepare dataset and save
modelstr = JSON.stringify(
Object.entries(
classifier.getClassifierDataset()).map(([label, data]) =>
[label, Array.from(data.dataSync()), data.shape]
)
);
// Dispose the tensor to release the memory.
img.dispose();
};
// When clicking a button, add an example for that class.
document.getElementById('class-a').addEventListener('click', () => addExample(0));
document.getElementById('class-b').addEventListener('click', () => addExample(1));
document.getElementById('class-c').addEventListener('click', () => addExample(2));
while (true) {
if (classifier.getNumClasses() > 0) {
const img = await webcam.capture();
// Get the activation from mobilenet from the webcam.
const activation = net.infer(img, 'conv_preds');
// Get the most likely class and confidence from the classifier module.
const result = await classifier.predictClass(activation);
const classes = ['A', 'B', 'C'];
document.getElementById('console').innerHTML = `
prediction: ${classes[result.label]}<br>
probability: ${result.confidences[result.label]}
`;
// Dispose the tensor to release the memory.
img.dispose();
}
await tf.nextFrame();
}
}
app();
navigator.mediaDevices.enumerateDevices().then(gotDevices);
</script>
</body>
</html>