jsijan commited on
Commit
e911796
·
verified ·
1 Parent(s): e0ce2d9

Update app/templates/index.html

Browse files
Files changed (1) hide show
  1. app/templates/index.html +66 -70
app/templates/index.html CHANGED
@@ -2,52 +2,50 @@
2
  <html lang="en">
3
  <head>
4
  <meta charset="UTF-8" />
 
5
  <title>Car Angle Classifier</title>
6
  <style>
7
  body {
8
  font-family: sans-serif;
9
- padding: 1rem;
10
  }
11
 
12
  h1 {
 
13
  margin-bottom: 1rem;
14
  }
15
 
16
  #drop-area {
17
  border: 2px dashed #ccc;
18
  border-radius: 10px;
19
- padding: 30px;
20
  text-align: center;
21
- color: #aaa;
22
- margin-bottom: 20px;
23
- cursor: pointer;
24
  }
25
 
26
- #drop-area.highlight {
27
- border-color: purple;
 
 
 
 
 
 
 
28
  }
29
 
30
  .thumb {
31
- display: inline-block;
32
- margin: 10px;
33
  text-align: center;
34
  }
35
 
36
  .thumb img {
37
- max-width: 180px;
38
- max-height: 140px;
39
- display: block;
40
- margin-bottom: 5px;
41
- border-radius: 8px;
42
- }
43
-
44
- #results {
45
- display: flex;
46
- flex-wrap: wrap;
47
  }
48
 
49
- .error {
50
- color: red;
51
  font-weight: bold;
52
  }
53
  </style>
@@ -57,82 +55,80 @@
57
 
58
  <div id="drop-area">
59
  <p>Drag & drop images here or click to select files</p>
60
- <input type="file" id="fileElem" multiple accept="image/*" style="display:none" />
61
  </div>
62
 
63
- <div id="results"></div>
64
 
65
  <script>
66
  const dropArea = document.getElementById("drop-area");
67
  const fileInput = document.getElementById("fileElem");
68
- const resultsContainer = document.getElementById("results");
69
 
70
  // drag events
71
- ;['dragenter', 'dragover'].forEach(eventName => {
72
- dropArea.addEventListener(eventName, e => {
73
  e.preventDefault();
74
- e.stopPropagation();
75
- dropArea.classList.add("highlight");
76
  }, false);
77
  });
78
 
79
- ;['dragleave', 'drop'].forEach(eventName => {
80
- dropArea.addEventListener(eventName, e => {
81
- e.preventDefault();
82
- e.stopPropagation();
83
- dropArea.classList.remove("highlight");
84
  }, false);
85
  });
86
 
 
87
  dropArea.addEventListener("click", () => fileInput.click());
88
  fileInput.addEventListener("change", () => handleFiles(fileInput.files));
89
- dropArea.addEventListener("drop", e => handleFiles(e.dataTransfer.files));
90
 
91
- async function handleFiles(files) {
92
- const previews = await Promise.all([...files].map(file => classifyImage(file)));
93
-
94
- // Sort: move 'unknown' predictions to the end
95
- previews.sort((a, b) => {
96
- if (a.prediction === "unknown") return 1;
97
- if (b.prediction === "unknown") return -1;
98
- return a.prediction.localeCompare(b.prediction);
99
- });
100
-
101
- resultsContainer.innerHTML = "";
102
- previews.forEach(({ preview, prediction, confidence }) => {
103
- const div = document.createElement("div");
104
- div.className = "thumb";
105
- div.innerHTML = `
106
- <img src="${preview}" />
107
- <div><strong>${prediction}</strong> (${confidence})</div>
108
- `;
109
- resultsContainer.appendChild(div);
110
- });
111
  }
112
 
113
- async function classifyImage(file) {
114
- const preview = URL.createObjectURL(file);
 
115
  const formData = new FormData();
116
- formData.append("file", file);
 
 
117
 
118
  try {
119
  const res = await fetch("/predict", {
120
  method: "POST",
121
- body: formData
122
  });
123
- const data = await res.json();
124
- return {
125
- preview,
126
- prediction: data.prediction || "unknown",
127
- confidence: data.confidence || "0"
128
- };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  } catch (err) {
130
- console.error("API error:", err);
131
- return {
132
- preview,
133
- prediction: "error",
134
- confidence: "0"
135
- };
136
  }
137
  }
138
  </script>
 
2
  <html lang="en">
3
  <head>
4
  <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
6
  <title>Car Angle Classifier</title>
7
  <style>
8
  body {
9
  font-family: sans-serif;
10
+ margin: 2rem;
11
  }
12
 
13
  h1 {
14
+ font-size: 2rem;
15
  margin-bottom: 1rem;
16
  }
17
 
18
  #drop-area {
19
  border: 2px dashed #ccc;
20
  border-radius: 10px;
21
+ padding: 2rem;
22
  text-align: center;
23
+ color: #888;
24
+ margin-bottom: 2rem;
 
25
  }
26
 
27
+ #drop-area.hover {
28
+ border-color: #666;
29
+ background-color: #f9f9f9;
30
+ }
31
+
32
+ #gallery {
33
+ display: flex;
34
+ flex-wrap: wrap;
35
+ gap: 1.5rem;
36
  }
37
 
38
  .thumb {
 
 
39
  text-align: center;
40
  }
41
 
42
  .thumb img {
43
+ max-width: 150px;
44
+ border: 1px solid #ccc;
45
+ border-radius: 6px;
 
 
 
 
 
 
 
46
  }
47
 
48
+ .label {
 
49
  font-weight: bold;
50
  }
51
  </style>
 
55
 
56
  <div id="drop-area">
57
  <p>Drag & drop images here or click to select files</p>
58
+ <input type="file" id="fileElem" multiple accept="image/*" style="display:none">
59
  </div>
60
 
61
+ <div id="gallery"></div>
62
 
63
  <script>
64
  const dropArea = document.getElementById("drop-area");
65
  const fileInput = document.getElementById("fileElem");
66
+ const gallery = document.getElementById("gallery");
67
 
68
  // drag events
69
+ ["dragenter", "dragover"].forEach(eventName => {
70
+ dropArea.addEventListener(eventName, (e) => {
71
  e.preventDefault();
72
+ dropArea.classList.add("hover");
 
73
  }, false);
74
  });
75
 
76
+ ["dragleave", "drop"].forEach(eventName => {
77
+ dropArea.addEventListener(eventName, () => {
78
+ dropArea.classList.remove("hover");
 
 
79
  }, false);
80
  });
81
 
82
+ dropArea.addEventListener("drop", handleDrop, false);
83
  dropArea.addEventListener("click", () => fileInput.click());
84
  fileInput.addEventListener("change", () => handleFiles(fileInput.files));
 
85
 
86
+ function handleDrop(e) {
87
+ e.preventDefault();
88
+ handleFiles(e.dataTransfer.files);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  }
90
 
91
+ async function handleFiles(files) {
92
+ gallery.innerHTML = "<p>Processing...</p>";
93
+
94
  const formData = new FormData();
95
+ for (const file of files) {
96
+ formData.append("files", file);
97
+ }
98
 
99
  try {
100
  const res = await fetch("/predict", {
101
  method: "POST",
102
+ body: formData,
103
  });
104
+
105
+ if (!res.ok) throw new Error(`Server error: ${res.status}`);
106
+ const predictions = await res.json();
107
+
108
+ // Sort: move unknown to end, others by descending confidence
109
+ predictions.sort((a, b) => {
110
+ if (a.prediction === "unknown") return 1;
111
+ if (b.prediction === "unknown") return -1;
112
+ return parseFloat(b.confidence) < parseFloat(a.confidence) ? 1 : -1;
113
+ });
114
+
115
+ gallery.innerHTML = "";
116
+ predictions.forEach((result, index) => {
117
+ const reader = new FileReader();
118
+ reader.onload = () => {
119
+ const div = document.createElement("div");
120
+ div.className = "thumb";
121
+ div.innerHTML = `
122
+ <img src="${reader.result}" alt="preview">
123
+ <div class="label">${result.prediction} (${parseFloat(result.confidence).toFixed(2)})</div>
124
+ `;
125
+ gallery.appendChild(div);
126
+ };
127
+ reader.readAsDataURL(files[index]);
128
+ });
129
+
130
  } catch (err) {
131
+ gallery.innerHTML = `<p style="color:red">Error: ${err.message}</p>`;
 
 
 
 
 
132
  }
133
  }
134
  </script>