pipecounting / static /index.html
bankba's picture
Upload 8 files
ecac36d
Raw History Blame Contribute Delete
10.3 kB
<!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>