-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
95 lines (84 loc) · 2.81 KB
/
app.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
let net;
let output;
const robot = '🤖';
const img = document.getElementById("source");
const botConsole = document.getElementById("console");
const unfilteredList = document.getElementById("unfiltered");
const getPredictions = document.getElementById("getPredictions");
const predictions = [];
window.onload = () => {
// Load random image from data.json for prediction analysis
fetch('./assets/json/data.json')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(({images}) => {
//console.log(images);
let imgSrc = images[Math.floor(Math.random() * images.length)];
//console.log(imgSrc);
// Set img attributes
img.setAttribute("src", `./assets/img/${imgSrc}`);
img.setAttribute("height", '264');
img.setAttribute("width", '378');
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
});
};
async function classify() {
console.log('Loading mobilenet..');
document.getElementById('console').innerText = `Loading the model and getting a prediction...`;
// Load the model.
net = await mobilenet.load();
console.log('Successfully loaded model');
// Make a prediction through the model on our image.
const result = await net.classify(img);
console.log(result);
// Collect prediction values and preformat for downloadable json file
const information = {
name: img.src,
labels: result,
}
predictions.push(information);
// Loop through and print complete prediction array results
result.forEach(({className, probability}, index) => {
output = `
<li>index: ${index}, label: ${className}, confidence: ${probability}</li>
`;
unfilteredList.innerHTML += `${output}`;
});
// Sort by probability
let resultElements = result.sort((a, b) => a > b)[0];
if (resultElements.probability > 0.15) {
// Convert the probability to percentages
let probabilityPercent = Math.round(resultElements.probability * 100);
// Display result
botConsole.innerText = `
${robot} ${probabilityPercent}% certain this is a(n) ${resultElements.className.replace(","," or")}.
`;
} else {
botConsole.innerText = `
${robot} I am not sure what I should recognize and my prediction probability is low. Is this a(n) ${resultElements.className.replace(","," or")}?
`;
}
}
function download(content, fileName, contentType) {
const a = document.createElement("a");
const file = new Blob([content], {
type: contentType,
});
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
}
function savePredictions() {
predictionsJSON = {
predictions,
};
download(JSON.stringify(predictionsJSON), "predictions.json", "text/plain");
}
classify();
getPredictions.onclick = savePredictions;