-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
98 lines (72 loc) · 3.24 KB
/
main.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
var loading = document.getElementsByClassName("loading")[0]
// Initialize the Image Classifier method with MobileNet
const classifier = ml5.imageClassifier('MobileNet', function () {
console.log('Model Loaded!');
});
var app = new Vue({
el: ".app",
data() {
return {
// MY FACE!!!
imgsrc: "http://2.bp.blogspot.com/_pVSSnrgAXPY/TGDYQlH1_XI/AAAAAAAAAA4/qHZk2bd8gi0/s1600/Photo-0189.jpg",
repo: {
link: "https://github.com/rdhwnsh/ml5vue",
author: "Nabil Ridhwanshah"
}
}
},
methods: {
changeImage() {
// IMAGE VERIFICATION
if (this.imgsrc.indexOf(".jpg") > 0 || this.imgsrc.indexOf(".png") > 0) {
// Change the not valid image file to none
document.getElementsByClassName("notvalidimage")[0].style.display = "none";
var image = document.getElementById('image');
image.src = this.imgsrc
// SET TIMEOUT FOR CALLBACK BECAUSE THE NEW IMAGE WILL NOT BE LOADED YET AND WILL CLASSIFY THE OLD IMAGE INSTEAD OF THE NEW ONE
setTimeout(() => {
this.classify_image(image)
}, 1)
} else {
// DISPLAY THE NOT VALID IMAGE
document.getElementsByClassName("notvalidimage")[0].style.display = "initial";
console.log("error")
}
},
classify_image(image) {
// Change the loading display to none
document.getElementsByClassName("loading")[0].style.display = "none"
classifier.predict(image, function (err, results) {
if (err) {
console.log(err);
}
// Result for default (0)
var result = results[0].className;
document.getElementById("result").innerHTML = result
// Probability in % for default (0)
var probability = Math.floor(results[0].probability * 100) + "%";
document.getElementById("probability").innerHTML = probability
// =================================================
// Result for 1
var result1 = results[1].className;
document.getElementById("result1").innerHTML = result1
// Probability in % for 1
var probability1 = Math.floor(results[1].probability * 100) + "%";
document.getElementById("probability1").innerHTML = probability1
// Result for 2
var result2 = results[02].className;
document.getElementById("result2").innerHTML = result2
// Probability in % for 2
var probability2 = Math.floor(results[02].probability * 100) + "%";
document.getElementById("probability2").innerHTML = probability2
});
}
},
mounted() {
this.changeImage()
// Change the loading display to none
document.getElementsByClassName("loading")[0].style.display = "none";
// Change the not valid image file to none
document.getElementsByClassName("notvalidimage")[0].style.display = "none";
}
})