Spaces:
Sleeping
Sleeping
yukiapple323
commited on
Update index.html
Browse files- index.html +25 -20
index.html
CHANGED
@@ -39,9 +39,13 @@
|
|
39 |
const canvas = document.getElementById('canvas');
|
40 |
const output = document.getElementById('output');
|
41 |
const captureButton = document.getElementById('capture');
|
|
|
42 |
navigator.mediaDevices.getUserMedia({
|
43 |
video: {
|
44 |
-
facingMode: { exact: "environment" }
|
|
|
|
|
|
|
45 |
}
|
46 |
})
|
47 |
.then(stream => {
|
@@ -50,54 +54,55 @@
|
|
50 |
.catch(err => {
|
51 |
console.error("Error accessing the camera: ", err);
|
52 |
});
|
|
|
53 |
captureButton.addEventListener('click', () => {
|
54 |
// Draw the video frame to the canvas
|
55 |
canvas.width = video.videoWidth;
|
56 |
canvas.height = video.videoHeight;
|
57 |
const context = canvas.getContext('2d');
|
58 |
context.drawImage(video, 0, 0, canvas.width, canvas.height);
|
|
|
59 |
// Convert canvas to image data
|
60 |
const dataURL = canvas.toDataURL('image/png');
|
|
|
61 |
// Process the image with Tesseract
|
62 |
Tesseract.recognize(
|
63 |
dataURL,
|
64 |
-
'kor',
|
65 |
-
"image-to-text",
|
66 |
-
model="team-lucid/trocr-small-korean",
|
67 |
{
|
68 |
logger: m => console.log(m)
|
69 |
}
|
70 |
).then(({ data: { text } }) => {
|
71 |
console.log(text);
|
72 |
analyzeNutrition(text);
|
|
|
|
|
73 |
});
|
74 |
});
|
|
|
75 |
function analyzeNutrition(text) {
|
76 |
-
// Extract
|
77 |
-
const
|
78 |
-
|
79 |
-
|
80 |
-
if (match) {
|
81 |
-
const sugarContent = parseFloat(match[1]);
|
82 |
let message = `Sugar content: ${sugarContent}g - `;
|
|
|
83 |
if (sugarContent > 20) {
|
84 |
message += 'Dangerous';
|
85 |
-
|
86 |
-
} else if (sugarContent
|
87 |
message += 'Normal';
|
88 |
-
|
89 |
} else {
|
90 |
message += 'Good';
|
91 |
-
|
92 |
}
|
93 |
-
|
94 |
} else {
|
95 |
-
|
96 |
-
|
97 |
}
|
98 |
}
|
99 |
-
}
|
100 |
-
}
|
101 |
</script>
|
102 |
</body>
|
103 |
-
</html>
|
|
|
39 |
const canvas = document.getElementById('canvas');
|
40 |
const output = document.getElementById('output');
|
41 |
const captureButton = document.getElementById('capture');
|
42 |
+
|
43 |
navigator.mediaDevices.getUserMedia({
|
44 |
video: {
|
45 |
+
facingMode: { exact: "environment" },
|
46 |
+
width: { ideal: 2000 },
|
47 |
+
height: { ideal: 2000 },
|
48 |
+
advanced: [{ focusMode: "continuous" }]
|
49 |
}
|
50 |
})
|
51 |
.then(stream => {
|
|
|
54 |
.catch(err => {
|
55 |
console.error("Error accessing the camera: ", err);
|
56 |
});
|
57 |
+
|
58 |
captureButton.addEventListener('click', () => {
|
59 |
// Draw the video frame to the canvas
|
60 |
canvas.width = video.videoWidth;
|
61 |
canvas.height = video.videoHeight;
|
62 |
const context = canvas.getContext('2d');
|
63 |
context.drawImage(video, 0, 0, canvas.width, canvas.height);
|
64 |
+
|
65 |
// Convert canvas to image data
|
66 |
const dataURL = canvas.toDataURL('image/png');
|
67 |
+
|
68 |
// Process the image with Tesseract
|
69 |
Tesseract.recognize(
|
70 |
dataURL,
|
71 |
+
'kor+eng', // Use both Korean and English
|
|
|
|
|
72 |
{
|
73 |
logger: m => console.log(m)
|
74 |
}
|
75 |
).then(({ data: { text } }) => {
|
76 |
console.log(text);
|
77 |
analyzeNutrition(text);
|
78 |
+
}).catch(err => {
|
79 |
+
console.error("Tesseract error: ", err);
|
80 |
});
|
81 |
});
|
82 |
+
|
83 |
function analyzeNutrition(text) {
|
84 |
+
// Extract sugar content from the recognized text
|
85 |
+
const sugarMatch = text.match(/(\d+(\.\d+)?)\s*(g|grams|그램)?\s*(sugar|당)/i);
|
86 |
+
if (sugarMatch) {
|
87 |
+
const sugarContent = parseFloat(sugarMatch[1]);
|
|
|
|
|
88 |
let message = `Sugar content: ${sugarContent}g - `;
|
89 |
+
|
90 |
if (sugarContent > 20) {
|
91 |
message += 'Dangerous';
|
92 |
+
output.className = 'red';
|
93 |
+
} else if (sugarContent >= 10 && sugarContent <= 19) {
|
94 |
message += 'Normal';
|
95 |
+
output.className = 'yellow';
|
96 |
} else {
|
97 |
message += 'Good';
|
98 |
+
output.className = 'green';
|
99 |
}
|
100 |
+
output.textContent = message;
|
101 |
} else {
|
102 |
+
output.textContent = 'Sugar content not found';
|
103 |
+
output.className = '';
|
104 |
}
|
105 |
}
|
|
|
|
|
106 |
</script>
|
107 |
</body>
|
108 |
+
</html>
|