Spaces:
Paused
Paused
File size: 10,327 Bytes
ecac36d | 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>โปรแกรมนับท่อจากรูปภาพ - NPISB</title>
<!-- Include Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
<!-- Include Cropper.js CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.5.9/cropper.min.css">
<!-- Custom CSS for centering the spinner and bottom navigation -->
<style>
/* Center the spinner */
.spinner-container {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 9999;
display: none;
/* Initially hidden */
}
/* Add a semi-transparent white overlay for loading */
.overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(255, 255, 255, 0.7);
/* White with 70% opacity */
z-index: 999;
/* Ensure it covers everything */
display: none;
/* Initially hidden */
}
</style>
</head>
<body>
<div class="overlay" id="loadingOverlay"></div>
<!-- Loading spinner -->
<div class="spinner-container">
<div class="text-center">
<div class="spinner-border text-primary" role="status">
<span class="sr-only">...</span>
</div>
<p>
<h2>กำลังประมวลผล...</h2>
</p>
</div>
</div>
<nav class="navbar navbar-dark bg-dark fixed-top">
<span class="navbar-brand mb-0 h1">โปรแกรมนับท่อจากรูปภาพ : NPISB</span>
<!-- Button added to the navbar -->
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<div>
<button id="buttonreset" style="display: none;" class="btn btn-warning btn-lg btn-block"
onclick="resetFormAndScrollUp()">Reset</button>
<button id="buttoncount" style="display: none;" class="btn btn-primary btn-lg btn-block"
onclick="cropAndUpload()">นับจำนวนท่อ</button>
</div>
</li>
</ul>
</nav>
<section class="container" style="padding-top: 50px;">
<div class="row mt-5">
<div class="col-md-12" id="top">
<div id="newcount" class="alert alert-primary" style="display: none;" role="alert">
โปรดเลือกรูปภาพ หรือถ่ายรูปใหม่ !!
</div>
<div class="custom-file">
<input type="file" class="custom-file-input" id="photoInput" accept="image/*">
<label class="custom-file-label" for="photoInput">ถ่ายรูปหรือเลือกรูป...</label>
</div>
</div>
</div>
<div class="row mt-3">
<div class="col-md-12">
<img id="preview" style="display: none;" class="img-fluid">
</div>
</div>
<div class="row mt-3">
<div class="col-md-12">
<div id="result"></div>
</div>
</div>
</section>
<footer class="footer text-center py-2">
<div class="container">
<span class="text-muted">© 2023 NPI-SB - Digital Infrastructure</span>
</div>
</footer>
<!-- Include Bootstrap JavaScript -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<!-- Include Cropper.js and spin.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.5.9/cropper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/spin.js/2.3.2/spin.min.js"></script>
<script>
// Initialize Cropper.js
let cropper;
// Function to handle file selection and image preview
const photoInput = document.getElementById('photoInput');
const preview = document.getElementById('preview');
const buttoncount = document.getElementById('buttoncount');
const newcount = document.getElementById('newcount');
const buttonreset = document.getElementById('buttonreset');
const customFileLabel = document.querySelector('.custom-file-label');
const resultDiv = document.getElementById('result');
const spinnerContainer = document.querySelector('.spinner-container');
function resetFormAndScrollUp() {
// Scroll up to the photo input
$('html, body').animate({
scrollTop: 0 // Scroll to the top of the page
}, 1000); // 1000 milliseconds (1 second) for the scroll animation
buttonreset.style.display = 'none';
newcount.style.display = 'block';
}
// Function to handle file selection and image preview
photoInput.addEventListener('change', function (e) {
const file = e.target.files[0];
if (file) {
customFileLabel.textContent = file.name; // Display the selected file name
buttoncount.style.display = 'block';
// Reset the Cropper.js instance and clear the previous image
if (cropper) {
cropper.destroy();
preview.removeAttribute('src');
preview.style.display = 'none';
buttoncount.style.display = 'block';
}
const reader = new FileReader();
reader.onload = function (e) {
// Set the new image source and display it
preview.src = e.target.result;
preview.style.display = 'block';
// Initialize Cropper.js with the new image
cropper = new Cropper(preview, {
//aspectRatio: 1, // You can adjust this for the desired aspect ratio
viewMode: 2, // Adjust the view mode as needed
});
};
reader.readAsDataURL(file);
} else {
customFileLabel.textContent = 'Choose a photo...';
// Reset the Cropper.js instance and clear the previous image
if (cropper) {
cropper.destroy();
preview.removeAttribute('src');
preview.style.display = 'none';
}
buttoncount.style.display = 'none';
}
});
preview.addEventListener('crop', function (event) {
buttonreset.style.display = 'none';
buttoncount.style.display = 'block';
});
// Function to make the API call
function cropAndUpload() {
spinnerContainer.style.display = 'block'; // Show the spinner
loadingOverlay.style.display = 'block';
newcount.style.display = 'none';
// Get the cropped image data
const croppedImageData = cropper.getCroppedCanvas().toDataURL('image/jpeg');
const apiUrl = '/detectImage';
// Create a FormData object to send the image as an UploadFile
const formData = new FormData();
const blob = dataURItoBlob(croppedImageData);
formData.append('file', blob, 'image.jpg'); // 'image.jpg' is the filename
// Make the API call using fetch
fetch(apiUrl, {
method: 'POST',
body: formData
})
.then(response => {
if (response.ok) {
// If the response is successful, display the image
response.blob().then(blob => {
const imgUrl = URL.createObjectURL(blob);
resultDiv.innerHTML = `<img src="${imgUrl}" class="img-fluid"> `;
$('html, body').animate({
scrollTop: $('#result').offset().top
}, 1000); // 1000 milliseconds (1 second) for the scroll animation
spinnerContainer.style.display = 'none'; // Hide the spinner
buttoncount.style.display = 'none';
buttonreset.style.display = 'block';
});
} else {
console.error('API Error:', response.status);
resultDiv.innerHTML = 'API Error. Please try again later.';
spinnerContainer.style.display = 'none'; // Hide the spinner
}
loadingOverlay.style.display = 'none';
})
.catch(error => {
console.error('API Error:', error);
resultDiv.innerHTML = 'API Error. Please try again later.';
spinnerContainer.style.display = 'none'; // Hide the spinner
});
}
// Function to convert a data URI to a Blob
function dataURItoBlob(dataURI) {
const byteString = atob(dataURI.split(',')[1]);
const mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
const ab = new ArrayBuffer(byteString.length);
const ia = new Uint8Array(ab);
for (let i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ab], { type: mimeString });
}
</script>
</body>
</html> |