-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
91 lines (76 loc) · 2.72 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
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
let model;
const classes = {
0: 'Baked Potato',
1: 'Burger',
2: 'Crispy Chicken',
3: 'Donut',
4: 'Fries',
5: 'Hot Dog',
6: 'Pizza',
7: 'Sandwich',
8: 'Taco',
9: 'Taquito'
};
async function loadModel() {
try {
model = await tf.loadLayersModel('Model/model.json');
const modeltag = document.getElementById('modelTag');
modeltag.innerHTML = `<u>Try Model( Loaded! )</u>`;
console.log('Model loaded successfully');
} catch (error) {
console.error('Failed to load model:', error);
}
}
function handleImageUpload() {
const imageInput = document.getElementById('imageInput');
const imageContainer = document.getElementById('imageContainer');
const file = imageInput.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
const img = new Image();
img.onload = function () {
// Display the selected image
imageContainer.innerHTML = '';
imageContainer.appendChild(img);
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
}
function detectImage() {
if (!model) {
console.error('Model not loaded yet');
return;
}
const imageInput = document.getElementById('imageInput');
const imageContainer = document.getElementById('imageContainer');
const file = imageInput.files[0];
if (file) {
const reader = new FileReader();
reader.onload = async function (e) {
const img = new Image();
img.onload = async function () {
// Convert the resized image to a TensorFlow tensor for prediction
const imageTensor = tf.browser.fromPixels(img)
.resizeNearestNeighbor([224, 224])
.toFloat()
.expandDims();
const scaledImg = imageTensor.div(255);
// Make predictions on the scaled image
const prediction = await model.predict(scaledImg).data();
const predictedClass = classes[prediction.indexOf(Math.max(...prediction))];
imageContainer.innerHTML += `<p class='mt-2 fw-bold fs-1'>Predicted Class: ${predictedClass}</p>`;
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
}
// Load the model when the page is loaded
window.onload = function () {
loadModel();
// Call the handleImageUpload function when an image is chosen
document.getElementById('imageInput').addEventListener('change', handleImageUpload);
};