-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
52 lines (43 loc) · 1.74 KB
/
script.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
const imageId = 'loaded-image';
const getImage = (id) => document.getElementById(id);
function handleFileSelect() {
const loadedImage = getImage(imageId);
const file = document.getElementById('image-uploader-input').files[0];
const reader = new FileReader();
reader.onloadend = () => {
loadedImage.src = reader.result;
}
if (file) {
reader.readAsDataURL(file); // reads the data as a URL
document.getElementById(imageId).style.display = 'block';
} else {
loadedImage.src = '';
}
}
function classify() {
const file = document.getElementById('image-uploader-input').files[0];
// The image we want to classify
const loadedImage = getImage(imageId);
if(!file) {
return alert('You should load image first');
}
const classificationResult = document.getElementById('classification-result');
const classificationProbability = document.getElementById('classification-prob');
// Initialize the Image Classifier method with MobileNet
const classifier = ml5.imageClassifier('MobileNet', () => {
console.log('Model Loaded!');
});
// Make a prediction with the selected image
// This will return an array with a default of 10 options with their probabilities
classifier.predict(loadedImage, (err, results) => {
classificationResult.innerText = results[0].className;
classificationProbability.innerText = results[0].probability.toFixed(4);
});
}
function reset() {
const loadedImage = getImage(imageId);
loadedImage.src = '';
document.getElementById('classification-result').innerText = '';
document.getElementById('classification-prob').innerText = '';
document.getElementById('image-uploader-input').value = '';
}