File size: 2,766 Bytes
7b813cc
 
14d234f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b813cc
14d234f
 
 
 
 
7b813cc
14d234f
 
 
 
 
 
 
 
 
 
 
 
 
 
7b813cc
14d234f
 
 
7b813cc
14d234f
 
7b813cc
14d234f
7b813cc
14d234f
 
 
 
 
7b813cc
14d234f
 
 
7b813cc
 
 
14d234f
 
 
7b813cc
 
 
14d234f
 
 
7b813cc
 
14d234f
 
 
 
 
 
 
 
 
 
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<!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>