|
|
|
const fileInput = document.getElementById('file-input'); |
|
const button = document.getElementById('button'); |
|
const errorMessage = document.getElementById('error-message'); |
|
const container = document.getElementById('container'); |
|
|
|
const imageContainer = document.getElementById('image-container'); |
|
const previewImage = document.getElementById('file-image'); |
|
const resultImage = document.getElementById('result-image'); |
|
|
|
|
|
const fileTitle = document.querySelector(".title"); |
|
const predInfo = document.querySelector(".pred-info"); |
|
|
|
const predictedValue = document.querySelector(".pred-info h3 span"); |
|
|
|
|
|
const topPred = document.querySelector(".pred-info ul"); |
|
|
|
|
|
|
|
const sendByCanvas = document.getElementById("canvas-send"); |
|
|
|
|
|
function createBlobFromImageData(imageData, mimeType = "image/jpeg") { |
|
const buffer = new ArrayBuffer(imageData.length); |
|
const view = new Uint8Array(buffer); |
|
for (let i = 0; i < imageData.length; i++) { |
|
view[i] = imageData.charCodeAt(i); |
|
} |
|
|
|
return new Blob([buffer], { type: mimeType }); |
|
} |
|
|
|
|
|
function initialState() { |
|
button.textContent = 'Choose file'; |
|
predInfo.style.display = 'none'; |
|
fileTitle.textContent = "Upload your file here"; |
|
imageContainer.style.display = 'none'; |
|
} |
|
|
|
function displayError(message, timeout = 3000) { |
|
errorMessage.textContent = message; |
|
errorMessage.style.display = 'block'; |
|
initialState(); |
|
setTimeout(() => { |
|
errorMessage.style.display = 'none'; |
|
}, timeout); |
|
} |
|
|
|
|
|
async function sendImageToServer(image, filename) { |
|
|
|
|
|
|
|
|
|
try { |
|
const formData = new FormData(); |
|
|
|
console.log(image) |
|
|
|
const base64Data = image.split(',')[1]; |
|
|
|
formData.append('file-input-64', base64Data); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
predInfo.style.display = 'none'; |
|
button.textContent = 'Processing....'; |
|
fileTitle.textContent = filename; |
|
|
|
|
|
|
|
console.log("Submit button is clicked"); |
|
|
|
const response = await fetch('/upload', { |
|
method: 'POST', |
|
body: formData |
|
}); |
|
|
|
topPred.innerHTML = ""; |
|
console.log('I am here') |
|
|
|
const jsonData = await response.json(); |
|
|
|
console.log(jsonData) |
|
console.log(typeof (jsonData)) |
|
const jsonObject = JSON.parse(jsonData); |
|
|
|
if (!response.ok) { |
|
throw new Error(`Error uploading file: ${jsonData.error}`); |
|
} |
|
|
|
resultImage.src = `data:image/jpeg;base64,` + jsonObject["ig"]; |
|
predInfo.style.display = 'flex'; |
|
|
|
predictedValue.textContent = `${jsonObject.item[0]} ( ${(jsonObject.prob[0] * 100).toFixed(3)}%)`; |
|
|
|
for (let index = 0; index < jsonObject.item.length; index++) { |
|
const newItem = document.createElement("li"); |
|
newItem.textContent = `${jsonObject.item[index]} ( ${(jsonObject.prob[index] * 100).toFixed(3)}%)` |
|
topPred.appendChild(newItem); |
|
} |
|
|
|
|
|
|
|
console.log('File uploaded successfully!'); |
|
|
|
} catch (error) { |
|
|
|
displayError(error.message); |
|
} finally { |
|
|
|
button.textContent = 'Choose file'; |
|
} |
|
} |
|
|
|
async function sentImageDataToSeverViaImage(event) { |
|
|
|
const file = event.target.files[0]; |
|
const allowedExtensions = ['jpeg', 'jpg']; |
|
|
|
const extension = file.name.split('.').pop().toLowerCase(); |
|
|
|
|
|
|
|
|
|
if (!allowedExtensions.includes(extension)) { |
|
event.target.value = ''; |
|
displayError('Invalid file format. Please upload a JPG or JPEG image.') |
|
} else { |
|
|
|
|
|
const reader = new FileReader(); |
|
|
|
|
|
reader.onload = function (event) { |
|
previewImage.src = event.target.result; |
|
resultImage.src = ''; |
|
|
|
imageContainer.style.display = 'flex'; |
|
|
|
}; |
|
|
|
reader.readAsDataURL(file); |
|
|
|
|
|
|
|
try { |
|
const formData = new FormData(); |
|
formData.append('file-input', file); |
|
|
|
|
|
|
|
button.textContent = 'Processing....'; |
|
fileTitle.textContent = file.name; |
|
predInfo.style.display = 'none'; |
|
|
|
|
|
|
|
|
|
const response = await fetch('/upload', { |
|
method: 'POST', |
|
body: formData |
|
}); |
|
|
|
if (!response.ok) { |
|
throw new Error(`Error uploading file: ${response.statusText}`); |
|
} |
|
|
|
topPred.innerHTML = ""; |
|
|
|
const jsonData = await response.json(); |
|
|
|
console.log(jsonData) |
|
console.log(typeof (jsonData)) |
|
const jsonObject = JSON.parse(jsonData); |
|
|
|
|
|
resultImage.src = `data:image/jpeg;base64,` + jsonObject["ig"]; |
|
predInfo.style.display = 'flex'; |
|
|
|
predictedValue.textContent = `${jsonObject.item[0]} ( ${(jsonObject.prob[0] * 100).toFixed(3)}%)`; |
|
|
|
for (let index = 0; index < jsonObject.item.length; index++) { |
|
const newItem = document.createElement("li"); |
|
newItem.textContent = `${jsonObject.item[index]} ( ${(jsonObject.prob[index] * 100).toFixed(3)}%)` |
|
topPred.appendChild(newItem); |
|
} |
|
|
|
|
|
|
|
console.log('File uploaded successfully!'); |
|
|
|
} catch (error) { |
|
errorMessage.textContent = `Error uploading file: ${error.message}`; |
|
} finally { |
|
|
|
button.textContent = 'Choose file'; |
|
} |
|
errorMessage.style.display = 'none'; |
|
} |
|
} |
|
|
|
|
|
async function sentImageDataToSeverViaCanvas() { |
|
const canvas = document.getElementById('drawing-board'); |
|
var dataURL = canvas.toDataURL("image/jpeg"); |
|
|
|
previewImage.src = dataURL; |
|
resultImage.src = ''; |
|
|
|
imageContainer.style.display = 'flex'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
closeDialog(); |
|
|
|
await sendImageToServer(dataURL, 'canvas_image.jpeg'); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
button.addEventListener('click', () => { |
|
errorMessage.style.display = 'none'; |
|
fileInput.click(); |
|
}); |
|
|
|
|
|
|
|
|
|
sendByCanvas.addEventListener('click', async () => { |
|
|
|
|
|
|
|
sentImageDataToSeverViaCanvas(); |
|
}); |
|
|
|
|
|
fileInput.addEventListener('change', async (event) => { |
|
sentImageDataToSeverViaImage(event); |
|
}); |