reidddd's picture
add fetch image api
14d234f
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>Upload File</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}
#preview {
margin-top: 20px;
max-width: 500px;
max-height: 500px;
display: none;
}
</style>
</head>
<body>
<h1>Vehicle Damage Detection</h1>
<form
id="uploadForm"
enctype="multipart/form-data"
>
<label for="file">Upload an image:</label>
<input
type="file"
id="file"
name="file"
accept="image/*"
required
/>
<br /><br />
<img
id="preview"
alt="Image Preview"
/>
<br /><br />
<button type="submit">Upload and Analyze</button>
</form>
<p id="response"></p>
<script>
const fileInput = document.getElementById("file");
const preview = document.getElementById("preview");
const uploadForm = document.getElementById("uploadForm");
const responseElement = document.getElementById("response");
// Preview the selected image
fileInput.addEventListener("change", function () {
const file = fileInput.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
preview.src = e.target.result;
preview.style.display = "block";
};
reader.readAsDataURL(file);
} else {
preview.style.display = "none";
}
});
// Handle form submission
uploadForm.addEventListener("submit", async function (event) {
event.preventDefault();
const formData = new FormData();
formData.append("file", fileInput.files[0]);
responseElement.textContent = "Uploading and analyzing...";
try {
const response = await fetch("/fetch-image", {
method: "POST",
body: formData,
});
if (response.ok) {
const result = await response.json();
responseElement.innerHTML = `
<strong>Analysis Result:</strong><br>
Total Cost: $${result.total_cost}<br>
<ul>
${result.damages
.map(
(damage) => `
<li>
Part: ${damage.part}, Area: ${damage.area_pixels} pixels, Cost: $${damage.cost_usd}
</li>
`
)
.join("")}
</ul>
`;
} else {
responseElement.textContent = "Error: Unable to analyze the image.";
}
} catch (error) {
responseElement.textContent = "Error: " + error.message;
}
});
</script>
</body>
</html>