-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
78 lines (66 loc) · 2.37 KB
/
main.js
File metadata and controls
78 lines (66 loc) · 2.37 KB
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
import * as tf from "@tensorflow/tfjs";
async function loadDenseNetModel() {
document.getElementById("predictionResult").innerText = `Loading Model ...
(can take up to 1 min)`;
// Load the DenseNet model from the local files
// const model = await tf.loadLayersModel("/mymodel2/model.json");
const model = await tf.loadLayersModel(
"https://raw.githubusercontent.com/SpectralGT/ProjectK_model/refs/heads/main/model.json"
);
console.log("DenseNet model loaded successfully");
document.getElementById("predictionResult").innerText = `Model Loaded
(first prediction takes time)`;
return model;
}
function preprocessImage(image) {
// Convert the image to a tensor and resize it to 224x224 pixels
let tensor = tf.browser
.fromPixels(image)
.resizeBilinear([224, 224]) // Resize the image to 224x224
.toFloat()
.expandDims(); // Add a batch dimension (shape: [1, 224, 224, 3])
// Normalize the image to range [-1, 1]
return tensor.div(127.5).sub(1);
}
async function classifyImage(model, imageElement) {
const processedImage = preprocessImage(imageElement);
const prediction = model.predict(processedImage);
// Get the class with the highest score
const predictedClass = prediction.argMax(-1);
const predictedClassIndex = (await predictedClass.data())[0];
console.log(await prediction.data());
// Display the prediction
const diseases = [
"Bacterial Pneumonia",
"Corona Virus Disease",
"Normal",
"Tuberculosis",
"Viral Pneumonia",
];
document.getElementById(
"predictionResult"
).innerText = `Predicted Disease: ${diseases[predictedClassIndex]}`;
}
// Load the model when the page loads
let densenetModel;
window.onload = async function () {
densenetModel = await loadDenseNetModel();
// Set up event listener for image input
const imageInput = document.getElementById("imageInput");
const selectedImage = document.getElementById("selectedImage");
imageInput.addEventListener("change", (event) => {
const file = event.target.files[0];
if (file) {
// Display the selected image
const reader = new FileReader();
reader.onload = function (e) {
selectedImage.src = e.target.result;
};
reader.readAsDataURL(file);
// Classify the image after it's loaded
selectedImage.onload = function () {
classifyImage(densenetModel, selectedImage);
};
}
});
};