-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch_mobileNet.js
executable file
·150 lines (135 loc) · 4.11 KB
/
sketch_mobileNet.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
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
const image = document.getElementById('image'); // The image we want to classify
const dropContainer = document.getElementById('container');
const warning = document.getElementById('warning');
const fileInput = document.getElementById('fileUploader');
function preventDefaults(e) {
e.preventDefault()
e.stopPropagation()
};
function windowResized() {
let windowW = window.innerWidth;
if (windowW < 480 && windowW >= 200) {
image.style.maxWidth = windowW - 80;
dropContainer.style.display = 'block';
} else if (windowW < 200) {
dropContainer.style.display = 'none';
} else {
image.style.maxWidth = '90%';
dropContainer.style.display = 'block';
}
}
['dragenter', 'dragover'].forEach(eventName => {
dropContainer.addEventListener(eventName, e => dropContainer.classList.add('highlight'), false)
});
['dragleave', 'drop'].forEach(eventName => {
dropContainer.addEventListener(eventName, e => dropContainer.classList.remove('highlight'), false)
});
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropContainer.addEventListener(eventName, preventDefaults, false)
});
dropContainer.addEventListener('drop', gotImage, false)
function gotImage(e) {
const dt = e.dataTransfer;
const files = dt.files;
if (files.length > 1) {
console.error('upload only one file');
}
const file = files[0];
const imageType = /image.*/;
if (file.type.match(imageType)) {
warning.innerHTML = '';
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onloadend = () => {
image.src = reader.result;
setTimeout(classifyImage, 100);
}
} else {
image.src = 'images/sphynx.jpg';
setTimeout(classifyImage, 100);
warning.innerHTML = 'Please drop an image file.'
}
}
function handleFiles() {
const curFiles = fileInput.files;
if (curFiles.length === 0) {
image.src = 'images/sphynx.jpg';
setTimeout(classifyImage, 100);
warning.innerHTML = 'No image selected for upload';
} else {
image.src = window.URL.createObjectURL(curFiles[0]);
warning.innerHTML = '';
setTimeout(classifyImage, 100);
}
}
function clickUploader() {
fileInput.click();
}
const result = document.getElementById('result'); // The result tag in the HTML
const probability = document.getElementById('probability'); // The probability tag in the HTML
// Initialize the Image Classifier method
let classifier, loadStartTime, loadCompleteTime;
async function loadModel() {
loadStartTime = window.performance.now()
console.log("Loading model...")
classifier = await await mobilenet.load();
loadCompleteTime = window.performance.now()
console.log("Model loaded in " + Number.parseFloat(loadCompleteTime - loadStartTime).toFixed(2).toString() + "ms")
modelLoaded()
classifyImage()
}
loadModel()
function modelLoaded() {
document.getElementById('loader').style.display = "none"
document.getElementById('container').style.display = "block"
}
async function classifyImage() {
inferenceStartTime = window.performance.now()
console.log("Starting Inference...")
prediction = await classifier.classify(image);
inferenceCompleteTime = window.performance.now()
console.log("Inference completed in " + Number.parseFloat(inferenceCompleteTime - inferenceStartTime).toFixed(2).toString() + "ms")
let prob = prediction[0]["probability"] * 100;
probability.innerText = Number.parseFloat(prob).toFixed(2) + '%';
result.innerText = prediction[0]["className"]
console.log(prediction[0]["className"])
}
let class_names = [
'Abyssinian',
'Bengal',
'Birman',
'Bombay',
'British_Shorthair',
'Egyptian_Mau',
'Maine_Coon',
'Persian',
'Ragdoll',
'Russian_Blue',
'Siamese',
'Sphynx',
'american_bulldog',
'american_pit_bull_terrier',
'basset_hound',
'beagle',
'boxer',
'chihuahua',
'english_cocker_spaniel',
'english_setter',
'german_shorthaired',
'great_pyrenees',
'havanese',
'japanese_chin',
'keeshond',
'leonberger',
'miniature_pinscher',
'newfoundland',
'pomeranian',
'pug',
'saint_bernard',
'samoyed',
'scottish_terrier',
'shiba_inu',
'staffordshire_bull_terrier',
'wheaten_terrier',
'yorkshire_terrier'
]