-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
81 lines (67 loc) · 2.91 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cat, Dog & Horse Classifier</title>
<link rel="stylesheet" href="styles.css">
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
</head>
<body>
<div class="container">
<h1>Cat, Dog & Horse Classifier</h1>
<div class="upload-section">
<button onclick="triggerFileInput()" class="upload-button">UPLOAD IMAGE</button>
<input type="file" accept="image/*" id="imageUpload" onchange="previewImage()" style="display: none;">
<div id="imagePreviewContainer">
<img id="imagePreview" src="" alt="Image Preview">
</div>
</div>
<button onclick="predictImage()" class="predict-button">CLASSIFY IMAGE</button>
<p id="prediction">Prediction will appear here</p>
</div>
<script>
let model;
let imageUploaded = false;
async function loadModel() {
model = await tf.loadGraphModel('tfjs_model/model.json');
}
async function predictImage() {
if (!imageUploaded) {
document.getElementById('prediction').innerText = 'Please upload an image first.';
return;
}
const imageElement = document.getElementById('imagePreview');
const image = await createImageBitmap(imageElement);
const tensor = tf.browser.fromPixels(image)
.resizeNearestNeighbor([120, 120]) // Adjust size as needed
.expandDims()
.toFloat()
.div(tf.scalar(255.0));
const predictions = await model.predict(tensor).data();
const classes = ['Cat', 'Dog', 'Horse'];
let predictionText = "Prediction Probabilities:<br>";
predictions.forEach((probability, index) => {
predictionText += `${classes[index]}: ${(probability * 100).toFixed(2)}%<br>`;
});
document.getElementById('prediction').innerHTML = predictionText;
}
function triggerFileInput() {
document.getElementById('imageUpload').click();
}
function previewImage() {
const file = document.getElementById('imageUpload').files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(event) {
document.getElementById('imagePreview').src = event.target.result;
imageUploaded = true; // Set flag to true when an image is uploaded
document.getElementById('prediction').innerText = ''; // Clear previous prediction message
};
reader.readAsDataURL(file);
}
}
loadModel();
</script>
</body>
</html>