Spaces:
Sleeping
Sleeping
Update templates/index.html
Browse files- templates/index.html +32 -23
templates/index.html
CHANGED
@@ -83,7 +83,7 @@
|
|
83 |
<h1>์ฑ๋ถ๋ถ์ํ</h1>
|
84 |
<video id="video" autoplay></video>
|
85 |
<button id="snap">์ฌ์ง ์ฐ๊ธฐ</button>
|
86 |
-
<canvas id="canvas"></canvas>
|
87 |
<p id="analysisResult" class="result"></p>
|
88 |
</div>
|
89 |
|
@@ -102,32 +102,35 @@
|
|
102 |
console.error("Error accessing webcam: " + err);
|
103 |
});
|
104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
function processAnalysisResult(result) {
|
106 |
const match = result.match(/\d+/);
|
107 |
if (match) {
|
108 |
const sugarContent = parseInt(match[0], 10);
|
109 |
let message = '';
|
110 |
let className = '';
|
111 |
-
let count = '';
|
112 |
-
|
113 |
if (sugarContent >= 0 && sugarContent <= 20) {
|
114 |
message = 'good';
|
115 |
className = 'good';
|
116 |
-
count = match;
|
117 |
} else if (sugarContent >= 21 && sugarContent <= 50) {
|
118 |
message = 'normal';
|
119 |
className = 'normal';
|
120 |
-
count = match;
|
121 |
} else if (sugarContent >= 51) {
|
122 |
message = 'dangerous';
|
123 |
className = 'dangerous';
|
124 |
-
count = match;
|
125 |
}
|
126 |
-
|
127 |
-
analysisResult.textContent = message + match;
|
128 |
analysisResult.className = className;
|
129 |
} else {
|
130 |
-
analysisResult.textContent = 'Error: Could not analyze image.'
|
131 |
analysisResult.className = '';
|
132 |
}
|
133 |
}
|
@@ -135,22 +138,28 @@
|
|
135 |
snap.addEventListener('click', () => {
|
136 |
context.drawImage(video, 0, 0, canvas.width, canvas.height);
|
137 |
const dataURL = canvas.toDataURL('image/png');
|
|
|
|
|
|
|
|
|
|
|
138 |
fetch('/save_image', {
|
139 |
method: 'POST',
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
|
|
154 |
});
|
155 |
</script>
|
156 |
</body>
|
|
|
83 |
<h1>์ฑ๋ถ๋ถ์ํ</h1>
|
84 |
<video id="video" autoplay></video>
|
85 |
<button id="snap">์ฌ์ง ์ฐ๊ธฐ</button>
|
86 |
+
<canvas id="canvas" style="display:none;"></canvas>
|
87 |
<p id="analysisResult" class="result"></p>
|
88 |
</div>
|
89 |
|
|
|
102 |
console.error("Error accessing webcam: " + err);
|
103 |
});
|
104 |
|
105 |
+
function dataURLToFile(dataurl, filename) {
|
106 |
+
let arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
|
107 |
+
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
|
108 |
+
while (n--) {
|
109 |
+
u8arr[n] = bstr.charCodeAt(n);
|
110 |
+
}
|
111 |
+
return new File([u8arr], filename, { type: mime });
|
112 |
+
}
|
113 |
+
|
114 |
function processAnalysisResult(result) {
|
115 |
const match = result.match(/\d+/);
|
116 |
if (match) {
|
117 |
const sugarContent = parseInt(match[0], 10);
|
118 |
let message = '';
|
119 |
let className = '';
|
|
|
|
|
120 |
if (sugarContent >= 0 && sugarContent <= 20) {
|
121 |
message = 'good';
|
122 |
className = 'good';
|
|
|
123 |
} else if (sugarContent >= 21 && sugarContent <= 50) {
|
124 |
message = 'normal';
|
125 |
className = 'normal';
|
|
|
126 |
} else if (sugarContent >= 51) {
|
127 |
message = 'dangerous';
|
128 |
className = 'dangerous';
|
|
|
129 |
}
|
130 |
+
analysisResult.textContent = `${message}: ${sugarContent}g`;
|
|
|
131 |
analysisResult.className = className;
|
132 |
} else {
|
133 |
+
analysisResult.textContent = 'Error: Could not analyze image.';
|
134 |
analysisResult.className = '';
|
135 |
}
|
136 |
}
|
|
|
138 |
snap.addEventListener('click', () => {
|
139 |
context.drawImage(video, 0, 0, canvas.width, canvas.height);
|
140 |
const dataURL = canvas.toDataURL('image/png');
|
141 |
+
const file = dataURLToFile(dataURL, 'captured_image.png');
|
142 |
+
|
143 |
+
const formData = new FormData();
|
144 |
+
formData.append('image', file);
|
145 |
+
|
146 |
fetch('/save_image', {
|
147 |
method: 'POST',
|
148 |
+
body: formData
|
149 |
+
})
|
150 |
+
.then(response => response.json())
|
151 |
+
.then(data => {
|
152 |
+
if (data.analysis_result) {
|
153 |
+
processAnalysisResult(data.analysis_result);
|
154 |
+
} else {
|
155 |
+
analysisResult.textContent = 'Error: ์ฑ๋ถ๋ถ์ํ๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.';
|
156 |
+
analysisResult.className = '';
|
157 |
+
}
|
158 |
+
alert(data.message);
|
159 |
+
})
|
160 |
+
.catch(error => {
|
161 |
+
console.error('Error:', error);
|
162 |
+
});
|
163 |
});
|
164 |
</script>
|
165 |
</body>
|