Spaces:
Runtime error
Runtime error
File size: 2,283 Bytes
709edf5 |
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 |
let model;
const chooseFiles = document.getElementById('chooseFiles');
const modelNameSelect = document.getElementById("modelNameSelect");
const segmentImageButton = document.getElementById("segmentImage");
const legendsDiv = document.getElementById("legends");
const image = document.getElementById('image');
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
chooseFiles.onchange = () => {
const [file] = chooseFiles.files
if (file) {
image.src = URL.createObjectURL(file);
}
};
segmentImageButton.onclick = predict;
document.getElementById("loadModel").onclick = async () => {
segmentImageButton.disabled = true;
updateModelLoadStatus("Model Loading...");
const modelName = modelNameSelect.options[modelNameSelect.selectedIndex].value;
await loadModel(modelName);
updateModelLoadStatus(modelName + " model loaded!");
segmentImageButton.disabled = false;
};
async function loadModel(modelName) {
model = await deeplab.load({ "base": modelName, "quantizationBytes": 2 });
}
function updateModelLoadStatus(status) {
document.getElementById("modelLoadedStatus").innerHTML = status;
}
async function predict() {
let prediction = await model.segment(image);
renderPrediction(prediction);
}
function renderPrediction(prediction) {
const { legend, height, width, segmentationMap } = prediction;
//console.log(`prediction: ${JSON.stringify(prediction)}`);
const segmentationMapData = new ImageData(segmentationMap, width, height);
console.log(segmentationMapData)
canvas.width = width;
canvas.height = height;
ctx.putImageData(segmentationMapData, 0, 0);
displayLegends(legend);
}
function displayLegends(legendObj) {
legendsDiv.innerHTML = "";
document.getElementById("legendLabel").style.visibility = "visible";
Object.keys(legendObj).forEach((legend) => {
const [red, green, blue] = legendObj[legend];
const span = document.createElement('span');
span.innerHTML = legend;
span.style.backgroundColor = `rgb(${red}, ${green}, ${blue})`;
span.style.padding = '10px';
span.style.marginRight = '10px';
span.style.color = '#ffffff';
legendsDiv.appendChild(span);
});
} |