|
<!DOCTYPE html> |
|
<html lang="en"> |
|
|
|
<head> |
|
<meta charset="UTF-8"> |
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
<title>์ฑ๋ถ๋ถ์ํ</title> |
|
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet"> |
|
<style> |
|
body { |
|
font-family: 'Roboto', sans-serif; |
|
background-color: #f0f2f5; |
|
display: flex; |
|
justify-content: center; |
|
align-items: center; |
|
height: 100vh; |
|
margin: 0; |
|
} |
|
|
|
.container { |
|
background: #fff; |
|
padding: 20px; |
|
border-radius: 8px; |
|
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); |
|
text-align: center; |
|
max-width: 700px; |
|
width: 100%; |
|
} |
|
|
|
h1 { |
|
color: #333; |
|
margin-bottom: 20px; |
|
} |
|
|
|
video, |
|
canvas, |
|
img { |
|
border-radius: 8px; |
|
width: 100%; |
|
max-width: 640px; |
|
height: auto; |
|
margin-bottom: 20px; |
|
} |
|
|
|
button { |
|
background-color: #4CAF50; |
|
color: white; |
|
padding: 10px 20px; |
|
border: none; |
|
border-radius: 5px; |
|
cursor: pointer; |
|
font-size: 16px; |
|
transition: background-color 0.3s, transform 0.3s; |
|
} |
|
|
|
button:hover { |
|
background-color: #45a049; |
|
transform: scale(1.05); |
|
} |
|
|
|
.result { |
|
margin-top: 20px; |
|
font-size: 18px; |
|
font-weight: bold; |
|
} |
|
|
|
.good { |
|
color: green; |
|
} |
|
|
|
.normal { |
|
color: orange; |
|
} |
|
|
|
.dangerous { |
|
color: red; |
|
} |
|
</style> |
|
</head> |
|
|
|
<body> |
|
<div class="container"> |
|
<h1>์ฑ๋ถ๋ถ์ํ</h1> |
|
<video id="video" autoplay></video> |
|
<button id="snap">์ฌ์ง ์ฐ๊ธฐ</button> |
|
<canvas id="canvas" style="display:none;"></canvas> |
|
<p id="analysisResult" class="result"></p> |
|
</div> |
|
|
|
<script> |
|
const video = document.getElementById('video'); |
|
const canvas = document.getElementById('canvas'); |
|
const snap = document.getElementById('snap'); |
|
const context = canvas.getContext('2d'); |
|
const analysisResult = document.getElementById('analysisResult'); |
|
|
|
navigator.mediaDevices.getUserMedia({ video: true }) |
|
.then(stream => { |
|
video.srcObject = stream; |
|
}) |
|
.catch(err => { |
|
console.error("Error accessing webcam: " + err); |
|
}); |
|
|
|
function dataURLToFile(dataurl, filename) { |
|
let arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1], |
|
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); |
|
while (n--) { |
|
u8arr[n] = bstr.charCodeAt(n); |
|
} |
|
return new File([u8arr], filename, { type: mime }); |
|
} |
|
|
|
function processAnalysisResult(result) { |
|
const match = result.match(/\d+/); |
|
if (match) { |
|
const sugarContent = parseInt(match[0], 10); |
|
let message = ''; |
|
let className = ''; |
|
if (sugarContent >= 0 && sugarContent <= 20) { |
|
message = 'good'; |
|
className = 'good'; |
|
} else if (sugarContent >= 21 && sugarContent <= 50) { |
|
message = 'normal'; |
|
className = 'normal'; |
|
} else if (sugarContent >= 51) { |
|
message = 'dangerous'; |
|
className = 'dangerous'; |
|
} |
|
analysisResult.textContent = `${message}: ${sugarContent}g`; |
|
analysisResult.className = className; |
|
} else { |
|
analysisResult.textContent = 'Error: Could not analyze image.'; |
|
analysisResult.className = ''; |
|
} |
|
} |
|
|
|
snap.addEventListener('click', () => { |
|
context.drawImage(video, 0, 0, canvas.width, canvas.height); |
|
const dataURL = canvas.toDataURL('image/png'); |
|
const file = dataURLToFile(dataURL, 'captured_image.png'); |
|
|
|
const formData = new FormData(); |
|
formData.append('image', file); |
|
|
|
fetch('/save_image', { |
|
method: 'POST', |
|
body: formData |
|
}) |
|
.then(response => response.json()) |
|
.then(data => { |
|
if (data.analysis_result) { |
|
processAnalysisResult(data.analysis_result); |
|
} else { |
|
analysisResult.textContent = 'Error: ์ฑ๋ถ๋ถ์ํ๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.'; |
|
analysisResult.className = ''; |
|
} |
|
alert(data.message); |
|
}) |
|
.catch(error => { |
|
console.error('Error:', error); |
|
}); |
|
}); |
|
</script> |
|
</body> |
|
|
|
</html> |