-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
108 lines (90 loc) · 3.06 KB
/
script.js
File metadata and controls
108 lines (90 loc) · 3.06 KB
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
const ROBOTFLOW_API_KEY = 'rf_dqDbILKhaEMFeRG4AC8EQYkAxl33';
const MODEL_ID = 'brainmri_classification/1';
// Get DOM Elements
const uploadArea = document.getElementById('uploadArea');
const imageUpload = document.getElementById('imageUpload');
const uploadButton = document.getElementById('uploadButton');
const uploadedImage = document.getElementById('uploadedImage');
const imagePreview = document.getElementById('imagePreview');
const predictionResult = document.getElementById('predictionResult');
const aiReview = document.getElementById('aiReview');
const loadingSpinner = document.getElementById('loadingSpinner');
let roboflowModel;
// --- Event Listeners for File Upload ---
// Click "Click to Upload" button
uploadButton.addEventListener('click', () => {
imageUpload.click();
});
// Handle file selection via input
imageUpload.addEventListener('change', (event) => {
const file = event.target.files[0];
if (file) {
handleFile(file);
}
});
// Handle drag over for styling
uploadArea.addEventListener('dragover', (event) => {
event.preventDefault();
uploadArea.classList.add('drag-over');
});
// Handle drag leave for styling
uploadArea.addEventListener('dragleave', (event) => {
event.preventDefault();
uploadArea.classList.remove('drag-over');
});
// Handle drop event
uploadArea.addEventListener('drop', (event) => {
event.preventDefault();
uploadArea.classList.remove('drag-over');
const file = event.dataTransfer.files[0];
if (file) {
handleFile(file);
}
});
// --- Core Logic for Image Handling and Prediction ---
function fileToBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
const base64 = reader.result.split(',')[1];
resolve(base64);
};
reader.onerror = reject;
reader.readAsDataURL(file);
});
}
async function handleFile(file) {
if (!file.type.startsWith('image/')) {
alert('Please upload an image file (e.g., JPG, PNG).');
return;
}
predictionResult.textContent = 'Analyzing...';
aiReview.textContent = 'Please wait while the AI processes the image.';
uploadedImage.style.display = 'none';
loadingSpinner.style.display = 'flex';
try {
const formData = new FormData();
formData.append('file', file);
// Send to our Flask backend
const response = await fetch('http://localhost:5000/predict', {
method: 'POST',
body: formData
});
if (!response.ok) {
throw new Error('Could not get prediction');
}
const data = await response.json();
// Set preview
uploadedImage.src = data.image;
uploadedImage.style.display = 'block';
// Display results
predictionResult.innerHTML = `<strong>${data.prediction.class}</strong> (Confidence: ${data.prediction.confidence}%)`;
aiReview.textContent = data.review;
} catch (error) {
predictionResult.textContent = 'Error: Could not get prediction.';
aiReview.textContent = 'There was an issue processing the image. Please try again.';
console.error(error);
} finally {
loadingSpinner.style.display = 'none';
}
}