-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
181 lines (150 loc) · 5.59 KB
/
index.html
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Prediction</title>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 20px;
}
.upload-area {
border: 2px dashed #ccc;
padding: 20px;
cursor: pointer;
}
.upload-area.dragover {
border-color: #007bff;
}
#uploaded-image {
max-width: 400px;
/* 限制图片的最大宽度 */
max-height: 300px;
/* 限制图片的最大高度 */
margin-top: 20px;
display: block;
object-fit: contain;
/* 确保图片按比例缩放 */
margin-left: auto;
margin-right: auto;
/* 水平居中 */
}
#prediction {
font-size: 24px;
font-weight: bold;
color: #007bff;
margin: 20px 0;
}
#chart-container {
width: 80%;
margin: 0 auto;
}
</style>
</head>
<body>
<h1>Image Prediction</h1>
<div class="upload-area" id="upload-area">
<p>Click or drag an image to upload</p>
<input type="file" id="file-input" accept="image/*" style="display: none;">
</div>
<img id="uploaded-image" alt="Uploaded Image" style="display: none;">
<div id="prediction" style="display: none;">Most Likely: <span id="top-prediction"></span></div>
<div id="chart-container" style="display: none;">
<canvas id="probability-chart"></canvas>
</div>
<script>
const uploadArea = document.getElementById('upload-area');
const fileInput = document.getElementById('file-input');
const uploadedImage = document.getElementById('uploaded-image');
const predictionText = document.getElementById('prediction');
const topPrediction = document.getElementById('top-prediction');
const chartContainer = document.getElementById('chart-container');
const chartCanvas = document.getElementById('probability-chart');
let chartInstance;
// Drag and drop functionality
uploadArea.addEventListener('click', () => fileInput.click());
uploadArea.addEventListener('dragover', (e) => {
e.preventDefault();
uploadArea.classList.add('dragover');
});
uploadArea.addEventListener('dragleave', () => {
uploadArea.classList.remove('dragover');
});
uploadArea.addEventListener('drop', (e) => {
e.preventDefault();
uploadArea.classList.remove('dragover');
const file = e.dataTransfer.files[0];
if (file) handleFileUpload(file);
});
fileInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) handleFileUpload(file);
});
function handleFileUpload(file) {
const reader = new FileReader();
reader.onload = () => {
uploadedImage.src = reader.result;
uploadedImage.style.display = 'block';
uploadImageToAPI(file);
};
reader.readAsDataURL(file);
}
async function uploadImageToAPI(file) {
const formData = new FormData();
formData.append('file', file);
try {
const response = await axios.post('http://127.0.0.1:8000/predict', formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
});
const predictions = response.data.predictions;
displayPrediction(predictions);
} catch (error) {
console.error('Error uploading image:', error);
alert('Failed to get predictions. Please try again.');
}
}
function displayPrediction(predictions) {
const topPredictionData = predictions[0];
const labels = predictions.map(item => item[0]);
const probabilities = predictions.map(item => (item[1] * 100).toFixed(2));
topPrediction.textContent = `${topPredictionData[0]} (${(topPredictionData[1] * 100).toFixed(2)}%)`;
predictionText.style.display = 'block';
if (chartInstance) chartInstance.destroy();
chartInstance = new Chart(chartCanvas, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: 'Probability (%)',
data: probabilities,
backgroundColor: 'rgba(0, 123, 255, 0.6)',
borderColor: 'rgba(0, 123, 255, 1)',
borderWidth: 1
}]
},
options: {
responsive: true,
plugins: {
legend: {
display: false
}
},
scales: {
y: {
beginAtZero: true,
max: 100
}
}
}
});
chartContainer.style.display = 'block';
}
</script>
</body>
</html>